CentOS 7 安裝MySQL 8.0詳細教程

本文在CentOS7通用二進制方式安裝MySQL 8.0.28

  1. 下載安裝包

    官方下載網(wǎng)址

    image.png
  2. 點擊Download 下載mysql-8.0.28-el7-x86_64.tar.gz

  3. 點擊No thanks, just start my download 直接下載

    也可以在CentOS7wget直接下載 [root@Centos ~]# wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.28-el7-x86_64.tar.gz

    [root@node1 ~]# wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.28-el7-x86_64.tar.gz
    --2023-11-22 11:31:25--  https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.28-el7-x86_64.tar.gz
    Resolving cdn.mysql.com (cdn.mysql.com)... 23.37.218.50, 2600:1417:4400:8ae::1d68, 2600:1417:4400:8b6::1d68
    Connecting to cdn.mysql.com (cdn.mysql.com)|23.37.218.50|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 833555286 (795M) [application/x-tar-gz]
    Saving to: ‘mysql-8.0.28-el7-x86_64.tar.gz’
    
    100%[==========================================================================>] 833,555,286 4.68MB/s   in 4m 7s  
    
    2023-11-22 11:35:33 (3.22 MB/s) - ‘mysql-8.0.28-el7-x86_64.tar.gz’ saved [833555286/833555286]
    
    
  4. 把下載好的mysql-8.0.28-el7-x86_64.tar.gz上傳至服務(wù)器

    上傳完成后就可以看到如下結(jié)果:

    [root@node1 ~]# ls
    mysql-8.0.28-el7-x86_64.tar.gz 
    
  5. 通過 rpm -qa | grep mariadb 命令查看 mariadb的安裝包

    [root@node1 ~]# rpm -qa | grep mariadb
    mariadb-libs-5.5.68-1.el7.x86_64
    
  6. 通過 rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps 命令裝卸 mariadb

    [root@node1 ~]# rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
    
  7. 通過 rpm -qa | grep mariadb 命令再次查看 mariadb 的安裝包

    [root@node1 ~]# rpm -qa | grep mariadb
    [root@node1 ~]# 
    #說明已經(jīng)卸載完成
    

    檢查mysql的rpm包,有的話全刪除
    rpm -qa |grep -i mysql # -i表示忽略大小寫
    rpm -e --nodeps ... # 依次挨個刪除
    刪除mysql相關(guān)的配置【my.cnf】和目錄【mysql】

    find / -name mysql # 查找mysql的命令
    rm -rf ... #刪除命令,依次挨個刪除
    
  8. 創(chuàng)建用戶和組

    #創(chuàng)建一個用戶組:mysql
    groupadd mysql
    #用戶 (用戶名/密碼)【創(chuàng)建一個系統(tǒng)用戶:mysql,指定用戶組為mysql】
    useradd -r -g mysql mysql
    #檢查用戶組、用戶是否存在
    groups mysql
    
    [root@node1 ~]# groupadd mysql
    [root@node1 ~]# useradd -r -g mysql mysql
    [root@node1 ~]# groups mysql
    mysql : mysql
    [root@node1 ~]# 
    
  9. 解壓mysql安裝包,并將解壓后生成的文件夾進行重命名

    [root@node1 ~]# ls
     mysql-8.0.28-el7-x86_64.tar.gz 
    [root@node1 ~]# tar -zxvf mysql-8.0.28-el7-x86_64.tar.gz 
    [root@node1 ~]# ls
    mysql-8.0.28-el7-x86_64   mysql-8.0.28-el7-x86_64.tar.gz
    [root@node1 ~]# mv mysql-8.0.28-el7-x86_64 mysql
    [root@node1 ~]# ls
    [root@node1 ~]# ls
    mysql  mysql-8.0.28-el7-x86_64.tar.g
    
  10. 在將mysql文件創(chuàng)建移至/usr/local目錄

[root@node1 ~]# mv mysql /usr/local/
[root@node1 ~]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
#說明已經(jīng)把mysql的文件全部移至/usr/local

/usr/local/mysql目錄下新增data目錄作為后續(xù)存放mysql數(shù)據(jù)用

