wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
下载地址:MySQL :: Download MySQL Yum Repository
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
yum install mysql-server
linux安装mysql,显示尚未安装gpg密钥
mysql-community-*****.el7.x86_64.rpm 的公钥尚未安装
添加参数,不进行校验
yum install -y mysql-server --nogpgcheck
编辑文件vim /etc/my.cnf
在[mysqld]添加配置
#让MYSQL大小写敏感(1-不敏感,0-敏感)
lower_case_table_names=1
如果需要修改端口号,可以通过port进行设置
port=13306
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.
通过如下命令可以在日志文件中找出临时密码,进行登录。
# 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)
开放指定的mysql端口。
firewall-cmd --permanent --zone=public --add-port=13306/tcp
刷新配置
firewall-cmd --reload
在/etc/my.cnf文件中,在[mysqld]下面增加配置加密方式
default_authentication_plugin=mysql_native_password
修改完后重启mysql
# systemctl restart mysqld.service
登录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;
查看密码规则:
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 |
通过远程可以连接成功了。
如果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,之后就可以使用新设置的密码连接了。