Linux (centos7)安装 MySQL 8 数据库并配置远程访问

1. mysql安装

1.1 下载并安装 MySQL 官方的 Yum Repository

wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm

 下载地址:MySQL :: Download MySQL Yum Repository

Linux (centos7)安装 MySQL 8 数据库并配置远程访问_第1张图片

1.2 然后进行repo的安装:

rpm -ivh mysql80-community-release-el7-1.noarch.rpm

1.3 执行安装

 yum install mysql-server

linux安装mysql,显示尚未安装gpg密钥

mysql-community-*****.el7.x86_64.rpm 的公钥尚未安装

添加参数,不进行校验
yum install -y mysql-server --nogpgcheck

2. mysql配置

2.1 设置不区分大小写

编辑文件vim /etc/my.cnf

在[mysqld]添加配置

#让MYSQL大小写敏感(1-不敏感,0-敏感)
lower_case_table_names=1

如果需要修改端口号,可以通过port进行设置

port=13306

2.2 启动MySQL

systemctl start mysqld.service 

查看状态

# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2022-04-24 16:52:37 CST; 16min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 19315 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 19355 (mysqld)
   Status: "Server is operational"
    Tasks: 38
   Memory: 367.6M
   CGroup: /system.slice/mysqld.service
           └─19355 /usr/sbin/mysqld

4月 24 16:52:35 k8s-node85 systemd[1]: Starting MySQL Server...
4月 24 16:52:37 k8s-node85 systemd[1]: Started MySQL Server.

2.3 设置密码

通过如下命令可以在日志文件中找出临时密码,进行登录。

# grep "password" /var/log/mysqld.log
2022-04-24T08:46:26.265059Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 4L>*******M

登录mysql,输入刚才的密码:

# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28
mysql> 

此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

具体密码根据需要设置。密码设置成功后,就可以执行命令了。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

3. 配置远程访问

3.1 防火墙开放端口

开放指定的mysql端口。

firewall-cmd --permanent --zone=public --add-port=13306/tcp

刷新配置

firewall-cmd --reload

3.2 修改密码加密方式

在/etc/my.cnf文件中,在[mysqld]下面增加配置加密方式

default_authentication_plugin=mysql_native_password

修改完后重启mysql

# systemctl restart mysqld.service

3.3 添加访问授权

登录mysql

# mysql -uroot -P13306 -p
Enter password: 

mysql>

查看授权

use mysql;
select host, user, authentication_string, plugin from user;

可以看到用户对应的主机是localhost,而不是%,所以不能远程连接。

update user set host='%' where user = 'root';
flush privileges;

3.4 新的密码加密规则导致的远程不能连接

查看密码规则:

mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             | $A$005$p!ksW.9jhhW4Mlg1tWuuolC8DK****** | caching_sha2_password |

修改密码规则:

mysql> ALTER USER 'root' IDENTIFIED BY 'new password' PASSWORD EXPIRE NEVER;            
Query OK, 0 rows affected (0.11 sec)

mysql> ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'new password';
Query OK, 0 rows affected (0.19 sec)

mysql> FLUSH PRIVILEGES;

可以看到密码规则修改过来了。

mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             | *E6E28AD89BDE948F84*********                              | mysql_native_password |

通过远程可以连接成功了。

Linux (centos7)安装 MySQL 8 数据库并配置远程访问_第2张图片

4. mysql8 root密码忘记重置方法

如果root密码忘记了,此时可以通过重置的方法进行重新设置。

编辑/etc/my.cnf,在[mysqld]中添加 

skip-grant-tables

重启mysql

systemctl restart mysqld.service

采用mysql客户端进行连接访问,此时不需要密码。

mysql -uroot

mysql> use mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
mysql> flush privileges;
mysql> quit

然后,打开/etc/my.cnf文件,注释掉刚才添加的skip-grant-tables,然后重启mysql,之后就可以使用新设置的密码连接了。

你可能感兴趣的:(数据库,linux,mysql8)