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

推荐订阅源

V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
V
V2EX
IT之家
IT之家
J
Java Code Geeks
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
D
Docker
S
Secure Thoughts
Recent Announcements
Recent Announcements
Webroot Blog
Webroot Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
博客园_首页
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Security Archives - TechRepublic
Security Archives - TechRepublic
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
I
InfoQ
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
W
WeLiveSecurity
T
Troy Hunt's Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Attack and Defense Labs
Attack and Defense Labs
美团技术团队
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Scott Helme
Scott Helme
T
Tor Project blog
Know Your Adversary
Know Your Adversary
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
G
Google Developers 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源码解读 TenantLineInnerInterceptor源码解读 BaseMultiTableInnerInterceptor源码解读 Spring AI开篇 SQL解析工具JSQLParser 芋道源码解读之多租户 芋道源码解读之数据权限 芋道源码解读开篇 Java实现将数据导出为Word文档 OA系统的天数该怎样计算 安装MySQL5.7 RockyLinux9环境下编译MySQL8 MySQL字符集及底层原理 Java实现LDAP登录 Docker Compose IPv4和IPv6 使用虚拟机安装一个K8s集群 使用GraalVM原生编译打包SpringBoot工程 Nginx防止目录穿越 Java线程的状态 Nginx防盗链设置 使用python将excel表格转换为SQL INSERT Redis的公共操作命令 Redis数据结构之Bitfleid Redis数据结构之Bitmap Redis数据结构之GEO Redis数据结构之Hash Redis数据结构之HyperLogLog Redis数据结构之List Redis数据结构之Set Redis数据结构之Stream Redis数据结构之String Redis数据结构之ZSet 使用python压缩图片 利用Python实现Hexo站点的持续集成 Nginx设置HTTPS监听 firewalld防火墙工具的使用 Linux信号(signal)机制 MySQL5.7x 主从复制 用IP自签发一个HTTPS证书 基于Hexo实现一个静态的个人博客 RockyLinux9环境下编译MySQL5.7 Docker离线安装 MySQL数据定义语言 Docker与联合文件系统 Docker的网络 Docker的镜像操作 MySQL存储过程 MyBatis-Plus开篇 MySQL变量 MySQL视图 MySQL事务 MySQL插入修改和删除 MySQL查询 MySQL系统命令 Docker的容器操作 Docker的安装和配置 Docker容器数据卷 浅谈OAuth2.0授权原理 JVM开篇 浅谈Linux(Unix)的I/O模型 一个通用的CloseableHttpClient工厂类 JUC可重入锁ReentrantLock JUC读写锁ReadWriteLock Java的单例 Java泛型 Java8的新特性 最近最少使用算法(LRU) MySQL函数 SpringBoot配置和启动 volatile作用分析
安装MySQL8
Liu Zijian · 2024-12-21 · via Liu Zijian's Blog | 一个技术博客

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包有相互的依赖关系,需要安装的包以及安装顺序如下:

  1. mysql-community-common-8.0.25-1.el7.x86_64.rpm
  2. mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
  3. mysql-community-libs-8.0.25-1.el7.x86_64.rpm
  4. mysql-community-client-8.0.25-1.el7.x86_64.rpm
  5. 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)