卸載
刪除mysql的數據文件 sudo rm /var/lib/mysql/ -R 刪除mysql的配置文件 sudo rm /etc/mysql/ -R 自動卸載mysql(包括server和client) sudo apt-get autoremove mysql* --purge sudo apt-get remove apparmor 然后在終端中查看MySQL的依賴項:dpkg --list|grep mysql
安裝
sudo apt update
sudo apt install mysql-server
安裝完成后,MySQL服務將自動啟動。要驗證MySQL服務器正在運行,請輸入:
sudo systemctl status mysql

安全操作
輸入root的密碼,然后一系列的配置
root@VM-0-2-ubuntu:/etc/mysql# sudo mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN 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 plugin? Press y|Y for Yes, any other key for No: 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: root Invalid option provided. 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: 12345678 Please set the password for root here. New password: Re-enter new password: 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 By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n ... skipping. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!
登錄
sudo mysql
或者
mysql -u root -p

創建用戶
命令: CREATE USER ‘username’@’host’ IDENTIFIED BY ‘password’; 說明: username:你將創建的用戶名 host:指定該用戶在哪個主機上可以登陸,如果是本地用戶可用localhost,如果想讓該用戶可以從任意遠程主機登陸,可以使用通配符% password:該用戶的登陸密碼,密碼可以為空,如果為空則該用戶可以不需要密碼登陸服務器 例子: CREATE USER ‘dog’@’localhost’ IDENTIFIED BY ‘123456’; CREATE USER ‘pig’@’192.168.1.101_’ IDENDIFIED BY ‘123456’; CREATE USER ‘pig’@’%’ IDENTIFIED BY ‘123456’; CREATE USER ‘pig’@’%’ IDENTIFIED BY ”; CREATE USER ‘pig’@’%’;
但是報錯,因為上述代碼的引號是中文字符,抄作業的后果

// 成功代碼 CREATE USER 'dimp'@'%' IDENTIFIED BY '12345678';
查看用戶
SELECT User,Host FROM mysql.user;

為用戶授權
//授予userone全部數據庫權限,並修改密碼 grant all on *.* to 'dimp'@'%' identified by '12345678';
其他命令
// 查看密碼策略 SHOW VARIABLES LIKE 'validate_password%' // 設置密碼強度為low set global validate_password_policy=LOW;
關於 mysql 密碼策略相關參數; 1)validate_password_length 固定密碼的總長度; 2)validate_password_dictionary_file 指定密碼驗證的文件路徑; 3)validate_password_mixed_case_count 整個密碼中至少要包含大/小寫字母的總個數; 4)validate_password_number_count 整個密碼中至少要包含阿拉伯數字的個數; 5)validate_password_policy 指定密碼的強度驗證等級,默認為 MEDIUM; 關於 validate_password_policy 的取值: 0/LOW:只驗證長度; 1/MEDIUM:驗證長度、數字、大小寫、特殊字符; 2/STRONG:驗證長度、數字、大小寫、特殊字符、字典文件; 6)validate_password_special_char_count 整個密碼中至少要包含特殊字符的個數

查看端口
show global variables like 'port';

報錯:sh: 0: getcwd() failed: No such file or directory
在執行命令前加 cd ~

