How to change the default directory location for the MySQL database

Source: Internet
Author: User
Tags socket mysql database

MySQL defaults to the data file storage directory for/var/lib/mysql. If you want to move the MySQL directory to/home/data, you need to do the following steps:

1, the home directory to establish the data directory

Cd/home

mkdir data

2, the MySQL service process to stop:

Mysqladmin-u root-p shutdown

3. Move/var/lib/mysql Entire directory to/home/data

mv/var/lib/mysql/home/data/

This will be the MySQL data file moved to the/home/data/mysql

4, find my.cnf configuration file

If there is no MY.CNF configuration file in the/etc/directory, please find *.cnf file under/usr/share/mysql/, copy one to/etc/and rename to my.cnf). The order is as follows:

[Root@test1 mysql]# cp/usr/share/mysql/my-medium.cnf/etc/my.cnf

5, edit the MySQL configuration file/etc/my.cnf

To ensure that MySQL works properly, you need to indicate where the Mysql.sock file will be generated. Modify the value on the right side of the equal sign in Socket=/var/lib/mysql/mysql.sock line:/home/mysql/mysql.sock. The operation is as follows:

VI my.cnf (with VI tool to edit MY.CNF file, find the following data modification)

# The MySQL server

[Mysqld]

Port = 3306

#socket =/var/lib/mysql/mysql.sock (original content, in order to be more secure with "#" comment on this row)

Socket =/home/data/mysql/mysql.sock (plus this row)

6, modify the MySQL startup script/etc/init.d/mysql

Finally, you need to modify the MySQL startup script/etc/init.d/mysql, datadir=/var/lib/mysql the path to the right of the equal sign in one line, and change it to your current actual storage path: Home/data/mysql.

[Root@test1 etc]# Vi/etc/init.d/mysql

#datadir =/var/lib/mysql (note this row)

Datadir=/home/data/mysql (plus this row)

7, restart the MySQL service

/etc/init.d/mysql start

or restart Linux with the reboot command

If the work is moving normally, it will be successful, otherwise check the previous 7 steps.

Also pay attention to the owner and permissions of the directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.