阿里云服务器安装后-移动MYSQL数据库文件位置

01/15/2016

1.停止MYSQL服务,移动默认数据目录 /var/lib/mysql 至指定位置.

tianyangtax@iZ25tc8mln7Z:~$ sudo service mysql stop
mysql stop/waiting
tianyangtax@iZ25tc8mln7Z:~$ cd /mnt
tianyangtax@iZ25tc8mln7Z:/mnt$ sudo mkdir database
tianyangtax@iZ25tc8mln7Z:/mnt$ cd database
tianyangtax@iZ25tc8mln7Z:/mnt/database$ sudo mv /var/lib/mysql ./
tianyangtax@iZ25tc8mln7Z:/mnt/database$ ls -l
total 4
drwx------ 4 mysql mysql 4096 Jan 15 21:31 mysql

2.更改配置文件

tianyangtax@iZ25tc8mln7Z:/mnt/database$ sudo vim /etc/apparmor.d/usr.sbin.mysqld
tianyangtax@iZ25tc8mln7Z:/mnt/database$ sudo cat /etc/apparmor.d/usr.sbin.mysqld
...
  #edit by royakon for move datadir to /mnt on 20160115
  #/var/lib/mysql/ r,
  #/var/lib/mysql/** rwk,
  /mnt/database/mysql/ r,
  /mnt/database/mysql/** rwk,
  #end edit
...
tianyangtax@iZ25tc8mln7Z:/mnt/database$ sudo vim /etc/mysql/my.cnf
tianyangtax@iZ25tc8mln7Z:/mnt/database$ sudo cat /etc/mysql/my.cnf
...
datadir         = /mnt/database/mysql
...

3.重新启动MYSQL服务,并测试

tianyangtax@iZ25tc8mln7Z:/mnt/database$ sudo service mysql start
mysql start/running, process 1718
tianyangtax@iZ25tc8mln7Z:/mnt/database$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.46-0ubuntu0.14.04.2 (Ubuntu)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye