先去官网下载RPM包,没想到RPM包是红帽发行版

https://dev.mysql.com/downloads/mysql/

 

【Centos】RPM安装Mysql8_初始化

 

使用wget直接下载到Centos里面:

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar

解压Tar包:

[root@localhost ~]# tar -xvf mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.27-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.27-1.el7.x86_64.rpm
mysql-community-common-8.0.27-1.el7.x86_64.rpm
mysql-community-devel-8.0.27-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.27-1.el7.x86_64.rpm
mysql-community-libs-8.0.27-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.27-1.el7.x86_64.rpm
mysql-community-server-8.0.27-1.el7.x86_64.rpm
mysql-community-test-8.0.27-1.el7.x86_64.rpm
[root@localhost ~]#

 

删除残留的MariaDB

[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# 

二次检查:

[root@localhost ~]# rpm -qa|grep mariadb
[root@localhost ~]# 

 

开始安装:

rpm -ivh --nodeps --force mysql-community-common-8.0.27-1.el7.x86_64.rpm
rpm -ivh --nodeps --force mysql-community-libs-8.0.27-1.el7.x86_64.rpm
rpm -ivh --nodeps --force mysql-community-libs-compat-8.0.27-1.el7.x86_64.rpm
rpm -ivh --nodeps --force mysql-community-client-8.0.27-1.el7.x86_64.rpm
rpm -ivh --nodeps --force mysql-community-client-plugins-8.0.27-1.el7.x86_64.rpm
rpm -ivh --nodeps --force mysql-community-embedded-compat-8.0.27-1.el7.x86_64.rpm 
rpm -ivh --nodeps --force mysql-community-server-8.0.27-1.el7.x86_64.rpm

 

查看我们安装的内容:

[root@localhost ~]# rpm -qa | grep mysql 
mysql-community-libs-8.0.27-1.el7.x86_64
mysql-community-server-8.0.27-1.el7.x86_64
mysql-community-client-plugins-8.0.27-1.el7.x86_64
mysql-community-libs-compat-8.0.27-1.el7.x86_64
mysql-community-embedded-compat-8.0.27-1.el7.x86_64
mysql-community-common-8.0.27-1.el7.x86_64
mysql-community-client-8.0.27-1.el7.x86_64
[root@localhost ~]# 

 

查看当前MySQL服务状态:

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@localhost ~]#

 

不管MySQL是否运行还是不运行,都执行服务停止命令

[root@localhost ~]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
[root@localhost ~]# 

 

执行初始化:

[root@localhost ~]# mysqld --initialize --console
[root@localhost ~]# 

 

目录可写权限赋予:

[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql/
[root@localhost ~]# 

 

启动MySQL:

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]#

 

查看服务状态:

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2021-10-24 09:31:18 CST; 3s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 9201 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 9241 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─9241 /usr/sbin/mysqld

10月 24 09:31:04 localhost.localdomain systemd[1]: Starting MySQL Server...
10月 24 09:31:18 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# 

 

翻看日志文件,找到初始化的密码:

[root@localhost ~]# cat /var/log/mysqld.log
2021-10-24T01:29:47.674319Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.27) initializing of server in progress as process 8984
2021-10-24T01:29:47.888787Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-10-24T01:29:50.370284Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-10-24T01:29:54.018924Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2021-10-24T01:29:54.022463Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2021-10-24T01:29:54.293689Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: SoOu?/jep09>
2021-10-24T01:31:13.997176Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 9241
2021-10-24T01:31:14.148958Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-10-24T01:31:15.775588Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-10-24T01:31:18.363257Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2021-10-24T01:31:18.381048Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2021-10-24T01:31:18.383717Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-10-24T01:31:18.383752Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-10-24T01:31:18.586545Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
2021-10-24T01:31:18.586771Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
[root@localhost ~]# 

 

登陆MySQL,输入随机生成的密码:【SoOu?/jep09>】

[root@localhost ~]# 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.27

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> 

 

设置root用户密码永不过期:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; 

更改root密码为123456:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

设置root账户允许被外部访问:

UPDATE mysql.`user` SET host = "%" WHERE user = "root";

刷新权限:

FLUSH PRIVILEGES;

退出:

QUIT;

 

防火墙拦截处理:

先查看防火墙状态:

[root@localhost ~]#  systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 日 2021-10-24 09:55:25 CST; 5s ago
     Docs: man:firewalld(1)
 Main PID: 12734 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─12734 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

10月 24 09:55:23 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
10月 24 09:55:25 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
10月 24 09:55:26 localhost.localdomain firewalld[12734]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
[root@localhost ~]# 

防火墙开启,没开放端口,会被拦截下来:

【Centos】RPM安装Mysql8_初始化_02

 

查看开放的端口列表:

[root@localhost ~]# firewall-cmd --zone=public --list-ports

[root@localhost ~]# 

开放3306端口:

[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost ~]# 

重新加载防火墙:

[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]#