1. 安装包选择
使用命令ldd --version检查版本
[root@Stephen-VM-1 bin]# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
由 Roland McGrath 和 Ulrich Drepper 编写。
2. 下载对应的安装包
上一步中查看到GNU libc的版本为 2.17,在mysql官方下载页中选择
3. 安装
3.1 准备工作
由于本人之前使用yum安装了mysql的低版本,因此需要使用yum remove卸载
如果使用yum安装/升级MySQL 8.0.36版本,请查看之前文章
https://blog.csdn.net/weixin_60021553/article/details/136387875
再次确认是否卸载干净,是否安装了其他版本
[root@Stephen-VM-1 opt] rpm -q mysql mysql-server mariadb mariadb-server
未安装软件包 mysql
未安装软件包 mysql-server
未安装软件包 mariadb
未安装软件包 mariadb-server
如果想卸载干净,清理所有之前的mysql相关
find / -name mysql
查找并删除清理
3.2 解压放置到指定目录
1)找到下载的mysql tar压缩包,进行解压
tar xvf mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
2)移动到目录,本人安装到/usr/local下
mv mysql-8.0.36-linux-glibc2.17-x86_64 /usr/loca/mysql
3)创建必要目录(data, log),并修改权限(使用mysql用户和用户组)
cd /usr/local/mysql
mkdir data
mkdir log
chown -R mysql. *
本人CentOS操作系统之前使用yum安装,已经自动创建,如果没有mysql和mysql用户组,需要自行创建
useradd -M -s /sbin/nologin mysql
3.4 初始化
1)创建必要my.cnf配置项,vi /etc/my.cnf,本人的my.cnf参考
[mysqld]
# 监听端口
port=3306
# 安装目录
basedir=/usr/local/mysql
# 数据目录
datadir=/usr/local/mysql/data
# 最大连接数
max_connections = 100
# 默认数据库引擎
default-storage-engine = InnoDB
# 字符集
character-set-server=utf8mb4
# 错误日志
log-error = /usr/local/mysql/log/mysql.log
# 开启慢日志
slow_query_log = 1
# 慢查询日志时间阈值 默认是10S
long-query-time = 30
# 慢查询日志存放路径
slow_query_log_file = /usr/local/mysql/log/show.log
# 记录那些由于查找了多余1000次而引发的慢查询
min_examined_row_limit = 1000
#
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
2)进入 /usr/local/mysql 目录执行
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
3)检查日志 /usr/local/mysql/log/,获取临时密码
2024-03-11T03:31:09.719527Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.36) initializing of server in progress as process 29253
2024-03-11T03:31:09.731165Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-03-11T03:31:11.108114Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-03-11T03:31:14.348821Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: a5.?pj,tmtND
临时密码见日志:A temporary password is generated for root@localhost: a5.?pj,tmtND
使用其登录
3.5 加入系统自启动
按如下命令进行操作
cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
ln -s /usr/local/mysql/bin/* /usr/local/bin/
systemctl start mysqld
至此服务mysql-server已经启动,并加入到系统服务自启动
4. 修改初始密码并还原备份数据库
登录数据库,使用日志里的临时密码。
mysql -uroot -p -h 127.0.0.1
登录完成后,由于未修改初始密码,一些操作是无法使用的,如下:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 650
Server version: 8.0.36
Copyright (c) 2000, 2024, 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.
提示,必须“reset you password"
4.1 修改密码
按照提示,MySQL 8 使用 ALTER USER statement修改初始密码
mysql> ALTER USER USER() IDENTIFIED BY '新密码';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
修改完成后再次登录验证
4.2 还原备份数据库
个人习惯用 source 的方式还原
mysql> CREATE DATABASE 数据库名 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
mysql> use 数据库名
mysql> source /home/root/备份.sql