1、安装MySQL
(1)在线安装
[root@localhost Mysql]# dnf install mysql-server
(2)离线安装
获取安装包
[root@localhost ~]# ll
total 975916
-rw-r--r-- 1 root root 999311360 Feb 26 17:50 mysql-8.0.36-1.el8.x86_64.rpm-bundle.tar
2、创建路径并解压
[root@localhost ~]# mkdir /Mysql
[root@localhost ~]# tar -xf mysql-8.0.36-1.el8.x86_64.rpm-bundle.tar -C /Mysql/
[root@localhost ~]# cd /Mysql
[root@localhost Mysql]# ll
total 975916
-rw-r--r-- 1 7155 31415 16734240 Dec 14 2023 mysql-community-client-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 32183048 Dec 14 2023 mysql-community-client-debuginfo-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 3724348 Dec 14 2023 mysql-community-client-plugins-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 3534396 Dec 14 2023 mysql-community-client-plugins-debuginfo-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 683892 Dec 14 2023 mysql-community-common-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 3699208 Dec 14 2023 mysql-community-debuginfo-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 25187132 Dec 14 2023 mysql-community-debugsource-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 7911104 Dec 14 2023 mysql-community-devel-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 2347336 Dec 14 2023 mysql-community-icu-data-files-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 1564796 Dec 14 2023 mysql-community-libs-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 2640980 Dec 14 2023 mysql-community-libs-debuginfo-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 67559076 Dec 14 2023 mysql-community-server-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 25954052 Dec 14 2023 mysql-community-server-debug-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 161003740 Dec 14 2023 mysql-community-server-debug-debuginfo-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 244094580 Dec 14 2023 mysql-community-server-debuginfo-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 376738224 Dec 14 2023 mysql-community-test-8.0.36-1.el8.x86_64.rpm
-rw-r--r-- 1 7155 31415 23729176 Dec 14 2023 mysql-community-test-debuginfo-8.0.36-1.el8.x86_64.rpm
3、安装依赖
[root@localhost Mysql]# yum install mysql-community-server-8.0.36-1.el8.x86_64.rpm yum install mysql-community-client-8.0.36-1.el8.x86_64.rpm mysql-community-common-8.0.36-1.el8.x86_64.rpm mysql-community-icu-data-files-8.0.36-1.el8.x86_64.rpm yum install mysql-community-client-plugins-8.0.36-1.el8.x86_64.rpm mysql-community-libs-8.0.36-1.el8.x86_64.rpm
4、启动服务
[root@localhost Mysql]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
5、查看服务状态
[root@localhost Mysql]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database serverLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Wed 2025-02-26 18:04:17 CST; 12s agoProcess: 2186 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)Process: 2208 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)Main PID: 2286 (mysqld)Status: "Server is operational"Tasks: 38 (limit: 21400)Memory: 464.2MCGroup: /system.slice/mysqld.service└─ 2286 /usr/libexec/mysqld --basedir=/usrFeb 26 18:04:10 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server...
Feb 26 18:04:10 localhost.localdomain mysql-prepare-db-dir[2208]: Initializing MySQL database
Feb 26 18:04:17 localhost.localdomain systemd[1]: Started MySQL 8.0 database server.
6、 查看端口
[root@localhost Mysql]# netstat -lnupt | grep 3306
tcp6 0 0 :::33060 :::* LISTEN 2286/mysqld
tcp6 0 0 :::3306 :::* LISTEN 2286/mysqld
前提安装 net-tools
[root@localhost Mysql]# yum install net-tools
在线安装无需密码可直接进入MySQL
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.41 Source distributionCopyright (c) 2000, 2025, 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>
离线安装需要获取密码
[root@localhost Mysql]# grep -i password /var/log/mysql.log
2025-02-26T10:55:23.740726Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;inS=:oe7DeZ
[root@localhost Mysql]# awk '/temporary password/ {print $NF}' /var/log/mysqld.log
;inS=:oe7DeZ
7、修改密码
mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.01 sec)
8、再次登入MySQL并查看数据库
[root@localhost ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.41 Source distributionCopyright (c) 2000, 2025, 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;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)mysql> \q
9、如无需输入密码直接进入MySQL
[root@localhost ~]# vim /etc/my.cnf
[client]
user=root
password=123456
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.41 Source distributionCopyright (c) 2000, 2025, 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>