系统版本:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@localhost ~]#

 

下载地址:https://dev.mysql.com/downloads/mysql/  点击download下载

 

 

卸载mariadb

[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64

 

yum -y remove mariadb-libs-5.5.56-2.el7.x86_64

解压mysql包:

tar xf  mysql-8.0.23-1.el7.x86_64.rpm-bundle.tar

安装过程:

[root@localhost ~]# rpm -ivh mysql-community-common-8.0.23-1.el7.x86_64.rpm
warning: mysql-community-common-8.0.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-common-8.0.23-1.e################################# [100%]

[root@localhost ~]# rpm -ivh  mysql-community-client-plugins-8.0.23-1.el7.x86_64.rpm
warning: mysql-community-client-plugins-8.0.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-client-plugins-8.################################# [100%]
[root@localhost ~]# rpm -ivh  mysql-community-libs-8.0.23-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-libs-8.0.23-1.el7################################# [100%]

[root@localhost ~]# rpm -ivh  mysql-community-client-8.0.23-1.el7.x86_64.rpm 
warning: mysql-community-client-8.0.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-client-8.0.23-1.e################################# [100%]
[root@localhost ~]# rpm -ivh  mysql-community-server-8.0.23-1.el7.x86_64.rpm 
warning: mysql-community-server-8.0.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-server-8.0.23-1.e################################# [100%]
  1.  mysqld --initialize;                                    #初始化
  2.  
    chown mysql:mysql /var/lib/mysql -R;    #修改属主和拥有者
  3.  
    systemctl start mysqld.service;               #启动mysql  
  4.  
    systemctl enable mysqld;                        #开机启动 
[root@localhost ~]# rpm -qa|grep mysql
mysql-community-server-8.0.23-1.el7.x86_64
mysql-community-client-plugins-8.0.23-1.el7.x86_64
mysql-community-libs-8.0.23-1.el7.x86_64
mysql-community-client-8.0.23-1.el7.x86_64
mysql-community-common-8.0.23-1.el7.x86_64
[root@localhost ~]# 
[root@localhost ~]# mysqld --initialize;
[root@localhost ~]# chown mysql:mysql /var/lib/mysql -R;
[root@localhost ~]# systemctl start mysqld.service;
[root@localhost ~]# ps -ef|grep mysql
mysql      4474      1  2 18:31 ?        00:00:01 /usr/sbin/mysqld
root       4539   4302  0 18:32 pts/3    00:00:00 grep --color=auto mysql
[root@localhost ~]# 
[root@localhost ~]# systemctl  enable mysqld
[root@localhost ~]# systemctl  status mysqld
鈼[0m mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-03-09 18:31:05 PST; 1min 26s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 4474 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           鈹斺攢4474 /usr/sbin/mysqld

Mar 09 18:30:55 localhost.localdomain systemd[1]: Starting MySQL Server...
Mar 09 18:31:05 localhost.localdomain systemd[1]: Started MySQL Server.

 

查找密码,修改密码:

[root@localhost ~]#  cat /var/log/mysqld.log | grep password
2021-03-10T02:30:37.846302Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ZW0P/.*3cIr.
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23

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> 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; Query OK, 0 rows affected (0.01 sec)

 

远程登陆授权:

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

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> 
mysql> create user 'root'@'%' identified with mysql_native_password by 'root';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.05 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> 

 

可以正常登陆 查询使用了:

[root@localhost ~]# mysql -uroot -proot
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 10
Server version: 8.0.23 MySQL Community Server - GPL

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

mysql> 

 

 

修改mysql数据文件位置:

[root@localhost lib]# systemctl stop mysqld.service;        

 

 

 

[root@localhost lib]#  cp  -r  /var/lib/mysql    /data
[root@localhost lib]#  mv mysql  mysql.bak.`date +%F`
[root@localhost lib]#  ln -s  /data/mysql/    /var/lib/mysql   

 

 

 

 

 

[root@localhost lib]# systemctl start mysqld.service;           
[root@localhost lib]# ps -ef|grep mysql
mysql     11199      1  0 00:08 ?        00:00:02 /usr/sbin/mysqld
root      11296   8939  0 00:09 pts/2    00:00:00 mysql -uroot -px xx
root      11514   8800  0 00:22 pts/0    00:00:00 grep --color=auto mysql
[root@localhost lib]# systemctl status mysqld.service;        
鈼[0m mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-03-10 00:08:09 PST; 14min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 11199 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           鈹斺攢11199 /usr/sbin/mysqld

Mar 10 00:08:07 localhost.localdomain systemd[1]: Starting MySQL Server...
Mar 10 00:08:09 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost lib]#