前言 MySQL 應該是使用 PHP 第一個會碰到的資料庫,通常會包在一起來教學。記錄一下如何在 CentOS 8 單獨安裝 MySQL 吧!
環境
安裝與啟動 MySQL 在 CentOS 8 以後 yum
漸漸的會被 dnf
取代,因此都會以 dnf
來做套件安裝移除的動作。開啟 Terminal,安裝 MySQL
1 2 3 4 5 6 7 8 9 10 11 [user@localhost ~] ... ... ... ... 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!
CentOS 中的服務安裝後預設都是不開啟的,所以要手動開啟,看到 active (running)
就代表 MySQL 正在運作囉!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [user@localhost ~]$ sudo systemctl start mysqld.service [user@localhost ~]$ sudo systemctl enable mysqld.service Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service. [user@localhost ~]$ sudo systemctl status mysqld.service ● 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.
設定 root 密碼 可以使用 mysql_secure_installation
來設定與安全性相關的設定,這個設定工具透過互動式問答幫助管理者設定 root 密碼、移除匿名登入帳號、禁止 root 從遠端登入、移除測試用的資料庫。
1 [user@localhost ~]$ sudo mysql_secure_installation
root 帳號的密碼預設是無,為了安全起見,登入後一定要改密碼!先登入 MySQL
1 2 3 4 5 6 7 8 9 10 11 12 [user@localhost ~]$ mysql -u root 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.
使用 SQL 修改密碼
1 2 mysql> ALTER USER 'root' @'localhost' IDENTIFIED BY 'password' ; # password 改為要設定的密碼 Query OK, 0 rows affected (0.00 sec)
登入 MySQL 用密碼登入 MySQL ,看到 Welcome to the MySQL monitor.
就成功囉
1 2 3 4 5 [user@localhost ~]$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ... ...
忘記 root 密碼 若是之前有裝過 MySQL,現在不記得 root 的密碼,請參考 HOW TO REMOVE MYSQL ENTIRELY FROM LINUX SYSTEM(CENTOS) 先清除舊的 MySQL 資料再安裝,預設密碼就會為空。