AppServer/

在 CentOS 上安装 MySQL 需要注意的一些事

发布
修订
未修订
字数
819
阅读
2 分钟

安装完 CentOS 之后:

bash
#更新一下 yum:
yum update -y

#如安装指定版本 MySQL,则按如下步骤:
sudo yum install -y wget
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo yum localinstall mysql80-community-release-el7-3.noarch.rpm
sudo yum install -y yum-utils
yum repolist enabled | grep "mysql.*-community.*"
#这个会显示当前可用的 MySQL 版本为 mysql80。
#列出所有版本:
yum repolist all | grep mysql
#禁用 mysql80:
sudo yum-config-manager --disable mysql80-community
#启用 mysql57
sudo yum-config-manager --enable mysql57-community
#再查询一下结果:
yum repolist enabled | grep mysql
#安装 mysql:
sudo yum install -y mysql-community-server
#查看新生成的初始密码:
sudo grep 'temporary password' /var/log/mysqld.log

然后使用 mysql -u root -p 和初始密码登录,修改密码:

bash
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New@password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'New@password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

安装完成后,默认情况下数据库目录是 /var/lib/mysql
有时候我们需要转移到其它目录,如: /data/db
操作如下:

bash
service mysqld stop
cp -rp /var/lib/mysql /data/db

然后修改 /etc/my.cnf 中关于 datadir 的值为 /data/db。
然后再重启动 MySQL,这时可能会报错,无法启动。
查看 SELinux 的状态:

bash
sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

如果把 SELinux 关掉(setenforce 0),又正常,则是 SELinux 的问题了。我们在新的数据库目录 /var/log/mysqld.log 中可以看到一些错误详情:

bash
[Warning] Can't create test file /data/db/server.lower-test
[Note] /usr/sbin/mysqld (mysqld 5.7.32) starting as process 26429 ...
[Warning] Can't create test file /data/db/oa-server.lower-test
[Warning] Can't create test file /data/db/oa-server.lower-test
[Note] InnoDB: PUNCH HOLE support available
[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
[Note] InnoDB: Uses event mutexes
[Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
[Note] InnoDB: Compressed tables use zlib 1.2.11
[Note] InnoDB: Using Linux native AIO
[Note] InnoDB: Number of pools: 1
[Note] InnoDB: Using CPU crc32 instructions
[Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
[Note] InnoDB: Completed initialization of buffer pool
[Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
[Note] InnoDB: Highest supported file format is Barracuda.
[Note] InnoDB: Creating shared tablespace for temporary tables
[ERROR] InnoDB: Operating system error number 13 in a file operation.
[ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
[ERROR] InnoDB: Operating system error number 13 in a file operation.
[ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
[ERROR] InnoDB: Cannot open datafile './ibtmp1'
[ERROR] InnoDB: Unable to create the shared innodb_temporary
[ERROR] InnoDB: Plugin initialization aborted with error Cannot open a file
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[ERROR] Failed to initialize builtin plugins.
[ERROR] Aborting

解决办法:
使用 semanage 对默认目录的安全上下文进行查询和修改。默认情况下没有安装 policycoreutils-python,用 yum 装一个就好了。

bash
yum -y install policycoreutils-python

先看一下 MySQL 相关的文件设置:

bash
semanage fcontext -l | grep -i mysql
#可以看到没有 /data/db 的设置。那么使用如下命令就可以了:
semanage fcontext -a -t mysqld_db_t "/data/db(/.*)?"
restorecon -Rv /data/db

然后我们再重启 MySQL 的服务,就会发现一切正常了。

评论

正在载入评论…

留下评论

人机验证
四位数字,看不清可点击图片刷新

评论经审核后显示。

回到顶部 ↑