1. 程式人生 > 實用技巧 >MySql允許root遠端連線,以及使用者相關的幾個常用命令

MySql允許root遠端連線,以及使用者相關的幾個常用命令

有時候需要使用root使用者遠端連線資料庫,MySql預設是不允許root使用者遠端連線的,需要設定

1、檢視使用者和使用者的許可權

[mysql]> select user, authentication_string, password, host from user;
+---------+-----------------------+-------------------------------------------+---------------+
| user    | authentication_string | password                                  |
host | +---------+-----------------------+-------------------------------------------+---------------+ | root | | *B5363C68BA208552A8F8AB110809DC2483BACE05 | localhost | | root | | | 127.0.0.1 | |
root | | | ::1 | | | | | localhost | | sst | | *B5363C68BA208552A8F8AB110809DC2483BACE05 | % | | haproxy | |
| % | | root | | *B5363C68BA208552A8F8AB110809DC2483BACE05 | 10.110.30.170 | | sst | | *B5363C68BA208552A8F8AB110809DC2483BACE05 | localhost | +---------+-----------------------+-------------------------------------------+---------------+

可以看到上面的root使用者的host只有本機,只能本機訪問不能遠端訪問

2、配置遠端訪問許可權,登入資料庫後執行

# 配置root允許所有連線(%),密碼是XXXXX
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'XXXXX' WITH GRANT OPTION;

# 如果只方形某個IP,可以直接指定IP
GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.110.30.60' IDENTIFIED BY 'XXXXX' WITH GRANT OPTION;

# 重新整理使許可權生效
flush privileges;

# 再次檢視許可權,可以看到已經生效了
[mysql]> select user, authentication_string, password, host from user;
+---------+-----------------------+-------------------------------------------+---------------+
| user    | authentication_string | password                                  | host          |
+---------+-----------------------+-------------------------------------------+---------------+
| root    |                       | *B5363C68BA208552A8F8AB110809DC2483BACE05 | localhost     |
| root    |                       | *B5363C68BA208552A8F8AB110809DC2483BACE05 | %             |
| root    |                       |                                           | 127.0.0.1     |
| root    |                       |                                           | ::1           |
|         |                       |                                           | localhost     |
| sst     |                       | *B5363C68BA208552A8F8AB110809DC2483BACE05 | %             |
| haproxy |                       |                                           | %             |
| root    |                       | *B5363C68BA208552A8F8AB110809DC2483BACE05 | 10.110.30.170 |
| sst     |                       | *B5363C68BA208552A8F8AB110809DC2483BACE05 | localhost     |
| root    |                       | *03A59A80F51CC4F34A7939EABAB38AC65043E2EB | 10.110.30.60  |
+---------+-----------------------+-------------------------------------------+---------------+
10 rows in set (0.00 sec)

--------------------------------------------------------------------------------------

後來裝MariaDB10.0.21的時候,只執行上面的初始化密碼命令不太好使了,又加了一條更新authentication_string的操作,例如下面是將root的密碼改成xxxxx,測試是能成功的

如果執行上面的方法不好使,可以再嘗試一下這個

mysql -uroot -pxxxxx <<EOF
  USE mysql;
  UPDATE user SET authentication_string=password('xxxxx'),plugin='mysql_native_password' WHERE user='root';
  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxx' WITH GRANT OPTION;
  FLUSH PRIVILEGES;
EOF

使用者許可權相關的常用命令

## 1、建立db1資料庫
CREATE database if NOT EXISTS db1 default character set utf8 collate utf8_general_ci;

## 2、建立使用者
# 指定ip:10.110.30.2用testuser使用者登入
create user 'testuser'@'10.110.30.2' identified by 'xxxxx';
# 指定ip:192.118.1.開頭的用testuser使用者登入
create user 'testuser'@'10.110.30.%' identified by 'xxxxx';
# 指定任何ip用testuser使用者登入
create user 'testuser'@'%' identified by 'xxxxx';

## 3、刪除使用者
drop user '使用者名稱'@'IP地址';

## 4、修改密碼
set password for '使用者名稱'@'IP地址'=Password('新密碼');

## 5、檢視使用者許可權
show grants for '使用者'

## 6、授權testuser使用者僅對db1.t1有查詢、插入和更新的操作
grant select, insert, update on db1.t1 to 'testuser'@'%';

## 7、授權testuser使用者對db1資料庫中的檔案執行任何操作
grant all privileges on db1.* to 'testuser'@'%';

## 8、授權testuser使用者對所有資料庫中的檔案執行任何操作
grant all privileges on *.*  to 'testuser'@'%';
# 授權同時修改密碼
GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'%' IDENTIFIED BY 'xxxxxx' WITH GRANT OPTION;

## 9、取消testuser使用者對資料庫db1所有表的所有許可權
revoke all on db1.* from 'testuser'@"%"; 

## 10、取消testuser使用者對所有資料庫的所有許可權 
revoke all privileges on *.* from 'testuser'@'%';

testuser