[MySQL] 在CentOS8.2上安裝MySQL8.0 與更改root密碼

MySQL 應該是使用 PHP 第一個會碰到的資料庫,通常會包在一起來教學。現在就來教如何在 CentOS8 上來單獨安裝 MySQL 吧!

(環境:CentOS8.2)
  1. 在 CentOS8 以後「yum」漸漸的會被「dnf」來取代,因此都會以「dnf」來做套件安裝移除的動作。
  2. 開啟 Terminal,安裝 MySQL
    [user@localhost ~]# sudo dnf install -y mysql-server
    ...
    ...
    ...
    ...
    Installed:
      mariadb-connector-c-config-3.0.7-1.el8.noarch                         mecab-0.996-1.module_el8.0.0+41+ca30bab6.9.x86_64                     mysql-8.0.17-3.module_el8.0.0+181+899d6349.x86_64                    
      mysql-common-8.0.17-3.module_el8.0.0+181+899d6349.x86_64              mysql-errmsg-8.0.17-3.module_el8.0.0+181+899d6349.x86_64              mysql-server-8.0.17-3.module_el8.0.0+181+899d6349.x86_64             
      protobuf-lite-3.5.0-7.el8.x86_64                                     
    
    Complete! #看到這行就代表安裝完成囉
    
  3. CentOS中的服務安裝好後預設都是不開啟的,所以要手動開啟,看到綠色 running 就代表 MySQL 正在運作囉!
    [user@localhost ~]$ sudo systemctl start mysqld.service   #馬上開啟mysql daeman(MySQL服務)
    
    [user@localhost ~]$ sudo systemctl enable mysqld.service  #之後開機也要自動開啟MySQL服務
    Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
    
    [user@localhost ~]$ sudo systemctl status mysqld.service  #查詢MySQL服務的狀態
     mysqld.service - MySQL 8.0 database server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2020-09-15 12:56:47 CST; 1min 37s ago
     Main PID: 8171 (mysqld)
       Status: "Server is operational"
        Tasks: 38 (limit: 49448)
       Memory: 376.2M
       CGroup: /system.slice/mysqld.service
               └─8171 /usr/libexec/mysqld --basedir=/usr
    
    Sep 15 12:56:46 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server...
    Sep 15 12:56:47 localhost.localdomain systemd[1]: Started MySQL 8.0 database server.
    
  4. 可以用「mysql_secure_installation」來設定與安全性相關的設定,這個設定工具可以幫助管理者設定 root 密碼、移除匿名登入帳號、禁止 root 從遠端登入、移除測試用的資料庫。若採用此步驟,可以跳過下一步
    [user@localhost ~]$ sudo mysql_secure_installation
    
  5. root 帳號的密碼預設是無,所以登入後一定要改密碼!
    [user@localhost ~]$ mysql -u root   #←不用密碼所以不用參數 -p
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 10
    Server version: 8.0.17 Source distribution
    
    Copyright (c) 2000, 2019, 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>ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';  #password改為要設定的密碼
    Query OK, 0 rows affected (0.00 sec)
    
  6. 用密碼登入 MySQL ,看到「Welcome to the MySQL monitor.」就成功囉
    [user@localhost ~]$ mysql -u root -p
    Enter password:        #←這裡輸入剛設定的密碼
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    ...
    ...
    
若是之前有裝過 MySQL,不記得 root 的密碼,請參考(HOW TO REMOVE MYSQL ENTIRELY FROM LINUX SYSTEM(CENTOS))先清除 MySQL 舊的資料再安裝,預設密碼就會為空。

留言

這個網誌中的熱門文章