## MySQL 迁移数据文件存储路径
操作系统:centos
软件:MySQL,PHP
### 停止MySQL服务
```
[root@centos]# systemctl stop mysqld.service
```
### 查看MySQL数据文件所属用户和组
MySQL数据文件默认是属于mysql组和mysql用户的
```
[root@centos]# ls -l /var/lib
total 120
……
drwxr-x--x 11 mysql mysql 4096 Dec 20 09:38 mysql_bak
……
```
### 迁移文件
把MySQL数据文件从`/var/lib/mysql`迁移到 `/data/server/mysql`
```
[root@centos]# mv /var/lib/mysql /data/server/
```
### 修改新目录所属用户和组
把新目录的所属用户和组修改为和之前一样
```
[root@centos]# chown -R mysql:mysql /data/server/mysql
```
### 修改MySQL配置
修改`mysqld`,`client`,`mysql`节的配置,修改完毕后启动`mysqld`服务
```
[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
##上面两行改为下面两行
datadir=/data/server/mysql
socket=/data/server/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#下面的行是新增的
[client]
default-character-set=utf8
socket=/data/server/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/data/server/mysql/mysql.sock
```
### 启动mysqld服务
```
[root@centos]# systemctl start mysqld.service
```
### 修改PHP配置文件
修改`php.ini`中`mysqli.default_socket`和`pdo_mysql.default_socket`,修改后要重启php-fpm服务
```
mysqli.default_socket = /data/server/mysql/mysql.sock
pdo_mysql.default_socket = /data/server/mysql/mysql.sock
```
### 查看phpinfo
![](https://img.kancloud.cn/c0/c8/c0c8d971155e7e06c95d77d637ac23a1_942x544.png)
![](https://img.kancloud.cn/ac/30/ac30ae496b858772be59ef70355634e8_941x166.png)
### 重启php-fpm服务
```
[root@centos]# systemctl restart php-fpm.service
```
### 测试是否成功
```
[root@centos]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14310
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
```
### 测试PHP是否成功连接MySQL