This is a short article that explains how you change the default MySQL data directory and adjust SELinux to account for the changes. The article assumes that you’re running either RHEL, CentOS, Scientific Linux or Fedora with SELinux enabled. This works with the most recent EL (6.2) version.
We’ll be doing this in the following order.
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
Stopping the MySQL server
# service mysqld stop
Create a new data diretory and move the content from the old one
Creating a new data directory
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=/data/mysql
and
socket=/var/lib/mysql/mysql.sock
to
socket=/data/mysql/mysql.sock
and save the file.
Adjust SELinux parameters to accept our new change
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 /data/mysql.
# semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
Now use the restorecon command to apply this context mapping to the running system.
# restorecon -Rv /data/mysql
Starting the MySQL server
# service mysqld start
Verifying access and connectivity
$ 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’
Move the default MySQL data directory with SELinux/Centos/RHEL 6.2/Fedora 16
This is a short article that explains how you change the default MySQL data directory and adjust SELinux to account for the changes. The article assumes that you’re running either RHEL, CentOS, Scientific Linux or Fedora with SELinux enabled. This works with the most recent EL (6.2) version.
We’ll be doing this in the following order.
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
Stopping the MySQL server
Create a new data diretory and move the content from the old one
Creating a new data directory
Moving the original data files
Ning: I going use cp for safe
Correct the MySQL configuration file
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.
Change
to
and
to
and save the file.
Adjust SELinux parameters to accept our new change
Should the following command output “Permissive” or “Disabled” then you may skip the details for SELinux.
Run the semanage command to add a context mapping for /data/mysql.
Now use the restorecon command to apply this context mapping to the running system.
Starting the MySQL server
Verifying access and connectivity
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
Optionally you can just use
to avoid connecting via the socket.
Image Gallery
Recent Posts
Categories
Meta