MySQL & MariaDB
1 前言
MySQL相信不用笔者多做介绍,本章将要使用yum部署MySQL 8。
2 最佳实践
2.1 系统环境
OS = CentOS 7.3 x86_64
IP = any
Host Name = any.cmdschool.org
2.2 安装前准备
2.2.1 配置安装源
vim /etc/yum.repos.d/mysql-8.0-community-rhel7.repo
加入如下配置,
[mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql
2.2.2 安装常用工具
yum install -y vim
2.3 部署软件包
yum install -y mysql-community-server mysql-community-client
2.4 配置MySQL服务
2.4.1 启动服务并设置自启动
systemctl start mysqld.service systemctl enable mysqld.service
另外,我们建议使用如下命令确实服务已经启动,
systemctl status mysqld.service
2.4.2 关闭密码复杂度要求
cp /etc/my.cnf /etc/my.cnf.default vim /etc/my.cnf
加入如下配置
[mysqld] # Disable password complexity check validate_password.policy=low validate_password.length=4
设置完成后,你需要重启数据库服务使配置生效,
systemctl restart mysqld.service
2.4.3 查询初始密码
grep "temporary password" /var/log/mysqld.log
可见如下显示,
2020-06-10T08:28:45.672805Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ouB!Cg4cmuFd
2.4.4 运行初始化向导
mysql_secure_installation
向导如下,
[...] Enter password for user root:****** [...] New password:****** Re-enter new password:****** [...] Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y [...] Remove anonymous users? (Press y|Y for Yes, any other key for No) : y [...] Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n [...] Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y [...] Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y [...]
2.4.5 登录测试
mysql -uroot -p
2.5 迁移数据目录
2.5.1 登录测试
systemctl stop mysqld.service
2.5.2 迁移数据到新的存储目录
mv /var/lib/mysql /data/
mkdir /var/lib/mysql
2.5.3 修改配置声明存储目录位置
vim /etc/my.cnf
修改如下配置,
[mysqld] # datadir=/var/lib/mysql datadir=/data/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid validate_password.policy=low validate_password.length=4
根据配置重建“socket”参数所需的目录,
mkdir /var/lib/mysql chown mysql:mysql /var/lib/mysql
2.5.4 关闭SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0
2.5.5 尝试启动服务
systemctl start mysqld.service
2.5 授予应用程序访问前线
mysql -uroot -p create user 'user1'@'localhost' identified with mysql_native_password by 'password'; grant all privileges on db1.* to 'user1'@'localhost'; flush privileges;
注:由于MySQL8已经将身份认证插件由“mysql_native_password”修改为“caching_sha2_password”,故而需要使用特别的函数生成密码。
没有评论