1. 下载mysql安装包
mysql5.7.34百度网盘,提取码:2f6m
下载mysql的安装包MySQL官网下载地址,如下图所示
2. 安装mysql
因为CentOS7自带Mariadb,所以要先将Mariadb删除掉才能安装mysql
# 找到mariadb
rpm -qa | grep mariadb
#找到对应的包后删除
rpm -e --nodeps mariadb-libs-XXX
3将下载好的msyql安装包上传到服务器或虚拟机,解压
tar -xf mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar
# 解压后的所需要的rpm包就都有了,如下所示
# mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar
# mysql-community-client-5.7.34-1.el7.x86_64.rpm
# mysql-community-common-5.7.34-1.el7.x86_64.rpm
# mysql-community-devel-5.7.34-1.el7.x86_64.rpm
# mysql-community-embedded-5.7.34-1.el7.x86_64.rpm
# mysql-community-embedded-compat-5.7.34-1.el7.x86_64.rpm
# mysql-community-embedded-devel-5.7.34-1.el7.x86_64.rpm
# mysql-community-libs-5.7.34-1.el7.x86_64.rpm
# mysql-community-libs-compat-5.7.34-1.el7.x86_64.rpm
# mysql-community-server-5.7.34-1.el7.x86_64.rpm
# mysql-community-test-5.7.34-1.el7.x86_64.rpm
安装rpm包,安装顺序为common,libs,devel,libs-compat,client,server
rpm -ivh mysql-community-common-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm
安装mysql-community-server-5.7.34-1.el7.x86_64.rpm
时候报错
[root@localhost opt]# rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.34-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
/usr/bin/perl is needed by mysql-community-server-5.7.34-1.el7.x86_64
perl(Getopt::Long) is needed by mysql-community-server-5.7.34-1.el7.x86_64
perl(strict) is needed by mysql-community-server-5.7.34-1.el7.x86_64
缺少perl依赖,通过yum进行安装
yum list | grep perl # 查找perl
yum install -y perl.x86_64 # 安装perl
安装完成perl后再进行安装mysql-community-server-5.7.34-1.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.34-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-5.7.34-1.e################################# [100%]
# 安装成功
到这里提示一下安装mysql-community-server-5.7.34-1.el7.x86_64.rpm
的时候提示缺少什么就通过yum或者其他方式安装什么就可以了这里缺少的perl可能还会缺少libaio
、net-tools
等一般通过yum直接进行安装就可以了。
3. 配置mysql
到这里就已经完成mysql的安装工作了,接下来就对mysql进行配置
编辑/etc/my.cnf文件
skip-grant-tables # 添加这个参数,用来跳过mysql的密码登录
开启mysql服务
systemctl start mysqld
netstat -nltp | grep 3306 # 开启服务后查看mysql默认的3306端口号是否存在,存在则证明服务启动成功
登录mysql
mysql -u root -p # 提示输入密码后直接Enter就可以,不需要输入密码
设置mysql的输入密码
mysql> flush privileges; # 先刷新权限,因为设置的skip-grant-tables,直接设置密码会报错
mysql> set password for root@localhost = password('password'); # 设置密码
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option; # 开启远程连接
mysql> flush privileges; # 刷新权限
如果在开启远程链接时报错 Your password does not satisfy the current policy requirements
,这是 mysql 初始化时,使用临时密码,修改自定义密码时,由于自定义密码比较简单,就出现了不符合密码策略的问题,修改一下mysql的密码策略即可
mysql> set global validate_password_policy=LOW; # 验证密码等级强度
mysql> set global validate_password_length=6; # 设置密码长度
修改完成后重新执行远程链接的命令即可。
通过exit
退出mysql的命令窗口,systemctl stop mysqld
停止mysql的服务,将/etc/my.cnf文件中的skip-grant-tables
注释掉,然后根据需求配置一下my.cnf文件内容
#添加如下配置
[client]
default-character-set=utf8
[mysqld] # [mysqld]这个文件中本来就存在,不用添加
character-set-server=utf8 # 添加到[mysqld] 后面
collation-server=utf8_general_ci # 添加到p[mysqld]后面
重新开启mysql服务,并加入开机自启
systemctl start mysqld # 启动mysql服务
systemctl enable mysqld # 将mysql服务加入到开机自启
到这里所有的步骤就结束了,可以使用Navicat连接一下mysql看一下是否成功。