安装MySQL8
本文最后更新于 2025年4月8日
1.下载
打开官网下载页面: https://downloads.mysql.com/archives/community/,在这里我选择8.0.25版本,Operating System一栏通常没有CentOS,选择RedHat即可,OS Version选择7,X86_64,然后下载第一个mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
下载完成后,将文件移动到/opt下,并解压,会解压出多个rpm软件包
[root@localhost opt]# tar -xvf mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.25-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
mysql-community-common-8.0.25-1.el7.x86_64.rpm
mysql-community-devel-8.0.25-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.25-1.el7.x86_64.rpm
mysql-community-libs-8.0.25-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
mysql-community-server-8.0.25-1.el7.x86_64.rpm
mysql-community-test-8.0.25-1.el7.x86_64.rpm
2.安装前准备
2.1 卸载自带的mariadb
安装基于CentOS 7.9.2009
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
CentOS 7.9系统预装了一个mariadb数据库,这个数据库是MySQL的”姐妹”,都是同一个作者编写的,mariadb会在/etc
目录生成一个my.cnf
作为配置文件,MySQL安装后也会在/etc
目录生成一个my.cnf
,因此为了排除干扰,首先要把预装的mariadb卸载掉。
查看是否预装:
[root@localhost ~]# rpm -qa | grep -i maria
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# ls /etc/ | grep my
my.cnf
my.cnf.d
卸载mariadb
rpm -e --nodeps mariadb-libs
2.2 安装依赖
截至目前,CentOS7的yum源已经无法访问,因此需要先更换为阿里巴巴的yum源
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
然后安装依赖项,我采用的系统版本需要额外安装这两个依赖,缺失这两个将导致后续安装mysql-community-server时出错
yum install -y net-tools
yum install -y perl
3.安装和初始化
cd进入/opt目录安装刚刚解压出的软件包,不需要每个都安装,仅安装MySQL的Server和Client以及依赖的包,安装需要按照顺序,因为rpm包有相互的依赖关系,需要安装的包以及安装顺序如下:
- mysql-community-common-8.0.25-1.el7.x86_64.rpm
- mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
- mysql-community-libs-8.0.25-1.el7.x86_64.rpm
- mysql-community-client-8.0.25-1.el7.x86_64.rpm
- mysql-community-server-8.0.25-1.el7.x86_64.rpm
使用rpm -ivh
命令安装这些包
[root@localhost opt]# rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-common-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.0.25-1.e################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
'警告:mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-plugins-8.################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.0.25-1.el7################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-client-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-8.0.25-1.e################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-server-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-8.0.25-1.e################################# [100%]
安装完成,查看一下安装的版本
[root@localhost opt]# mysql --version
mysql Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)
[root@localhost opt]# mysqladmin --version
mysqladmin Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)
为了保证数据库目录与文件的所有者为mysql登录用户,如果是以root身份运行mysql服务,需要执行下面的命令初始化:
mysqld --initialize --user=mysql
说明: --initialize
选项默认以安全模式来初始化,则会为root用户生成一个密码并将该密码标记为过期,登录后你需要设置一个新的密码。生成的临时密码会往日志中记录一份,查看密码:
cat /var/log/mysqld.log
[root@localhost opt]# mysqld --initialize --user=mysql
[root@localhost opt]# cat /var/log/mysqld.log
2025-04-06T10:34:02.749835Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.25) initializing of server in progress as process 9479
2025-04-06T10:34:02.921735Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-04-06T10:34:06.304242Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-04-06T10:34:10.865584Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: /*E.4-*jf_#)
[root@localhost opt]#
接下来启动服务
systemctl start mysqld
[root@localhost opt]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2025-04-06 18:39:55 CST; 7s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 9531 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 9556 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─9556 /usr/sbin/mysqld
4月 06 18:39:46 localhost.localdomain systemd[1]: Starting MySQL Server...
4月 06 18:39:55 localhost.localdomain systemd[1]: Started MySQL Server.
4.登录和基础配置
4.1 登录并重置密码
用之前生成的初始密码登录MySQL的root用户,命令是:mysql -uroot -p
,Enter password:后输入密码,登录成功并进入MySQL交互命令行,但是不能操作,提示需要重置root密码
[root@localhost opt]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25
Copyright (c) 2000, 2021, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
执行这个语句重置密码
alter user 'root'@'localhost' identified by 'lzj';
4.2 打开远程连接
安装好的MySQL会自带4个数据库,连接配置的表位于mysql库的user表上,use mysql;
将当前库切换为mysql,通过select host,user from user;
查看得知系统中的4个用户都仅允许本机访问
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.65 sec)
mysql> use mysql;
Database changed
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
4 rows in set (0.01 sec)
通过执行以下语句修改root用户可访问的地址,可以采用通配符,按自己需要修改即可,我选择直接修改为%
允许root用户从192.168.1.x主机访问
update mysql.user set host ='192.168.1.%' where user = 'root';
允许root用户从一切远程主机访问
update mysql.user set host ='%' where user = 'root';
修改完成,刷新权限,即可远程连接
flush privileges;
远程连接前一定要关闭CentOS7自带的防火墙firewalld
4.3 修改字符集
在MySQL8.0版本之前,默认字符集为latin1
,utf8字符集指向的是utf8mb3
。网站开发人员在数据库设计的时候往往会将编码修改为utf8字符集,如果遗忘修改默认的编码,就会出现乱码的问题,从MySQL8.0开始,数据库的默认编码将改为utf8mb4
,从而避免上述乱码的问题。
mysql> show variables like '%char%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.12 sec)