- 通过
rpm -qa | grep mariadb
命令查看mariadb
的安装包。
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-connector-c-3.3.8-2.oe2403sp1.x86_64
[root@localhost ~]#
- 通过
rpm -e xxx --nodeps
命令装卸mariadb
。
[root@localhost ~]# rpm -e mariadb-connector-c-3.3.8-2.oe2403sp1.x86_64 --nodeps
#通过 rpm -qa | grep mariadb 命令再次查看 mariadb 的安装包
[root@localhost ~]# rpm -qa | grep mariadb #说明已经没有mariadb
[root@localhost ~]#
- 为例避免安装失败,检查是否已经存在mysql相关文件,有的话全删除。
[root@localhost ~]# rpm -qa |grep mysql
#如果结果显示就使用rpm -e --nodeps卸载
[root@localhost ~]#
#查找mysql文件的
[root@localhost ~]# find / -name mysql
/usr/share/bash-completion/completions/mysql
/var/lib/selinux/targeted/active/modules/100/mysql
[root@localhost ~]#
[root@localhost ~]# rm -rf /usr/share/bash-completion/completions/mysql
[root@localhost ~]# rm -rf /var/lib/selinux/targeted/active/modules/100/mysql
- 下载MySQL软件包。
root@localhost ~]# wget https://cdn.mysql.com/archives/mysql-8.4/mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar
--2025-03-11 11:00:58-- https://cdn.mysql.com/archives/mysql-8.4/mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar
正在解析主机 cdn.mysql.com (cdn.mysql.com)... 23.202.138.125, 2600:1413:5000:98f::1d68, 2600:1413:5000:988::1d68
正在连接 cdn.mysql.com (cdn.mysql.com)|23.202.138.125|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:887470080 (846M) [application/x-tar]
正在保存至: “mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar”
9.x86_64.rpm-bundle.tar 1%[> ] 16.00M 1.56MB/s 剩余 9m 1s
- 创建一个目录文件并把MySQL软件包解压到该目录。
[root@localhost ~]# mkdir mysql
[root@localhost ~]# ls
mysql mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar
[root@localhost ~]# tar -xvf mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar -C mysql/
[root@localhost ~]# cd mysql/
[root@localhost mysql]# ls
mysql-community-client-8.4.2-1.el9.x86_64.rpm mysql-community-libs-compat-8.4.2-1.el9.x86_64.rpm
mysql-community-client-debuginfo-8.4.2-1.el9.x86_64.rpm mysql-community-libs-compat-debuginfo-8.4.2-1.el9.x86_64.rpm
mysql-community-client-plugins-8.4.2-1.el9.x86_64.rpm mysql-community-libs-debuginfo-8.4.2-1.el9.x86_64.rpm
mysql-community-client-plugins-debuginfo-8.4.2-1.el9.x86_64.rpm mysql-community-server-8.4.2-1.el9.x86_64.rpm
mysql-community-common-8.4.2-1.el9.x86_64.rpm mysql-community-server-debug-8.4.2-1.el9.x86_64.rpm
mysql-community-debuginfo-8.4.2-1.el9.x86_64.rpm mysql-community-server-debug-debuginfo-8.4.2-1.el9.x86_64.rpm
mysql-community-debugsource-8.4.2-1.el9.x86_64.rpm mysql-community-server-debuginfo-8.4.2-1.el9.x86_64.rpm
mysql-community-devel-8.4.2-1.el9.x86_64.rpm mysql-community-test-8.4.2-1.el9.x86_64.rpm
mysql-community-icu-data-files-8.4.2-1.el9.x86_64.rpm mysql-community-test-debuginfo-8.4.2-1.el9.x86_64.rpm
mysql-community-libs-8.4.2-1.el9.x86_64.rpm
[root@localhost mysql]#
#在此重点不是调试,把debug相关的安装包删除
[root@localhost mysql]# rm -rf *debug*
[root@localhost mysql]# ls
mysql-community-client-8.4.2-1.el9.x86_64.rpm mysql-community-libs-8.4.2-1.el9.x86_64.rpm
mysql-community-client-plugins-8.4.2-1.el9.x86_64.rpm mysql-community-libs-compat-8.4.2-1.el9.x86_64.rpm
mysql-community-common-8.4.2-1.el9.x86_64.rpm mysql-community-server-8.4.2-1.el9.x86_64.rpm
mysql-community-devel-8.4.2-1.el9.x86_64.rpm mysql-community-test-8.4.2-1.el9.x86_64.rpm
mysql-community-icu-data-files-8.4.2-1.el9.x86_64.rpm
[root@localhost mysql]#
- 安装MySQL。
[root@localhost mysql]# rpm -ivh *.rpm --force --nodeps
警告:mysql-community-client-8.4.2-1.el9.x86_64.rpm: 头 V4 RSA/SHA256 Signature, 密钥 ID a8d3785c: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.4.2-1.el################################# [ 11%]
2:mysql-community-client-plugins-8.################################# [ 22%]
3:mysql-community-libs-8.4.2-1.el9 ################################# [ 33%]
4:mysql-community-client-8.4.2-1.el################################# [ 44%]
5:mysql-community-icu-data-files-8.################################# [ 56%]
6:mysql-community-server-8.4.2-1.el################################# [ 67%]
7:mysql-community-test-8.4.2-1.el9 ################################# [ 78%]
8:mysql-community-devel-8.4.2-1.el9################################# [ 89%]
9:mysql-community-libs-compat-8.4.2################################# [100%]
/usr/lib/tmpfiles.d/dbus.conf:13: Line references path below legacy directory /var/run/, updating /var/run/dbus/containers → /run/dbus/containers; please update the tmpfiles.d/ drop-in file accordingly.
- 启动MySQL服务,并设置服务开机启动。
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl enable mysqld
[root@localhost mysql]#
查看是否启动
[root@localhost mysql]# ps -ef | grep mysql
mysql 5108 1 0 15:05 ? 00:00:01 /usr/sbin/mysqld
#就看到mysqld进程即为已经启动
- 通过
cat /var/log/mysqld.log | grep password
命令查看数据库的初始密码。
[root@localhost mysql]# cat /var/log/mysqld.log | grep password
2025-03-11T07:05:03.797360Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _Qeq&=,Nl5-V
[root@localhost mysql]#
在该例中,在root@localhost:
后面的一串字符_Qeq&=,Nl5-V
即为初始密码。
- 使用初始密码登录MySQL。
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.2
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>
- 通过
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxx';
命令来修改登录密码。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
在此修改密码为
Root@123456
为例,根据实际需求设置密码。如果有修改为简单密码的需求可以降低密码策略,查看当前密码策略命令
mysql> SHOW VARIABLES LIKE 'validate_password%'; +-------------------------------------------------+--------+ | Variable_name | Value | +-------------------------------------------------+--------+ | validate_password.changed_characters_percentage | 0 | | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | +-------------------------------------------------+--------+ 8 rows in set (0.01 sec)
使用
set global
设置相应的值。
- 通过
exit;
命令退出 MySQL,然后通过新密码再次登陆。
mysql> exit;
Bye
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.4.2 MySQL Community Server - GPL
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>
- 通过以下命令,进行远程访问的授权
CREATE USER 'root'@'%' IDENTIFIED WITH caching_sha2_password by 'Root@123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
注:这里远程密码以
Root@123456
为例,根据实际需求设置密码。
mysql> CREATE USER 'root'@'%' IDENTIFIED WITH caching_sha2_password by 'Root@123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
- 远程连接登录方式验证。
[root@localhost mysql]# mysql -h 10.90.100.150 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 8.4.2 MySQL Community Server - GPL
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>
其中
-h
后面的IP地址为MySQL服务所在的主机IP地址。在此需要注意,如果是跨主机连接出现不成功就看一下检查防火墙是。
至此,MySQL8.4就已经安装完成,可以把.tar
包和解压的安装包文件进行删除掉了。
[root@localhost ~]# ls
anaconda-ks.cfg mysql mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar
[root@localhost ~]# rm -rf mysql mysql-8.4.2-1.el9.x86_64.rpm-bundle.tar
[root@localhost ~]#
参考:
https://blog.youkuaiyun.com/m0_74282926/article/details/143993089
https://blog.youkuaiyun.com/sunyuhua_keyboard/article/details/142452111