文章目录
此学习文是基于MySQL 8.0写的
得益于大神朋友的悉心指导解决不少坑,才写出此文,向大神奉上膝盖
数据量会随着时间的推移而增长,当它超出文件系统的容量时,需要添加磁盘或将数据目录移动到更大的卷中。
Apt(Debain、Ubuntu) 1. 检查当前的数据目录
mysql> show variables like '%datadir%';+---------------+-----------------+| Variable_name | Value |+---------------+-----------------+| datadir | /var/lib/mysql/ |+---------------+-----------------+1 row in set (0.00 sec)mysql> exitBye
2. 停止MySQL
# nangy @ nangy-vm in ~ [8:40:29] $ sudo service mysql stop
3. 检查状态
# nangy @ nangy-vm in ~/Downloads [8:40:47] $ sudo service mysql status● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: "inactive (dead)" since Fri 2019-08-09 08:40:47 CST; 5s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 4777 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS) Process: 4454 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 4777 (code=exited, status=0/SUCCESS) Status: "Server shutdown complete"8月 06 14:30:18 nangy-vm systemd[1]: Starting MySQL Community Server...8月 06 14:32:02 nangy-vm systemd[1]: Started MySQL Community Server.8月 09 08:40:36 nangy-vm systemd[1]: Stopping MySQL Community Server...8月 09 08:40:47 nangy-vm systemd[1]: Stopped MySQL Community Server.
4. 在创建新目录并将所有权更改到指定用户下:
# nangy @ nangy-vm in ~ [9:06:33] $ sudo mkdir -pv /data mkdir: 已创建目录 '/data'# 所有权更改# nangy @ nangy-vm in ~ [9:02:19] $ sudo chown -R mysql:mysql /data# 或者# 授权给非root用户(非必选)# nangy @ nangy-vm in ~ [9:02:19] $ sudo chmod 777 -R /data
5. 将文件移动到新的数据目录中
# nangy @ nangy-vm in ~ [9:09:58] $ sudo rsync -av /var/lib/mysql /data
6. 配置/etc/mysql/mysql.conf.d/mysqld.cnf
修改datadir的路径指向为/data/mysql,wq保存并退出
# nangy @ nangy-vm in ~ [9:07:31] $ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf [mysqld]pid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sock# datadir = /var/lib/mysqldatadir = /data/mysqllog-error = /var/log/mysql/error.logsecure-file-priv = /var/lib/mysql-files
7. 启动MySQL服务器并确认数据目录已更改
# nangy @ nangy-vm in ~ [9:16:38] $ sudo service mysql startmysql> show variables like '%datadir%';+---------------+--------------+| Variable_name | Value |+---------------+--------------+| datadir | /data/mysql/ |+---------------+--------------+1 row in set (0.44 sec)
8. 验证数据是否完整并删除旧数据目录
# nangy @ nangy-vm in ~ [9:18:23] $ sudo rm -rf /var/lib/mysql
Yum(Red Hat、CentOS)
详细步骤请参考Apt,Yum只列出与Apt有区别的部分步骤
2. 停止MySQL
[nangy@nangy-vm ~]$ sudo systemctl stop mysqld
3. 检查状态
[nangy@nangy-vm ~]$ sudo systemctl status mysqld
应显示stopped MySQL Community Server
6. 配置Access Control
[nangy@nangy-vm ~]$ sudo vim /etc/my.cnf[mysqld]# datadir=/var/lib/mysqldatadir=/data/mysqlsocket=/var/lib/mysql/mysql.socklog-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
7. 启动MySQL服务器并确认数据目录已更改
仅配置/etc/my.cnf依然会启动失败,解决方法请见坑!Yum-01
# 启动MySQL[nangy@nangy-vm ~]$ sudo systemctl start mysqld# 登录MySQL,查看数据目录mysql> show variables like '%datadir%';+---------------+--------------+| Variable_name | Value |+---------------+--------------+| datadir | /data/mysql/ |+---------------+--------------+1 row in set (0.44 sec)
如果 MySQL 无法启动并显示错误:MySQL data dir not found at /var/lib/mysql, Please Creat one,那么执行sudo mkdir /var/lib/mysql/。
如果没有找到 MySQL 系统数据库,运行mysql_install_db工具,它会创建所需的目录。
坑! 坑!Yum-01:启动失败 – 托管访问控制系统(SELinux)
报错原因:修改了数据目录
[nangy@nangy-vm ~]$ sudo systemctl start mysqldJob for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
查看一下状态,阿西吧,居然failed,不可接受,找解决办法
各种找度娘,还是大神多,找了几篇神文:SELinux之于MySQL 或者 一文彻底明白linux中的selinux到底是什么
安全增强型 Linux(Security-Enhanced Linux)简称SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。
SELinux 主要作用就是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。
在使用了 SELinux 的操作系统中,决定一个资源是否能被访问的因素除了某个资源是否拥有对应用户的权限(读、写、执行)之外,还需要判断每一类进程是否拥有对某一类资源的访问权限。
SELinux工作模式可以在 /etc/selinux/config 中设定。
如果想从 disabled 切换到 enforcing 或者 permissive的话,需要重启系统。反过来也一样。 enforcing 和 permissive 模式可以通过 setenforce 1|0 命令快速切换。
需要注意的是,如果系统已经在关闭SELinux的状态下运行了一段时间,在打开SELinux之后的第一次重启速度可能会比较慢。因为系统必须为磁盘中的文件创建安全上下文(我表示我重启了大约10分钟,还以为是死机了……)。
SELinux日志的记录需要借助 auditd.service 这个服务,请不要禁用它。
照着做喽!填坑呗
# 执行[root@nangy-vm nangy]$ setenforce 0# 查询状态[root@nangy-vm nangy]$ getenforcePermissive# 回到管理用户,启动MySQL[nangy@nangy-vm ~]$ sudo systemctl start mysqld# 查询状态,美滋滋,完美启动起来了[nangy@nangy-vm ~]$ sudo systemctl status mysqld● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: "active (running)" since 六 2019-08-17 09:11:28 CST; 5s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 14474 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 14511 (mysqld) Status: "Server is operational" Tasks: 39 CGroup: /system.slice/mysqld.service └─14511 /usr/sbin/mysqld8月 17 09:11:26 nangy-vm systemd[1]: Starting MySQL Server...8月 17 09:11:28 nangy-vm systemd[1]: Started MySQL Server.
[nangy@nangy-vm ~]$ sudo vim /etc/selinux/config# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.# SELINUX=enforcingSELINUX=permissive# SELINUXTYPE= can take one of three values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection.SELINUXTYPE=targeted
后记,这种操作可能产生安全隐患,但是本文是MySQL的学习文,所有操作都在个人电脑上,所以没有考虑过多的安全问题
坑!Yum-02:登录失败
报错原因:修改了数据目录
[nangy@nangy-vm ~]$ mysql -u root -pEnter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
继续找度娘,找到神文:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
[nangy@nangy-vm ~]$ mysql -h 127.0.0.1 -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 8Server version: 8.0.17 MySQL Community Server - GPL
原创文章,作者:筱凯,如若转载,请注明出处:https://www.jingyueyun.com/ask/464.html