How to change mysql default data directory in Ubuntu

本文介绍如何在Ubuntu系统中更改MySQL默认的数据目录位置。通过详细步骤指导如何复制数据目录、编辑配置文件并调整AppArmor设置来确保MySQL服务正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to change mysql data directory in Ubuntu

In this tutorial we will learn how to change the default path of mysql data directory.
The mysql server bydefault keep the data in /var/lib/mysql . If you want to change its path follow the given below steps.

Lets have an overview for changing mysql data directory method:

(1) Copy the new mysql directory to new location in server
(2) edit the my.cnf file and give new mysql data directory path
(3) edit /etc/apparmor.d/usr.sbin.mysqld file and add 2 lines for new mysql data directory.
(4) Restart the apparmor service
(5) Restart the mysql server service.

(6)这步不可少 

     mysql_install_db -user=mysql --basedir=/usr -datadir=/data/mysql --defaults-file=/etc/mysql/my.cnf

     mysql_install_db -user=mysql --basedir=/usr -datadir=/data/mysql --defaults-file=/etc/mysql/my-       default.cnf(如果没有复制一份)

For reference purpose I will copy the /var/lib/mysql to /var/lib/mysqlnew .

Note:
(a) Inside /var/lib/mysql . the new database will be in directory with same name. for eg. If my database name is linuxdb then same name of directory you will find i.e /var/lib/mysql/linuxdb . Hence same databases should be exist in new mysql data dir.

(b) Given below are the important files that should be present in new mysql data dir.
ibdata1,ib_logfileN,mysql_upgrade_info,debian-5.5.flag

-rw-r–r– 1 mysql mysql 0 May 1 14:54 debian-5.5.flag
-rw-rw—- 1 mysql mysql 27262976 May 18 12:58 ibdata1
-rw-rw—- 1 mysql mysql 5242880 May 18 12:58 ib_logfile0
-rw-rw—- 1 mysql mysql 5242880 Mar 5 11:07 ib_logfile1
-rw-rw—- 1 mysql mysql 6 May 1 14:55 mysql_upgrade_info

(c) The ownership and group of entire /var/lib/mysql is mysql:mysql . Just as u can see in above box.

Now follow the given below steps to change mysql data directory

Step1: Copy the /var/lib/mysql to new name /var/lib/mysqlnew

cp -prvf /var/lib/mysql /var/lib/mysqlnew

check the ownsership and group of new dir mysqlnew. It should mysql:mysql

root@sharad-sapplica:/var/lib# ls -ld mysqlnew/
drwx—— 7 mysql mysql 4096 May 18 13:00 mysqlnew/
root@sharad-sapplica:/var/lib# ls -la mysqlnew/
total 36896
drwx—— 7 mysql mysql 4096 May 18 13:00 .
drwxr-xr-x 83 root root 4096 May 18 12:56 ..
-rw-r–r– 1 mysql mysql 0 May 1 14:54 debian-5.5.flag
-rw-rw—- 1 mysql mysql 27262976 May 17 19:31 ibdata1
-rw-rw—- 1 mysql mysql 5242880 May 18 13:00 ib_logfile0
-rw-rw—- 1 mysql mysql 5242880 Mar 5 11:07 ib_logfile1
drwx—— 2 mysql mysql 4096 May 1 14:55 mysql
-rw-rw—- 1 mysql mysql 6 May 1 14:55 mysql_upgrade_info
drwx—— 2 mysql mysql 4096 May 1 14:55 performance_schema
drwx—— 2 mysql mysql 4096 Mar 5 11:07 test
root@sharad-sapplica:/var/lib#

Step 2: Now edit my.cnf file
and change the value of datadir with new path of mysql data directory.
Then save and exit

vi /etc/mysql/my.cnf

#datadir = /var/lib/mysql
datadir = /var/lib/mysqlnew

Note: After doing step 2, if you try to restart the mysql service it will not start and give some error in /var/log/syslog .
For reference below is the syslog details.

# tail -3 /var/log/syslog

May 18 12:58:19 mypc kernel: [ 7780.046897] type=1400 audit(1368862099.538:51): apparmor=”STATUS” operation=”profile_replace” name=”/usr/sbin/mysqld” pid=8171 comm=”apparmor_parser”
May 18 12:58:19 mypc kernel: [ 7780.065904] type=1400 audit(1368862099.558:52): apparmor=”DENIED” operation=”mknod” parent=1 profile=”/usr/sbin/mysqld” name=”/var/lib/mysqlnew/mypc.lower-test” pid=8175 comm=”mysqld” requested_mask=”c” denied_mask=”c” fsuid=0 ouid=0
May 18 12:58:19 mypc kernel: [ 7780.065975] type=1400 audit(1368862099.558:53): apparmor=”DENIED” operation=”mknod” parent=1 profile=”/usr/sbin/mysqld” name=”/var/lib/mysqlnew/mypc.lower-test” pid=8175 comm=”mysqld” requested_mask=”c” denied_mask=”c” fsuid=0 ouid=0
May 18 12:58:19 mypc kernel: [ 7780.069767] type=1400 audit(1368862099.562:54): apparmor=”DENIED” operation=”open” parent=1 profile=”/usr/sbin/mysqld” name=”/var/lib/mysqlnew/mysql/plugin.frm” pid=8175 comm=”mysqld” requested_mask=”r” denied_mask=”r” fsuid=121 ouid=121

Step 3: After reading the syslog we get to know apparmor is denying the new configuration for mysql. To rectify this issue we will edit the file /etc/apparmor.d/usr.sbin.mysqld. And paste the new path of mysql data directory.
And restart the apparmor service.

See the below method

# vi /etc/apparmor.d/usr.sbin.mysqld

/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
/var/lib/mysqlnew/ r,
/var/lib/mysqlnew/** rwk,

Note: I kept the 2 lines for /var/lib/mysql in file as it is.Only added new 2 lines.(see the Bolded 2 lines)

/etc/init.d/apparmor restart

Step 4: Now restart the mysql service .And try to connect the mysql-server.

#/etc/init.d/mysql restart

# mysql -u root -p

Note: Do not apply the steps directly in Live Production servers. First test the method in staging or test machines.

It depend upon the scenario on which you are working either you are copying the same data dir within same system or copying it from another server to new server.
The above given eg. is just for an reference.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值