Ubuntu之Mysql體驗
安裝
更新包索引
sudo apt-get update
安裝mysql
sudo apt-get install mysql-server
配置
初始化
sudo mysql_secure_installation
配置項
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的選項)
#2
Please set the password for root here...
New password: (輸入密碼)
Re-enter new password: (重複輸入)
#3
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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的選項)
#4
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) : Y (我的選項)
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的選項)
#6
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 (我的選項)
檢查mysql 服務狀態
systemctl status mysql.service
如果亮綠燈了,就表示成功了。
遠程登錄
開啓數據庫3306端口
netstat -an | grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
如果顯示如上,說明mysql端口目前只監聽本地連接127.0.0.1。然後需要修改mysql的配置文件
cd /etc/mysql/mysql.conf.d
sudo vi mysqld.cnf
// 將其中bind-address = 127.0.0.1註釋掉
遠程授權
低版本的
// 1.登錄
mysql -u username -p password
// 遠程授權
mysql> grant all on *.* to 'username'@'%' identified by 'password';
//說明:username爲你的mysql用戶名,password爲你的mysql密碼。
高版本的
// 1.登錄
mysql -u username -p password
// 2.創建一個新的用戶
mysql> create user 'username'@'%' identified by 'password' // 其中%是不限制ip
mysql> create user 'admin'@'%' identified by '123456'
// 但是遇到了問題,報錯:Your password does not satisfy the current policy requirements
// 具體可以參考 https://blog.csdn.net/ssiyla/article/details/82931439
// 解決:
mysql> show variables like "%validate%";
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 | // 密碼的長度
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM | // 修改爲LOW 或者 0
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
mysql> set global validate_password.length=6;
mysql> set global validate_password.policy=0;
之後就可以創建了新用戶了
// 3.設置權限
a.查看用戶
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+---------------------------------------+
| query |
+---------------------------------------+
| User: 'admin'@'%'; |
| User: 'debian-sys-maint'@'localhost'; |
| User: 'mysql.infoschema'@'localhost'; |
| User: 'mysql.session'@'localhost'; |
| User: 'mysql.sys'@'localhost'; |
| User: 'root'@'localhost'; |
+---------------------------------------+
6 rows in set (0.00 sec)
b.查看權限
mysql> show grants for 'admin'@'%';
+-----------------------------------+
| Grants for admin@% |
+-----------------------------------+
| GRANT USAGE ON *.* TO `admin`@`%` |
+-----------------------------------+
1 row in set (0.00 sec)
c.設置所有權限/單個權限
mysql> GRANT INSERT ON *.* TO `admin`@`%`;
mysql> GRANT ALL ON *.* TO `admin`@`%`;
重啓服務
/etc/init.d/mysql restart
防火牆中開啓3306端口
sudo ufw allow 3306
測試
用Navicat連接MySQL發現還是不行
// 具體可以參考 https://www.jianshu.com/p/dbeb0867e50f
報錯:1251-Client does not support authentication protocol requested by server;
解決方案:
// 1.登錄mysql
mysql -u username -p password
// 2.查看用戶配置項
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | admin | caching_sha2_password | $A$005$|nHG1lKf-0@!lW/E%FZtasASLvGat8TUfOIYjPJEvDeaghisn6.jNvWv9qy.7 |
| localhost | debian-sys-maint | caching_sha2_password | $A$005$aA|~MBm5WW1 CRFw9D2FDt1T6E4IraphM0FS6ngqOF1YkLhckKqQRn0 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | auth_socket | |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
6 rows in set (0.00 sec)
// 3.修改
mysql> ALTER USER 'admin'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
//4.再次查看
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | admin | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | debian-sys-maint | caching_sha2_password | $A$005$aA|~MBm5WW1 CRFw9D2FDt1T6E4IraphM0FS6ngqOF1YkLhckKqQRn0 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | auth_socket | |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
6 rows in set (0.01 sec)
數據庫操作
創建數據庫
CREATE DATABASE IF NOT EXISTS 數據庫名 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
創建表
use 數據庫名;
CREATE TABLE IF NOT EXISTS 表名(id int primary key auto_increment,name varchar(10) not null);