1. 程式人生 > >Centos 7.4 通過 tar 包方式安裝 MySQL 8.0

Centos 7.4 通過 tar 包方式安裝 MySQL 8.0

MySQL 8.0 也推出一段時間了,整理一篇安裝教程

  1. CentOS7.4系統自帶mariadb

     [[email protected] etc]# rpm -qa|grep mariadb
     mariadb-libs-5.5.56-2.el7.x86_64
     [[email protected] etc]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
    

    部分部落格需要移除 /etc/my.cnf,經實踐,本要執行解除安裝之前/etc目錄下有my.cnf和my.cnf.d,執行解除安裝之後已經一起移除了。或者可以執行下面程式碼:

     [[email protected] etc]# rm my.cnf
    

    或檔案不存在,會提示

     [[email protected] etc]# rm my.cnf
     rm: cannot remove ‘my.cnf’: No such file or director
    
  2. 檢查mysql是否存在

     [[email protected] etc]# rpm -qa | grep mysql
     [[email protected] etc]# 
    

    沒有任何輸出表示未安裝

  3. 建立mysql組和使用者

    (1) 先檢查是否存在

     [[email protected]
    etc]# cat /etc/group | grep mysql [[email protected] etc]# cat /etc/passwd | grep mysql [[email protected] etc]#

    (2) 沒有輸出,則建立組和使用者

     [[email protected] etc]# groupadd mysql
     [[email protected] etc]# useradd -g mysql mysql
    
     [[email protected] etc]# passwd mysql
     Changing password for user mysql.
     New password: 
     Retype new password: 
     passwd: all authentication tokens updated successfully.
     [
    [email protected]
    etc]#
  4. 準備安裝檔案

    截圖比較多,附在文章最後,點選跳轉

  5. 解壓安裝

    (1) 把mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz檔案移動到/usr/local/mysql目錄下,假設檔案在/home/ftp目錄下

     [[email protected] etc]# mv /home/ftp /usr/local/mysql
    

    (3) 解壓

     [[email protected] etc]# cd /usr/local
     [[email protected] local]# tar -xvJf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz 
     mysql-8.0.13-linux-glibc2.12-x86_64/bin/myisam_ftdump
     mysql-8.0.13-linux-glibc2.12-x86_64/bin/myisamchk
     ----------
     #此處省略二百多行 mysql-8.0.13-linux-glibc2.12-x86_64...
     ----------
     mysql-8.0.13-linux-glibc2.12-x86_64/lib/libmysqlharness.so.1
     mysql-8.0.13-linux-glibc2.12-x86_64/lib/libmysqlrouter.so
     mysql-8.0.13-linux-glibc2.12-x86_64/lib/libmysqlrouter.so.1
     [[email protected] local]# mv mysql-8.0.13-linux-glibc2.12-x86_64 mysql
    
  6. 更改所屬的組和使用者

    注意每次執行的目錄

     [[email protected] mysql]# mkdir data
     [[email protected] mysql]# cd ..
     [[email protected] local]# chown -R mysql mysql
     [[email protected] local]# chgrp -R mysql mysql
    
  7. 準備/etc/my.cnf檔案

     [[email protected] mysql]# vim /etc/my.cnf
     [mysql]
     # 設定mysql客戶端預設字符集
     default-character-set=utf8mb4 
    
     [mysqld]
     # 設定3306埠
     port = 3306 
     # 設定mysql的安裝目錄
     basedir=/usr/local/mysql
     # 設定mysql資料庫的資料的存放目錄
     datadir=/usr/local/mysql/data
     # 允許最大連線數
     max_connections=200
     # 服務端預設編碼(資料庫級別)
     character-set-server=utf8mb4
     # 建立新表時將使用的預設儲存引擎
     default-storage-engine=INNODB 
     lower_case_table_names=1
     max_allowed_packet=16M
    
     [[email protected] mysql]# chown 777 /etc/my.cnf
    
  8. 執行初始化

     [[email protected] mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
     -bash: bin/mysql_install_db: No such file or directory
    

    切換到/usr/local/mysql/bin目錄下,發現確實沒有 mysql_install_db,改成 bin/mysqld --initialize

     [[email protected] mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
     bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
    

    又報錯,執行下面命令

     [[email protected] mysql]#  yum install -y libaio
     Loaded plugins: fastestmirror
     Loading mirror speeds from cached hostfile
     Resolving Dependencies
     --> Running transaction check
     ---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
     --> Finished Dependency Resolution
     ----------
     #此處省略二十多行輸出語句
     ----------
     Installed:
       libaio.x86_64 0:0.3.109-13.el7                                                                                                    
    
     Complete!
    

    再執行

     [[email protected] mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
     2018-11-21T07:30:42.888897Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 26596
     2018-11-21T07:30:48.222050Z 5 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: l*JsMq=uX4>k
     2018-11-21T07:30:49.788339Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server has completed
    

    再執行

     [[email protected] mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
     [[email protected] mysql]# chmod +x /etc/init.d/mysqld
    
  9. 配置環境變數

    在 /etc/profile 最後新增如下內容export PATH=$PATH:/usr/local/mysql/bin

     [[email protected] mysql]# vim /etc/profile
    
     ----------
     #省略N行配置資訊
     ----------
     unset i
     unset -f pathmunge
    
     #set mysql environment
     export PATH=$PATH:/usr/local/mysql/bin
    

    使配置檔案生效

     [[email protected] mysql]# source /etc/profile
    
  10. 啟動 MySQL 服務

    [[email protected] mysql]# /etc/init.d/mysqld start
    Starting MySQL.Logging to '/usr/local/mysql/data/iZ286t0wuf9Z.err'.
    .[  OK  ]
    [[email protected] mysql]# 
    
  11. 設定 MySQL 服務開機啟動

    此處根據參考文件用了 chkconfig 命令,但看到提示資訊之後才知道,Centos 5.7開始,

    在 Centos 5.7 中服務不在是用 service 這個命令來啟動與停止,也不再用 chkconfig 來設定開機啟動與否!在 centos7 中所有對服務的管理都集中到了 systemctl 當中; systemctl 不再是合之前一樣依賴 /etc/init.d/ 下的指令碼,它是通過配置檔案來完成對服務的管理的。(參考:蔣樂興的技術隨筆

    [[email protected] ~]# 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]'.
    
    aegis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    agentwatch      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
    

    此處暫且這樣採用 chkconfig 設定

    [[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld
    [[email protected] mysql]# chkconfig --add mysqld
    [[email protected] mysql]# chkconfig --list mysqld
    [[email protected] mysql]# chkconfig --level 35 mysqld on
    

    執行 add 操作之後,list 中就會有 mysqld 了

    [[email protected] ~]# 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]'.
    
    aegis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    agentwatch      0:off   1:off   2:on    3:on    4:on    5:on    6:off
    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
    
  12. 初始化 root 密碼

    並沒意料到此處有障礙,本人沒有找到初始密碼在哪裡,無奈選擇重置。然後重置比我預計的複雜一點,步驟太多,請參考另一篇部落格:MySQL 8.0 以上版本重置 root 使用者密碼

  13. 新增遠端訪問許可權

    在伺服器上可以登入 root 使用者,但遠端連連線會提示

    1130: host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server

    xxx.xxx.xxx.xxx 遠端客戶端的IP地址,即當前客戶端IP地址禁止訪問。

    [[email protected] ~]# 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.13 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    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> 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> select host,user from user;
    +-----------+------------------+
    | host      | user             |
    +-----------+------------------+
    | localhost | mysql.infoschema |
    | localhost | mysql.session    |
    | localhost | mysql.sys        |
    | localhost | root             |
    +-----------+------------------+
    4 rows in set (0.00 sec)
    
    mysql> 
    

    查詢結果顯示,root允許的IP地址為localhost,修改成%即可。當然還有更復雜的設定,比如指定IP地址,或者指定IP區間,後續會再更新。

    mysql> update user set host='%' where user='root';
    Query OK, 1 row affected (0.03 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> 
    

    到這裡就可以遠端登入了。

<span id="jump">附:步驟4. 準備安裝檔案的過程截圖</span> MySQL_01 MySQL_02 MySQL_03 MySQL_04 MySQL_05

先整理這麼多了,歡迎大家共同討論。