[root@node1 ~]# mkdir /usr/local/mysql/data
  1. 設(shè)置權(quán)限

    [root@node1 local]# cd /usr/local/
    [root@node1 local]# chown -R mysql:mysql /usr/local/mysql/
    [root@node1 local]# 
    [root@node1 local]# chmod -R 755 /usr/local/mysql/
    
  2. 配置mysql環(huán)境變量

    [root@node1 local]# vim /etc/profile
    

    在打開的文件末尾增加如下的內(nèi)容。

    export MYSQL_HOME=/usr/local/mysql
    export PATH=$MYSQL_HOME/bin:$PATH
    

    使環(huán)境變量生效:

    [root@node1 local]# source /etc/profile
    
  3. 設(shè)置mysql配置文件my.cnf

    如存在默認/etc/my.cnf,先把默認/etc/my.cnf重命名或刪除

    CentOS7默認會有一個/etc/my.cnf文件,為了不影響安裝,可以直接刪除或重命名

    mv /etc/my.cnf /etc/my.cnf.bak 或  rm /etc/my.cnf
    
    [root@node1 local]# vim /etc/my.cnf
    

    /etc/my.cnf內(nèi)容如下:

    [mysql]
    # 設(shè)置mysql客戶端默認字符集
    default-character-set = utf8mb4
    
    [mysqld]
    #設(shè)置3306端口
    port = 3306  
    # 設(shè)置mysql的安裝目錄
    basedir = /usr/local/mysql
    # 設(shè)置mysql數(shù)據(jù)庫的數(shù)據(jù)的存放目錄
    datadir = /usr/local/mysql/data
    
    socket = /tmp/mysql.sock
    #日志
    log-error = /usr/local/mysql/data/mysql.log
    pid-file = /usr/local/mysql/data/mysql.pid
    # 允許最大連接數(shù)
    max_connections = 1000 
    #數(shù)據(jù)庫默認字符集, 主流字符集支持一些特殊表情符號(特殊表情符占用4個字節(jié))
    character-set-server = utf8mb4
    #數(shù)據(jù)庫字符集對應一些排序等規(guī)則,注意要和character-set-server對應
    collation-server = utf8mb4_general_ci
    #設(shè)置client連接mysql時的字符集,防止亂碼
    init_connect = 'SET NAMES utf8mb4'
    # 創(chuàng)建新表時將使用的默認存儲引擎
    default-storage-engine = INNODB
    lower_case_table_names = 1 
    max_allowed_packet = 16M
    
    [client]
    port  = 3306
    socket  = /tmp/mysql.sock
    default-character-set = utf8mb4
    
    
  4. 設(shè)置mysql服務(wù)為開機啟動

    創(chuàng)建軟鏈接

    [root@node1 local]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    

    賦予執(zhí)行權(quán)限

    [root@node1 local]# chmod +x /etc/init.d/mysqld 
    

    添加mysqld服務(wù)

    [root@node1 local]# chkconfig --add mysqld
    

    查看服務(wù)列表來驗證是否添加成功

    [root@node1 local]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
    netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
    network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    
  5. 初始化mysql

    [root@node1 local]# mysqld --initialize  --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
    
  6. 啟動mysql服務(wù)

    [root@node1 local]# systemctl start mysqld
    

    查看是否啟動

    [root@node1 local]# ps -ef | grep mysql
    #就看到mysqld進程即為已經(jīng)啟動
    
  7. 通過 cat /usr/local/mysql/data/mysql.log | grep password 命令查看數(shù)據(jù)庫的密碼

    image.png

    在該例中,密碼為cpJ,yAPV#4wx ,即可以通過該命令登錄mysql

  8. 通過 mysql -uroot -p 敲回車鍵進入數(shù)據(jù)庫登陸界面

[root@node1 data]# 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.28

Copyright (c) 2000, 2022, 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> 
  1. 通過 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'命令來修改密碼
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> exit;

在此修改密碼為123456為例,根據(jù)自身實際情況設(shè)置密碼

  1. 通過 exit; 命令退出 MySQL,然后通過新密碼再次登陸
mysql> exit;
Bye
[root@Centos mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.24 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> 
  1. 通過以下命令,進行遠程訪問的授權(quán)

    create user 'root'@'%' identified with mysql_native_password by '123456';
    grant all privileges on *.* to 'root'@'%' with grant option;
    flush privileges;
    
    
    mysql> create user 'root'@'%' identified with mysql_native_password by '123456';
    Query OK, 0 rows affected (0.01 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)
    
    mysql> 
    
  2. 通過 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;命令修改加密規(guī)則,MySql8.0 版本 和 5.0 的加密規(guī)則不一樣,而現(xiàn)在的可視化工具只支持舊的加密方式。

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;    
    Query OK, 0 rows affected (0.01 sec)
    
  3. 通過 flush privileges; 命令刷新修該后的權(quán)限

    mysql>  flush privileges; 
    Query OK, 0 rows affected (0.00 sec)
    
  4. 通過 exit; 命令退出 MySQL

    mysql> exit;
    Bye
    
  5. 自此就可以用Navicat鏈接數(shù)據(jù)庫,如果鏈接不成功就看一下檢查防火墻。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容