CentOS 安装 MySQL8,超详细!

CentOS 安装 MySQL8,超详细!

一、离线安装MySQL

1. 检查是否已安装mysql

rpm -qa | grep mysql

2. 卸载已安装的mysql

rpm -ev mysql80-community-release-el7-7.noarch
rpm -ev mysql-community-client-plugins-8.0.22-1.ky3.kb1.x86_64

3. 下载rpm包:

下载地址:MySQL :: Download MySQL Community Server (Archived Versions)

注:版本需对应,如下述包都是8.0.32

mysql-community-client-8.0.32-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm
mysql-community-common-8.0.32-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.32-1.el7.x86_64.rpm
mysql-community-libs-8.0.32-1.el7.x86_64.rpm
mysql-community-server-8.0.32-1.el7.x86_64.rpm

4. 按依赖顺序安装

将下载好的rpm包拷至服务器,可按下列顺序安装,或按提示安装

rpm -ivh mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm 
rpm -ivh mysql-community-common-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.32-1.el7.x86_64.rpm

5. 重启mysql服务

systemctl start mysqld.service

6. 检查mysql服务运行状态

systemctl status mysqld.service

7. 停止mysql服务

systemctl stop mysqld.service

8. 重启mysql服务

systemctl restart mysqld.service

二、MySQL修改密码及登录

1. 获取初始密码

grep 'temporary password' /var/log/mysqld.log

2. 登录mysql

# -h ip地址
mysql -u root -p

输入上述命令获取的密码

3. 修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'PASSWD';

4. 查看mysql密码策略

SHOW VARIABLES LIKE 'validate_password%';

5. 设置密码安全等级为LOW

set global validate_password.policy=LOW;

6. 设置密码长度为6

set global validate_password.length=6;

7. 修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

8. 添加新用户

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

三、设置mysql允许远程登录

1. 改表法

use mysql;
select host, user from user;
update user set host = '%' where user = 'root';

2. 授权法(未验证)

use mysql;
select  User,authentication_string,Host from user;
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

3. 重启服务生效

systemctl restart mysqld.service

4. 注意

本教程为测试环境,出于安全性考虑,在生产运行环境下应做到:

  • 不应当给root用户开启远程登录权限。
  • 给用户赋权时,应当根据用户的需求,做到权限细分。如限定登录的ip地址,在grant命令中只开放select、update的权限等。

四、开启关闭服务器防火墙 

https://blog.csdn.net/qq_39997939/article/details/130900328

1. 查看服务器防火墙是否开启

# 如果显示running,则为开启状态
firewall-cmd --state

2. 关闭防火墙

systemctl stop firewalld.service

3. 开启防火墙

systemctl start firewalld.service

4. 关闭开机自启动

systemctl disable firewalld.service

5. 开启开机自启动

systemctl enable firewalld.service

6. 开启防火墙端口、重载防火墙使改动生效

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --query-port=8080/tcp

 

 

posted @ 2023-10-28 10:29  Janus2003  阅读(1422)  评论(0编辑  收藏  举报