90 likes | 101 Views
Sometimes you may need to change default MySQL directory. Here are the steps to change default data directory in MySQL.<br><br>Visit https://fedingo.com/how-to-change-default-mysql-data-directory-in-linux/
E N D
Create New Directory We will assume that our MySQL data will be moved to /home/ubuntu/mysql-data. So we will create a new directory for this purpose, using the following commands. We need to ensure that the owner of this directory is mysql:mysql, the default user of MySQL database. # mkdir /home/ubuntu/mysql-data # chown -R mysql:mysql /home/ubuntu/mysql-data
Identify Current MySQL Directory The default data directory of MySQL is located at /var/lib/mysql, it is better to check it using the following MySQL query. # mysql -u root -p -e "SELECT @@datadir;"
Copy MySQL Data Directory (½) You need to stop your MySQL service before you copy its data directory. Run the following commands to stop MySQL server. ------------- On SystemD ------------- # systemctl stop mariadb # systemctl is-active mariadb ------------- On SysVInit ------------- # service mysqld stop # service mysqld status OR # service mysql stop # service mysql status
Copy MySQL Data Directory (2/2) Run the following command to copy MySQL data directory to new location. # cp -R -p /var/lib/mysql/* /home/ubuntu/mysql-data
Update MySQL Configuration Run the following command to open it. # vi /etc/my.cnf OR # vi /etc/mysql/my.cnf Under [mysqld]: datadir=/home/ubuntu/mysql-data socket=/home/ubuntu/mysql-data/mysql.sock Under [client]: port=3306 socket=/home/ubuntu/mysql-data/mysql.sock
Set SELinux Security Context (For RHEL/CentOS) If you are running RHEL/CentOS system, then add SELinux security context to /home/ubuntu/mysql-data before restarting MySQL server. # semanage fcontext -a -t mysqld_db_t "/home/ubuntu/mysql-data(/.*)?" # restorecon -R /mnt/mysql-data
Restart MySQL Server ------------- On SystemD ------------- # systemctl stop mariadb # systemctl is-active mariadb ------------- On SysVInit ------------- # service mysqld stop # service mysqld status OR # service mysql stop # service mysql status
Thank You Visit for details https://fedingo.com/how-to-change-default-mysql-data-directory-in-linux/