ubuntumysql
Stop MySQL using the following command:linux
sudo /etc/init.d/mysql stop
Copy the existing data directory (default located in /var/lib/mysql
) using the following command:sql
sudo cp -R -p /var/lib/mysql /newpath
edit the MySQL configuration file with the following command:ubuntu
sudo gedit /etc/mysql/my.cnf
Look for the entry for datadir
, and change the path (which should be /var/lib/mysql
) to the new data directory.centos
In the terminal, enter the command:app
sudo gedit /etc/apparmor.d/usr.sbin.mysqld
Look for lines beginning with /var/lib/mysql
. Change /var/lib/mysql
in the lines with the new path.socket
Save and close the file.tcp
Restart the AppArmor profiles with the command:this
sudo /etc/init.d/apparmor reload
Restart MySQL with the command:spa
sudo /etc/init.d/mysql restart
Now login to MySQL and you can access the same databases you had before.
來源: http://stackoverflow.com/questions/1795176/how-to-change-mysql-data-directory
centos:
Stopping the MySQL server
Create a new data directory and move the content from the old data directory
Correct the MySQL configuration file
Adjust SELinux parameters to accept our new change
Starting the MySQL server
# service mysqld stop
# mkdir /srv/mysql/
# chown mysql:mysql /srv/mysql
# mv /var/lib/mysql/* /srv/mysql/
Edit the my.cnf file for your distribution. In my example it’s located in the /etc/mysql/ directory. RHEL/CentOS/Scientific Linux put the my.cnf file directly in /etc by default.
# nano /etc/mysql/my.cnf
Change
datadir=/var/lib/mysql
to
datadir=/srv/mysql
and
socket=/var/lib/mysql/mysql.sock
to
socket=/srv/mysql/mysql.sock
and save the file.
Should the following command output 「Permissive」 or 「Disabled」 then you may skip the details for SELinux.
# getenforce
Run the semanage command to add a context mapping for /srv/mysql.
# semanage fcontext -a -t mysqld_db_t "/srv/mysql(/.*)?"
Now use the restorecon command to apply this context mapping to the running system.
# restorecon -Rv /srv/mysql
# service mysqld start
$ mysql -u root -p
mysql> show databases;
If this is working, you’re up and running. Should you get a message that says
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
then add the following to your /etc/my.cnf
[client] socket = /srv/mysql/mysql.sock
Optionally you can just use
$ mysql -u root -p --protocol tcp
to avoid connecting via the socket.
http://crashmag.net/change-the-default-mysql-data-directory-with-selinux-enabled