Linux MySQL数据库冷迁移采坑记录

Linux MySQL数据库冷迁移采坑记录

当前文件系统已满,需要将MySQL默认路径迁移到新文件系统下。
MySQL数据库文件原位置:/var/lib/mysql 。
要移动至:/mysql/mysql 路径下。

1.停止MySQL服务

MySQL服务进程停止:
#mysqladmin -u root -p shutdown
查看mysql服务是否已经停止
#netstat -an |grep 3306

2.复制物理数据文件

把/var/lib/mysql整个目录移到/home/data
#cp -R /var/lib/mysql /mysql/mysql/
#chown -R mysql:mysql /data/mysql
#chmod -R 755 /data/mysql

把MySQL的数据文件移动到了/mysql/mysql下

3.修改my.cnf配置文件

#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock

将原路径注释,修改新路径如下:
datadir=/mysql/mysql
socket=/mysql/mysql/mysql.sock

4.启动MySQL报错

启动MySQL服务报错如下:

[root@localhost mysql]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

查看MySQL服务状态如下:

[root@localhost mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Mon 2021-04-19 00:06:13 PDT; 1s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 19072 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 19030 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 17159 (code=exited, status=0/SUCCESS)

查看MySQL日志信息如下:

# tail -40 mysqld.log 

2021-04-19T07:06:12.605508Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2021-04-19T07:06:12.605613Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2021-04-19T07:06:12.605627Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2021-04-19T07:06:12.605633Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2021-04-19T07:06:12.605637Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2021-04-19T07:06:12.605642Z 0 [ERROR] InnoDB: Cannot open datafile './ibtmp1'

ERROR信息,关键错误 :InnoDB: Operating system error number 13 in a file operation.

没有访问权限,不能写入,检查目录授权都正确,很是疑惑???

5.解决InnoDB: Operating system error number 13 in a file operation问题

遍访百度,需要关闭selinux :
先临时关闭,再测试一下:

[root@localhost /]# getenforce
Enforcing
[root@localhost /]# setenforce 0
[root@localhost /]# getenforce 
Permissive
[root@localhost /]# systemctl start mysqld

启动MySQL成功!!!

[root@localhost mysql]# netstat -nlp |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      19759/mysqld        
[root@localhost mysql]#

登录数据库,提示如下:

[root@localhost /]# mysql -uroot -p
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

这个问题解决方法就是建立一个软连接:

ln -s /mysql/mysql/mysql.sock /var/lib/mysql/mysql.sock

登录MySQL后,在MySQL数据库中检查数据路径:

mysql> show global variables like "%datadir%";
+---------------+---------------+
| Variable_name | Value         |
+---------------+---------------+
| datadir       | /mysql/mysql/ |
+---------------+---------------+
1 row in set (0.02 sec)

6.永久关闭selinux

SELINUX=disabled

[root@localhost mysql]# cat /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=enforcing
SELINUX=disabled 
#SELINUXTYPE= can take one of three two values:
#targeted - Targeted processes are protected,
#minimum - Modification of targeted policy. Only selected processes are protected. 
#mls - Multi Level Security protection.
SELINUXTYPE=targeted 

7.SELinux 参数说明,来自网络:

  • SELinux 的工作模式

SELinux 提供了 3 种工作模式:Disabled、Permissive 和 Enforcing,而每种模式都为 Linux 系统安全提供了不同的好处。

  • Disable工作模式(关闭模式)

在 Disable 模式中,SELinux 被关闭,默认的 DAC 访问控制方式被使用。对于那些不需要增强安全性的环境来说,该模式是非常有用的。

例如,若从你的角度看正在运行的应用程序工作正常,但是却产生了大量的 SELinux AVC 拒绝消息,最终可能会填满日志文件,从而导致系统无法使用。在这种情况下,最直接的解决方法就是禁用 SELinux,当然,你也可以在应用程序所访问的文件上设置正确的安全上下文。

需要注意的是,在禁用 SELinux 之前,需要考虑一下是否可能会在系统上再次使用 SELinux,如果决定以后将其设置为 Enforcing 或 Permissive,那么当下次重启系统时,系统将会通过一个自动 SELinux 文件重新进程标记。

关闭 SELinux 的方式也很简单,只需编辑配置文件 /etc/selinux/config,并将文本中 SELINUX= 更改为 SELINUX=disabled 即可,重启系统后,SELinux 就被禁用了。

  • Permissive工作模式(宽容模式)

在 Permissive 模式中,SELinux 被启用,但安全策略规则并没有被强制执行。当安全策略规则应该拒绝访问时,访问仍然被允许。然而,此时会向日志文件发送一条消息,表示该访问应该被拒绝。

  • SELinux Permissive 模式主要用于以下几种情况:

审核当前的 SELinux 策略规则;
测试新应用程序,看看将 SELinux 策略规则应用到这些程序时会有什么效果;
解决某一特定服务或应用程序在 SELinux 下不再正常工作的故障。

某些情况下,可使用 audit2allow 命令来读取 SELinux 审核日志并生成新的 SELinux 规则,从而有选择性地允许被拒绝的行为,而这也是一种在不禁用 SELinux 的情况下,让应用程序在 Linux 系统上工作的快速方法。

Enforcing工作模式(强制模式)
从此模式的名称就可以看出,在 Enforcing 模式中, SELinux 被启动,并强制执行所有的安全策略规则。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值