前言:因公司数据库目录存储满了,无法加空间进行更换数据目录
Warning:更换数据目录之前先问一下有么有在用,或者又没用风险之类的,如果数据库有读写或者其他操作建议晚上进行操作。
1.停用MySQL数据库
[root@mysql ~]# systemctl stop mysqld
2.查看数据现存目录迁移至所需目录
[root@mysql ~]# cat /etc/my.cnf | grep datadir
datadir=/usr/local/mysql/data
[root@mysql ~]# cp -ar /usr/local/mysql/ /data/
3.修改MySQL的配置文件
[root@mysql ~]# vim /etc/my.cnf
#datadir=/usr/local/mysql/data
#socket=/usr/local/mysql/mysql.sock
datadir=/data/mysql/data
socket=/data/mysql/mysql.sock
4.将socket软链接至原有目录(否则会出现启动失败的情况)
[root@mysql ~]# ln -s /data/mysql/mysql.sock /usr/local/mysql/
5.启动MySQL服务
[root@mysql ~]# systemctl start mysqld
6.查看服务状态是否正常
[root@mysql ~]# systemctl status mysqld.service
● mysqld.service - MySQL
Loaded: loaded (/etc/init.d/mysql; enabled; vendor preset: disabled)
Active: active (running) since 二 2020-04-07 11:55:26 CST; 1min 10s ago
Process: 37229 ExecStop=/etc/init.d/mysql stop (code=exited, status=0/SUCCESS)
Process: 37311 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS)
Main PID: 37318 (mysqld_safe)
Tasks: 28
Memory: 151.5M
CGroup: /system.slice/mysqld.service
├─37318 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --...
└─37449 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data...
4月 07 11:55:25 mysql systemd[1]: Starting MySQL...
4月 07 11:55:26 mysql systemd[1]: Started MySQL.
[root@mysql ~]# ps -ef | grep mysqld
mysql 37318 1 0 11:55 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/usr/local/mysql/data/mysqld.pid
mysql 37449 37318 0 11:55 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysql/data/mysql.err --pid-file=/usr/local/mysql/data/mysqld.pid --socket=/data/mysql/mysql.sock
root 37551 36896 0 11:56 pts/1 00:00:00 grep --color=auto mysqld
[root@mysql ~]# netstat -anpt | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 37449/mysql
7.检查数据是否正常
[root@mysql ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| wordpress |
+--------------------+
6 rows in set (0.00 sec)