#查询已经存在的mysql及相关依赖包
[root@localhost opt]# rpm -qa | grep -i mysql
#此时你会看到一堆列表,使用 yum 命令依次卸载
#yum remove mysql-xxx-xxx-xxx
#接下来删除 mysql 的残留目录及文件,先用下面的命令找出所有mysql相关的文件夹
[root@localhost opt]# find / -name mysql
/home/java/.m2/repository/mysql
find: ‘/proc/2349578’: No such file or directory
find: ‘/proc/2350559’: No such file or directory
find: ‘/proc/2350560’: No such file or directory
find: ‘/proc/2350569’: No such file or directory
find: ‘/proc/2350571’: No such file or directory
find: ‘/proc/2350573’: No such file or directory
find: ‘/proc/2350577’: No such file or directory
/var/lib/selinux/targeted/active/modules/100/mysql
/usr/share/bash-completion/completions/mysql
/usr/share/selinux/targeted/default/active/modules/100/mysql
#如下依次删除
[root@localhost ~]# rm -rf /var/lib/selinux/targeted/active/modules/100/mysql
[root@localhost ~]# rm -rf /usr/share/bash-completion/completions/mysql
[root@localhost ~]# rm -rf /usr/share/selinux/targeted/default/active/modules/100/mysql
#下载rpm文件
[root@localhost opt]# wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
#安装rpm文件
[root@localhost opt]# rpm -ivh mysql80-community-release-el8-1.noarch.rpm
warning: mysql80-community-release-el8-1.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el8-1 ################################# [100%]
#执行成功后,命令查看 /etc/yum.repos.d/ 文件夹下会产生两个文件
[root@localhost opt]# ls /etc/yum.repos.d/ | grep mysql
mysql-community.repo
mysql-community-source.repo
#更新yum缓存
[root@localhost opt]# yum clean all
36 files removed
[root@localhost opt]# yum makecache
CentOS Linux 8 - AppStream 87 B/s | 38 B 00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
#上面如果报错就执行下面设置镜像地址
[root@localhost opt]# sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
[root@localhost opt]# sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
[root@localhost opt]# yum makecache
CentOS Linux 8 - AppStream 1.7 MB/s | 8.4 MB 00:05
CentOS Linux 8 - BaseOS 285 kB/s | 4.6 MB 00:16
CentOS Linux 8 - Extras 4.2 kB/s | 10 kB 00:02
MySQL 8.0 Community Server 916 kB/s | 3.2 MB 00:03
MySQL Connectors Community 47 kB/s | 102 kB 00:02
MySQL Tools Community 276 kB/s | 794 kB 00:02
Metadata cache created.
#开始安装mysql
[root@localhost opt]# yum install mysql-community-server
Last metadata expiration check: 0:00:20 ago on Wed 06 Sep 2023 03:06:10 PM CST.
All matches were filtered out by modular filtering for argument: mysql-community-server
Error: Unable to find a match: mysql-community-server
#上面如果报错,就执行
[root@localhost opt]# yum module disable mysql
Last metadata expiration check: 0:04:15 ago on Wed 06 Sep 2023 03:06:10 PM CST.
Dependencies resolved.
============================================================================================================================================================================================================================================
Package Architecture Version Repository Size
============================================================================================================================================================================================================================================
Disabling modules:
mysql
Transaction Summary
============================================================================================================================================================================================================================================
Is this ok [y/N]: y
Complete!
#继续开始安装mysql,并加上--nogpgcheck
[root@localhost opt]# yum -y install mysql-community-server --nogpgcheck
Last metadata expiration check: 0:07:50 ago on Wed 06 Sep 2023 03:06:10 PM CST.
Dependencies resolved.
============================================================================================================================================================================================================================================
Package Architecture Version Repository Size
============================================================================================================================================================================================================================================
Installing:
mysql-community-server x86_64 8.0.34-1.el8 mysql80-community 64 M
Installing dependencies:
mysql-community-client x86_64 8.0.34-1.el8 mysql80-community 16 M
mysql-community-client-plugins x86_64 8.0.34-1.el8 mysql80-community 3.5 M
mysql-community-common x86_64 8.0.34-1.el8 mysql80-community 668 k
mysql-community-icu-data-files x86_64 8.0.34-1.el8 mysql80-community 2.2 M
mysql-community-libs x86_64 8.0.34-1.el8 mysql80-community 1.5 M
Transaction Summary
============================================================================================================================================================================================================================================
Install 6 Packages
Total size: 88 M
Installed size: 415 M
Downloading Packages:
[SKIPPED] mysql-community-client-8.0.34-1.el8.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-client-plugins-8.0.34-1.el8.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-common-8.0.34-1.el8.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-icu-data-files-8.0.34-1.el8.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-libs-8.0.34-1.el8.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-server-8.0.34-1.el8.x86_64.rpm: Already downloaded
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-community-common-8.0.34-1.el8.x86_64 1/6
Installing : mysql-community-client-plugins-8.0.34-1.el8.x86_64 2/6
Installing : mysql-community-libs-8.0.34-1.el8.x86_64 3/6
Running scriptlet: mysql-community-libs-8.0.34-1.el8.x86_64 3/6
Installing : mysql-community-client-8.0.34-1.el8.x86_64 4/6
Installing : mysql-community-icu-data-files-8.0.34-1.el8.x86_64 5/6
Running scriptlet: mysql-community-server-8.0.34-1.el8.x86_64 6/6
Installing : mysql-community-server-8.0.34-1.el8.x86_64 6/6
Running scriptlet: mysql-community-server-8.0.34-1.el8.x86_64 6/6
Verifying : mysql-community-client-8.0.34-1.el8.x86_64 1/6
Verifying : mysql-community-client-plugins-8.0.34-1.el8.x86_64 2/6
Verifying : mysql-community-common-8.0.34-1.el8.x86_64 3/6
Verifying : mysql-community-icu-data-files-8.0.34-1.el8.x86_64 4/6
Verifying : mysql-community-libs-8.0.34-1.el8.x86_64 5/6
Verifying : mysql-community-server-8.0.34-1.el8.x86_64 6/6
Installed:
mysql-community-client-8.0.34-1.el8.x86_64 mysql-community-client-plugins-8.0.34-1.el8.x86_64 mysql-community-common-8.0.34-1.el8.x86_64 mysql-community-icu-data-files-8.0.34-1.el8.x86_64 mysql-community-libs-8.0.34-1.el8.x86_64
mysql-community-server-8.0.34-1.el8.x86_64
Complete!
#启动 mysql 并设为开机启动
[root@localhost opt]# systemctl start mysqld
[root@localhost opt]# systemctl enable mysqld
#获取初始密码登录mysql并修改自己的密码
[root@localhost opt]# cat /var/log/mysqld.log | grep password
2023-09-06T07:21:55.300090Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: oJSos8=t)gvo
#用上面获取的初始密码"oJSos8=t)gvo"登录mysql
[root@localhost opt]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34
Copyright (c) 2000, 2023, 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.
#修改密码,如图 “PassWord@666” 替换成自己的密码,注意 mysql 默认情况下设置的密码需要有大小写+数字+特殊符号
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'PassWord@666';
Query OK, 0 rows affected (0.00 sec)
#设置远程访问链接数据库
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set Host = '%' where User = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
#开启端口
[root@localhost opt]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost opt]# firewall-cmd --reload
success