惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Jina AI
Jina AI
罗磊的独立博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
月光博客
月光博客
博客园_首页
Vercel News
Vercel News
P
Proofpoint News Feed
GbyAI
GbyAI
Y
Y Combinator Blog

Liu Zijian's Blog | 一个技术博客

使用Certbot自动续签HTTPS证书 使用Filebeat采集Nginx日志到ES Python的协程 Python中的异常 Python中的类和对象 Python的函数 Python的数据结构,推导式、迭代器和生成器 Spring AI集成多模态模型 LangChain4j多模态 LangChain Tools工具使用 Python中的模块和包 Python全局环境和虚拟环境(venv) LangChain Prompt提示词工程 LangChain4j Tools工具使用 基于Dify搭建AI智能体应用 LangChain4j RAG检索增强生成 Spring AI实现MCP Server Spring AI集成MCP Client LangChain4j Prompt提示词工程 Spring AI使用知识库增强对话功能 Spring AI实现一个智能客服 Spring AI实现一个简单的对话机器人 实现MinIO数据的每日备份 自己实现一个DNS服务 简单理解AI智能体 大模型和大模型应用 LangChain开篇 LangChain4j开篇 一个解析Excel2007的POI工具类 DataPermissionInterceptor源码解读
安装MySQL5.7
Liu Zijian · 2024-12-21 · via Liu Zijian's Blog | 一个技术博客

1.下载

打开官网下载页面: https://downloads.mysql.com/archives/community/,在这里我选择5.7.28版本,Operating System一栏通常没有CentOS,选择RedHat即可,OS Version选择7,X86_64,然后下载第一个mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

下载完成后,将文件移动到/opt下,并解压,会解压出多个rpm软件包

[root@localhost opt]# tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar 
mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-test-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-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包有相互的依赖关系,需要安装的包以及安装顺序如下:

  1. mysql-community-common-5.7.28-1.el7.x86_64.rpm
  2. mysql-community-libs-5.7.28-1.el7.x86_64.rpm
  3. mysql-community-client-5.7.28-1.el7.x86_64.rpm
  4. mysql-community-server-5.7.28-1.el7.x86_64.rpm

使用rpm -ivh命令安装这些包

[root@localhost opt]# rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-common-5.7.28-1.e################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-libs-
mysql-community-libs-5.7.28-1.el7.x86_64.rpm         mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh  mysql-community-libs-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-5.7.28-1.el7################################# [100%]
[root@localhost opt]# rpm -ivh  mysql-community-client-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-client-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-5.7.28-1.e################################# [100%]
[root@localhost opt]# rpm -ivh  mysql-community-server-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-5.7.28-1.e################################# [100%]

通过rpm -qa已经能看到装上去的包了

[root@localhost opt]# rpm -qa | grep mysql
mysql-community-libs-5.7.28-1.el7.x86_64
mysql-community-common-5.7.28-1.el7.x86_64
mysql-community-client-5.7.28-1.el7.x86_64
mysql-community-server-5.7.28-1.el7.x86_64

查看一下安装的版本

[root@localhost opt]# mysql --version
mysql  Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using  EditLine wrapper

为了保证数据库目录与文件的所有者为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-06T12:26:08.004754Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-04-06T12:26:08.339163Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-04-06T12:26:08.403698Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-04-06T12:26:08.470936Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5151ca04-12e2-11f0-8409-000c29920d74.
2025-04-06T12:26:08.472919Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-04-06T12:26:10.249602Z 0 [Warning] CA certificate ca.pem is self signed.
2025-04-06T12:26:10.762512Z 1 [Note] A temporary password is generated for root@localhost: G*kRrqwYn7KX

接下来启动服务

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 20:27:54 CST; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1468 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1451 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1471 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1471 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

4月 06 20:27:53 localhost.localdomain systemd[1]: Starting MySQL Server...
4月 06 20:27:54 localhost.localdomain systemd[1]: Started MySQL Server.

4.登录和基础配置

4.1登录并重置密码

用之前生成的初始密码登录MySQL的root用户,命令是:mysql -uroot -pEnter 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 2
Server version: 5.7.28

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

执行这个语句重置密码

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.00 sec)

mysql> use mysql;
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 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,从而避免上述乱码的问题。

查看当前字符集

show variables like '%char%';
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

字符集不支持中文,因此MySQL5.7需要修改服务端字符集,修改字符集的配置文件位于/etc/my.cnf,将配置添加在[mysqld]节点上

[mysqld]

character-set-server = utf8

修改后新建的库和表才会使用utf8,之前建的库表的字符集不会被自动修改,需要手动修改:

⚠️ utf8兼容latin1,因此之前的数据支持修改字符集为utf8,如果不兼容就可能出现乱码问题,不兼容的情况可以将数据导出在重新插入

修改库

alter database db_xxx character set 'utf8';

修改表

alter table tb_xxx convert to character set 'utf8';