centos 7.9 安装配置mysql5.7

系统配置
【centos 7.9 安装配置mysql5.7】# 禁用防火墙与SElinuxsystemctl stop firewalld && systemctl disable firewalldsetenforce 0sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config# 开启防火墙启用MySQL 3306端口firewall-cmd --zone=public --add-port=3306/tcp --permanent# 重载配置firewall-cmd --reload# 查看端口号是否开启(返回yes开启)firewall-cmd --query-port=3306/tcp# 获取防火墙列表firewall-cmd --list-all# 磁盘分区(数据库数据盘)mkfs.xfs /dev/sdbmkdir /datamount /dev/sdb /data# 设置自动挂载# 获取磁盘UUID[root@mysql8 ~]# blkid | grep sdb/dev/sdb: UUID="dc71382a-b53a-40ff-a0fe-2c0422105ddd" TYPE="xfs"# 追加至/etc/fstabecho "UUID=dc71382a-b53a-40ff-a0fe-2c0422105ddd/dataxfsdefaults0 0" >> /etc/fstab2.配置安装源
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm3.安装mysql5.7
yum install mysql-community-server# 启动服务&设置开机自启动systemctl start mysqld && systemctl enable mysqld && systemctl status mysqld初始化数据库4.初始化数据库
# 获取数据库临时密码grep 'A temporary password' /var/log/mysqld.log | tail -1# 初始化数据库mysql_secure_installationSecuring the MySQL server deployment.Enter password for user root: **********The 'validate_password' plugin is installed on the server.The subsequent steps will run with the existing configurationof the plugin.Using existing password for root.Estimated strength of the password: 100# 修改root管理员密码Change the password for root ? ((Press y|Y for Yes, any other key for No) : yNew password: ******************Re-enter new password: ******************Estimated strength of the password: 100Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : yBy default, a MySQL installation has an anonymous user,allowing anyone to log into MySQL without having to havea user account created for them. This is intended only fortesting, and to make the installation go a bit smoother.You should remove them before moving into a productionenvironment.# 删除匿名用户Remove anonymous users? (Press y|Y for Yes, any other key for No) : ySuccess.Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe root password from the.NETwork.# 禁止root账号远程登录Disallow root login remotely? (Press y|Y for Yes, any other key for No) : ySuccess.By default, MySQL comes with a database named 'test' thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.# 删除测试库Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database...Success. - Removing privileges on test database...Success.Reloading the privilege tables will ensure that all changesmade so far will take effect immediately.# 重新加载特权表 , 生效配置Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ySuccess.All done!修改数据库存放位置数据库存放在系统盘始终是不安全的 , 所以一般我们会配置一个数据盘 , 用于单独存放数据库文件 , 在前面的【系统通用配置】中 , 我们已经挂载了磁盘/dev/sdb至/data , 这个就是专用用于数据库数据存储的 , 详细MySQL修改方法如下:
# 创建数据库存放目录mkdir /data/mysql# 配置权限chown -vR mysql:mysql /data/mysql/chmod -vR 700 /data/mysql/# 停止数据库服务systemctl stop mysqld# 复制原数据库至新目录cp -av /var/lib/mysql/* /data/mysql/# 修改/etc/my.cnf配置 , 如下所示:[root@localhost ~]# cat /etc/my.cnf | grep -v "^#" | grep -v "^$"[mysqld]datadir=/data/mysqlsocket=/data/mysql/mysql.socksymbolic-links=0log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid[client]port=3306socket=/data/mysql/mysql.sock[mysqld_safe]socket=/data/mysql/mysql.socknice=0# 启动数据库服务systemctl start mysqld && systemctl status mysqld# 确保数据库启动成功以后 , 我们就可以删除不再需要的原数据库文件了rm -rf /var/lib/mysql设置允许远程连接远程连接可以设置root账号 , 也可以单独创建一个账号 , 专门用于远程管理 , 毕竟root账号的权限过大 , 一旦密码泄漏严重影响数据库安全 。
# 登录MySQL[root@localhost ~]# mysql -u root -p# 进入mysql库mysql> use mysql;# 更新域属性 , '%'表示允许任何主机访问mysql> update user set host="%" where user ="root";# 全局特权所有数据库mysql> grant all privileges on *.* to 'root'@'%' with grant option;# 生效配置mysql> FLUSH PRIVILEGES;


推荐阅读