Centos6.5 移动mysql存储目录
最近新上线了一数据抓取项目,由于数据库随着抓取,数据量渐渐增大,所以将数据库存储目录从根分区移到了/home分区,转移过程中发现一些问题,这里做以记录。
mysql是通过yum安装的,所以默认的配置文件为:/etc/my.cnf,默认的数据库存储目录为:/var/lib/mysql/,如下所示,安装方法不同的同学请自行查找自己的mysql配置文件。
[root@localhost ~]# vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
——————————低调的分割线—————————-
1,创建数据存储目录
[root@localhost ~]# mkdir /home/data/
2,停止mysql服务进程
[root@localhost ~]# service mysqld stop
3,转移数据库存储目录
为保险期间,先进行复制,成功后再删除
[root@localhost ~]# cp -av /var/lib/mysql /home/data/
4,修改数据库配置文件
[root@localhost ~]# vi /etc/my.cnf [mysqld] #datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock datadir=/home/data/mysql socket=/home/data/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
5,修改MySQL启动脚本/etc/init.d/mysqld,
打开/etc/init.d/mysqld,找到“get_mysql_option mysqld datadir”,注释:get_mysql_option mysqld datadir "/var/lib/mysql",添加get_mysql_option mysqld datadir "/home/data/mysql"。其实 /etc/my.cnf 中的 datadir=/var/lib/mysql 也可以不修改,因为这里才是启动mysql服务进程时读取的配置;
[root@localhost ~]# vi /etc/init.d/mysqld
#get_mysql_option mysqld datadir "/var/lib/mysql"
get_mysql_option mysqld datadir "/home/data/mysql"
datadir="$result"
get_mysql_option mysqld socket "$datadir/mysql.sock"
socketfile="$result"
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"
errlogfile="$result"
get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid"
mypidfile="$result"
6,修改目录所属以及权限
[root@localhost ~]# chown -R mysql:mysql /home/data/mysql/ ← 改变数据库的归属为mysql [root@localhost ~]# chmod 700 /home/data/mysql/test/ ← 改变数据库目录属性为700 [root@localhost ~]# chmod 660 /home/data/mysql/test/* ← 改变数据库中数据的属性为660
7,修改php配置文件php.ini
(1)php版本
[root@localhost ~]# php -v PHP 7.0.15 (cli) (built: Jan 19 2017 21:55:34) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
(2)修改php默认链接的socket
找到pdo_mysql.default_socket,默认情况下为空,连接的是“/var/lib/mysql/mysql.sock”,所以需要修改为“/home/data/mysql/mysql.sock”
如下所示:
[root@localhost ~]# vi /etc/php.ini ; -----省略---- ; Default socket name for local MySQL connects. If empty, uses the built-in ; MySQL defaults. ; http://php.net/pdo_mysql.default-socket pdo_mysql.default_socket="/home/data/mysql/mysql.sock" ; -----省略---- mysql.default_socket ="/home/data/mysql/mysql.sock" ; -----省略---- mysqli.default_socket ="/home/data/mysql/mysql.sock"
共有三处:
-
pdo_mysql.default_socket="/home/data/mysql/mysql.sock"
-
mysql.default_socket ="/home/data/mysql/mysql.sock"
-
mysqli.default_socket ="/home/data/mysql/mysql.sock"
不修改的后果:数据库启动正常 http启动正常 但访问网站时会出现如下错误:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)
8,重启mysql,删除原存储目录
[root@localhost ~]# service mysqld restart [root@localhost ~]# rm -rf /var/lib/mysql
9,访问网站 查看效果,没问题了恭喜你,有问题就留言吧!