系统环境:CentOS7.9
官网下载yum仓库的包
[root@localhost ~]# wget https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
[root@localhost ~]# yum -y install mysql80-community-release-el7-7.noarch.rpm
进入仓库查看mysqlyum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
![](https://img-blog.csdnimg.cn/img_convert/f1f46d1feac545ac906684d2966b293f.png)
##这三个就是官方mysql yum源,进入mysql-community.repo查看版本再此也可以选择版本默认是mysql8.0版本,如果要选择mysql5.7版本就把mysql8.0的enabled=1改成0,mysql5.7的enabled=0改成1然后yum makecache
[root@localhost yum.repos.d]# vim mysql-community.repo
![](https://img-blog.csdnimg.cn/img_convert/f229143ce3934187b9b5d832ccfae463.png)
列出所有可安装mysql的软件包
[root@localhost ~]# yum list | grep mysql
![](https://img-blog.csdnimg.cn/img_convert/1781b3410f344c328628cb98ac94797d.png)
##安装这个版本
[root@localhost ~]# yum -y install mysql-community-server.x86_64
[root@localhost ~]# systemctl enable mysqld --now
[root@localhost ~]# netstat -anptu | grep mysql
tcp6 0 0 :::33060 :::* LISTEN 2866/mysqld
tcp6 0 0 :::3306 :::* LISTEN 2866/mysqld
查看mysql默认密码
[root@localhost ~]# grep password /var/log/mysqld.log
![](https://img-blog.csdnimg.cn/img_convert/88663f68959d448baf84762a44214859.png)
##这是mysql安装后的默认密码,尝试登录
[root@localhost ~]# mysql -uroot -p'u?j#so_W/7+('
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.32 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
##登录成功
修改mysql默认密码
[root@localhost ~]# vim /etc/my.cnf
skip-grant-tables ##加入这行意思是跳过权限验证的问题
[root@localhost ~]# systemctl restart mysqld
现在可以不要密码登录mysql
[root@localhost ~]# msyql -uroot
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> use mysql;
mysql> update user set authentication_string='' where user = 'root'; ##把root密码设置为空
mysql> flush privileges; ##刷新权限
登出mysql进入mysql配置文件把skip-grant-tables加注释
[root@localhost ~]# vim /etc/my.cnf
#skip-grant-tables
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -uroot
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ABc123...';
##注意mysql密码必须含有大小写字母,数字,特殊符号
尝试登录mysql
[root@localhost ~]# mysql -uroot -p'ABc123...'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.32 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
##登录成功