先決條件
確保您以具有sudo特權的用戶身份登錄。
在Ubuntu上安裝MySQL
在撰寫本文時,Ubuntu存儲庫中可用的MySQL的最新版本是MySQL 8.0。要安裝它,請運行以下命令:
sudo apt update
sudo apt install mysql-server
安裝完成后,MySQL服務將自動啟動。要驗證MySQL服務器正在運行,請輸入:
sudo systemctl status mysql
輸出應顯示該服務已啟用並正在運行:
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-04-28 20:59:52 UTC; 10min ago
Main PID: 8617 (mysqld)
Status: "Server is operational"
...
保護MySQL
MySQL安裝隨附一個名為的腳本mysql_secure_installation
,可讓您輕松提高數據庫服務器的安全性。
調用不帶參數的腳本:
sudo mysql_secure_installation
系統將要求您配置VALIDATE PASSWORD PLUGIN
用來測試MySQL用戶密碼強度並提高安全性的密碼:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
密碼驗證策略分為三個級別:低,中和強。按下y
如果你想設置的驗證密碼插件或任何其他鍵移動到下一個步驟:
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
在下一個提示符下,將要求您設置MySQL root用戶的密碼:
Please set the password for root here.
New password:
Re-enter new password:
如果您設置了驗證密碼插件,該腳本將向您顯示新密碼的強度。鍵入y
以確認密碼:
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
接下來,將要求您刪除匿名用戶,限制root用戶對本地計算機的訪問,刪除測試數據庫並重新加載特權表。您應該回答y
所有問題。
以root身份登錄
要從命令行與MySQL服務器進行交互,請使用MySQL客戶端實用程序,該實用程序是作為MySQL服務器軟件包的依賴項安裝的。
在MySQL 8.0上,auth_socket
默認情況下,root用戶通過插件進行身份驗證。
該auth_socket
插件對localhost
通過Unix套接字文件從進行連接的用戶進行身份驗證。這意味着您不能通過提供密碼來以root用戶身份進行身份驗證。
要以root用戶身份登錄到MySQL服務器,請輸入:
sudo mysql
將為您提供MySQL Shell,如下所示:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19-0ubuntu5 (Ubuntu)
Copyright (c) 2000, 2020, 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>
如果要使用外部程序(例如phpMyAdmin)以root用戶身份登錄到MySQL服務器,則有兩個選擇。
第一個是將身份驗證方法從更改auth_socket
為mysql_native_password
。您可以通過運行以下命令來做到這一點:
mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
mysql > FLUSH PRIVILEGES;
推薦的第二個選項是創建一個新的專用管理用戶,該用戶可以訪問所有數據庫:
GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';
結論
我們已經向您展示了如何在Ubuntu 20.04上安裝MySQL。現在您的數據庫服務器已啟動並正在運行,下一步是學習如何管理MySQL用戶帳戶和數據庫。
如果您有任何疑問或反饋,請隨時發表評論。