「这是我参与2022首次更文挑战的第1天,活动详情查看:2022首次更文挑战」
安装步骤
只需要跟着下面每一步骤中的命令,在Linux系统中执行命令。如果在安装时遇到问题,看一下本文记录的常见错误是否遇到了相同错误,每一种错误给出了相应的解决方法。
下载MySQL的repo源
安装wget
yum -y install wget wget
进入usr目录创建mysql目录
## 进入usr
cd /usr/
## 创建mysql目录
mkdir mysql
## 进入mysql目录
cd mysql/
下载快照安装文件
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
安装MySQL
解析快照文件,安装rpm包
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
智能安装方式
yum install mysql-server
- 如果出现以下情况
Public key for mysql-community-client-5.7.40-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-client-5.7.40-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
这里需要禁掉GPG验证检查,执行如下命令重新安装
yum -y install mysql-community-server --nogpgcheck
启动MySQL服务
启动
service mysqld start
查看服务状态
systemctl status mysqld
修改临时密码
查看随机生成的密码
grep password /var/log/mysqld.log
使用root用户登录
mysql -u root -p
然后输入随机生成的密码回车
修改密码
密码要求需要有大小写字母、数字和特殊字符,不然不让你设置
alter user 'root'@'localhost' identified by 'Ran@123456';
图形化界面访问数据库
打开远程连接功能
## root用户登录
mysql -u root -p
## 切换到mysql数据库
use mysql;
## 修改user表的host字段值为通配符%
update user set host='%' where user='root';
## 刷新
flush privileges;
使用Navicat Premium连接虚拟机中MySQL数据库
常见错误
服务没有启动
- ERROR 2002 (HY000):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- 解决方法:
- 执行命令systemctl status mysqld查看mysql服务状态,需要启动
- 执行命令service mysqld start启动mysql服务
- 执行命令systemctl status mysqld查看mysql服务状态,需要启动
需要指定登录密码
- ERROR 1045 (28000):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- 解决方法:
- 在线安装MySQL会默认随机生成一个密码,需要使用命令查找随机生成的密码
grep password /var/log/mysqld.log
需要重置密码
- ERROR 1820 (HY000):
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
- 解决方法:
- 使用命令设置密码
alter user 'root'@'localhost' identified by 'Ran@123456';
需要打开远程连接权限
- 不支持远程连接错误码1130
1130 - Host 'gateway' is not allowed to connect to this MySQL server
-
解决方法:
- 查看mysql库中的user表
select user,host from user;
- 将user字段为root的host字段值修改为%
update user set host='%' where user='root';
需要关闭防火墙
- 61 "Connection refused"
2003 - Can't connect to MySQL server on '172.16.167.11' (61 "Connection refused")
- 解决方法:
- 执行命令systemctl status firewalld查看防火墙的状态,需要关闭防火墙
- 执行命令systemctl stop firewalld关闭防火墙
- 执行命令systemctl status firewalld查看防火墙的状态,需要关闭防火墙
安全防火墙添加规则
“2003 can’t connect to MySQL server on…(60 operation timed out)”
阿里云服务器-安全-防火墙-添加规则