将 mariaDb 数据库移动到主文件夹

将 mariaDb 数据库移动到主文件夹

安装后,我试图移动 Fedora 上的数据库位置:

# su
# systemtcl stop mariadb
# cp -rp /var/lib/mysql /home
# chown mysql.mysql /home/mysql

接下来,我正在编辑文件 /etc/my.cnf.d/mariadb-server.cnf

[mysqld]
datadir=/var/lib/mysql/
socket=/var/lib/mysql.sock

[mysqld]
datadir=/home/mysql/
socket=/home/mysql.sock

然后

# systemctl start mariadb
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

所以

systemctl status mariadb
● mariadb.service - MariaDB 10.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2016-06-29 17:51:18 CEST; 29s ago
  Process: 6488 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)
  Process: 3103 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 6304 ExecStartPost=/usr/libexec/mysql-wait-ready $MAINPID (code=exited, status=1/FAILURE)
  Process: 6303 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=1/FAILURE)
  Process: 6259 ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n (code=exited, status=0/SUCCESS)
  Process: 6231 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 6303 (code=exited, status=1/FAILURE)

Jun 29 17:51:17 123PUB-PC systemd[1]: Starting MariaDB 10.0 database server...
Jun 29 17:51:17 123PUB-PC mysqld_safe[6303]: 160629 17:51:17 mysqld_safe Logging to '/var/log/maria...g'.
Jun 29 17:51:17 123PUB-PC mysqld_safe[6303]: mkdir: impossible de créer le répertoire « /home/mysql…xiste
Jun 29 17:51:17 123PUB-PC mysqld_safe[6303]: Fatal error Can't create database directory '/home/mys...ck'
Jun 29 17:51:17 123PUB-PC systemd[1]: mariadb.service: Main process exited, code=exited, status=1/FAILURE
Jun 29 17:51:18 123PUB-PC systemd[1]: mariadb.service: Control process exited, code=exited status=1
Jun 29 17:51:18 123PUB-PC systemd[1]: Failed to start MariaDB 10.0 database server.
Jun 29 17:51:18 123PUB-PC systemd[1]: mariadb.service: Unit entered failed state.
Jun 29 17:51:18 123PUB-PC systemd[1]: mariadb.service: Failed with result 'exit-code'.
Hint: Some lines were ellipsized, use -l to show in full.

因此看起来 mysql 对 home/mysql 没有写权限,但是:

# ls -la /home/mysql
drwxr-xr-x. 4 mysql mysql     4096 Jun 29 17:38 .
drwxr-xr-x. 5 root  root      4096 Jun 29 17:54 ..
-rw-rw----. 1 mysql mysql    16384 Jun 29 17:38 aria_log.00000001
-rw-rw----. 1 mysql mysql       52 Jun 29 17:38 aria_log_control
-rw-rw----. 1 mysql mysql 12582912 Jun 29 17:38 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Jun 29 17:38 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Jun 29 17:05 ib_logfile1
-rw-rw----. 1 mysql mysql        0 Jun 29 17:05 multi-master.info
drwx------. 2 mysql mysql     4096 Jun 29 17:05 mysql
-rw-r--r--. 1 mysql mysql       16 Jun 29 17:05 mysql_upgrade_info
drwx------. 2 mysql mysql     4096 Jun 29 17:05 performance_schema

我没有看到它与主目录有任何区别。

那么接下来呢?

答案1

这是 SELinux 的问题。但是为了确保测试时能禁用强制功能,请放心。

测试

在你的服务器中执行此操作(仅限 root 用户)以暂时禁用 SELinux

# setenforce 0

现在你可以启动服务器了

# systemctl start mariadb

使用 mysql 客户端连接,但首先修改文件上的配置/etc/my.cnf.d/client.cnf以使用新的套接字文件。

#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]
socket=/home/mysql/mysql.sock

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]

如果一切正常,那么您将能够连接到新找到的数据库。

停止服务器

# systemctl stop mariadb

使用 SELinux 配置的永久解决方案

SELinux 阻止 mysqld 在默认目录之外的任何目录中工作(读取、写入、访问)。
要解决此问题,您需要修改此行为,以便 SELinux 授予对新位置的权限。

获取上下文。列表可能有所不同,但应该显示mysqld_db_t语境。

# ls -lZ /var/lib/mysql
drwx------. mysql mysql unconfined_u:object_r:mysqld_db_t:s0 mysql

添加获得的上下文(mysqld_db_t) 到 的映射/home/mysql

# semanage fcontext -a -t mysqld_db_t "/home/mysql(/.*)?"

应用上下文

# restorecon -R -v /home/mysql

检查上下文是否适用

# grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local
/home/mysql(/.*)?    system_u:object_r:mysqld_db_t:s0


# ls -lZ /home/mysql
drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql

现在一切都已准备就绪。您应该可以毫无问题地重新执行 SELinux 并启动您的服务器。

# setenforce 1

# systemctl start mariadb

相关内容