MySQL的用戶賬號:
-
由兩部分組成:用戶名和主機名
-
格式:'user_name'@'host' host必須要用引號括起來
注意:host可以是一個主機名也可以是具體的ip地址、網段等,
當host為主機名時:
#例如:
user1@'web1.redhat.org'
當host是ip地址或者網段時:
#例如:
[email protected].%.%
nacy@'192.168.1.%'
bob@'10.0.0.0/255.255.0.0'
創建用戶:
create user 'user_name'@'host' [IDENTIFIED BY 'password']
注意:
host必須用引號括起來,user_name部分可以忽略引號
創建的用戶默認只有登錄資料庫的權限,
創建用戶的時候指定密碼:create user user_name identified by 'password'
修改用戶名:
rename user old_name to new_name;
洗掉用戶:
drop user 'user_name'@'host'
修改用戶密碼:
注意:
-
新版mysql中用戶密碼可以保存在mysql.user表的authentication_string欄位中
-
如果mysql.user表的authentication_string和password欄位都保存密碼authentication_string優先生效
-
使用update來操作表修改密碼,需要使用FLUSH PRIVILEGES重繪權限才會生效
修改用戶密碼
檔案:https://dev.mysql.com/doc/refman/5.7/en/set-password.html
5.6版本修改密碼
mysql5.6的alter user命令只能用來修改用戶密碼的過期時間,
(1)update mysql.user set password=password('123456') where User="xxx" and Host = "xxx";
(2)set password for xxx@xxx = password('xxx');
5.7及以上版本
MySQL 5.7.6版本起,user表僅使用authentication_string列代替之前版本中的password列來存盤密碼
- 使用沒有的字串 PASSWORD()
SET PASSWORD FOR 'jeffrey'@'localhost' = 'password';
- 使用PASSWORD()函式(pawword()在 MySQL 5.7 中已棄用,在8.0洗掉了這個函式)
SET PASSWORD FOR 'jeffrey'@'localhost' = PASSWORD('password');
- 更改用戶密碼的首選陳述句:alter user
ALTER USER user IDENTIFIED BY 'auth_string';
忘記管理員密碼的解決辦法:
- 啟動mysqld行程時,為其使用如下選項:
--skip-grant-tables:不做權限和賬號驗證,
--skip-networking:禁止遠程連接(空密碼遠程連接上去危險性大),自己能連上因為不走埠號,走的是socket檔案
-
使用UPDATE命令修改管理員密碼
-
關閉mysqld行程,移除上述兩個選項,重啟mysqld
范例:Mariadb 和MySQL5.6版之前破解root密碼
#修改組態檔
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
skip-grant-tables
skip-networking
#重啟mysql
[root@centos8 ~]#systemctl restart mysqld|mariadb
#進入MySQL修改root密碼
[root@centos8 ~]#mysql #連接到MySQL
#方法1
#mariadb 舊版和MySQL5.6版之前
MariaDB [(none)]> update mysql.user set password=password('ubuntu') where user='root';
#mariadb 新版
MariaDB [(none)]> update mysql.user set authentication_string=password('ubuntu') where user='root';
#方法2
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> alter user root@'localhost' identified by 'ubuntu';
#注釋掉對應檔案
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
#skip-grant-tables
#skip-networking
#重啟mysql
[root@centos8 ~]#systemctl restart mysqld|mariadb
范例: MySQL5.7和8.0 破解root密碼
#修改組態檔
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
skip-grant-tables
skip-networking #MySQL8.0不需要
#重啟MySQL服務
[root@centos8 ~]#systemctl restart mysqld
#方法1
mysql> update mysql.user set authentication_string='' where user='root' and
host='localhost';
#方法2
mysql> flush privileges; #再執行下面任意一個命令
mysql> alter user root@'localhost' identified by 'ubuntu';
#進入MySQL后修改root密碼為新密碼
mysql> set password for root@'localhost'='ubuntu';
#注釋掉對應陳述句
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
#skip-grant-tables
#skip-networking
#重啟mysql
[root@centos8 ~]#systemctl restart mysqld
更加簡單破解密碼的方法
停止資料庫以后,洗掉掉mysql的所有資料,
重新啟動資料庫的時候就會重新初始化,相當于重裝系統,
注意:測驗環境測驗,
權限管理和DCL陳述句
注意:
新建用戶的默認權限:USAGE,僅僅能登錄資料庫系統
權限分類
資料庫、表、欄位、管理類、程式類
-
資料庫:對資料庫的相關操作權限
-
表:針對表的相關操作
-
管理類:查看資料庫、創建用戶等操作
-
程式類:針對函式、存盤程序、觸發器等的操作
-
所有權限:ALL PRIVILEGES 或 ALL
授權:grant .. to ..
grant 權限 on db.tb to user [WITH GRANT OPTION]
#*.*:表示所有資料庫和所有表
#WITH GRANT OPTION:允許把自己的權限授權給別人
例如:
mysql> grant all on *.* to tom@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for tom;
+------------------------------------------+
| Grants for tom@% |
+------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'tom'@'%' |
+------------------------------------------+
1 row in set (0.00 sec)
在授權的時候創建用戶(8.0已經洗掉這種語法)
grant privilege on db.tb to user identified by 'password'
例如:
mysql> grant all on *.* to bob@'%' identified by 'redhat';
Query OK, 0 rows affected, 1 warning (0.00 sec)
取消權限:revoke .. from ..
revoke privilege on db.tb for user
例如:
mysql> revoke all on *.* from bob@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for bob@'%';
+---------------------------------+
| Grants for bob@% |
+---------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'%' |
+---------------------------------+
1 row in set (0.00 sec)
查看指定用戶的全權限:show grants for ..
例如:
mysql> show grants for tom@'%';
+---------------------------------+
| Grants for tom@% |
+---------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' |
+---------------------------------+
1 row in set (0.00 sec)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/506168.html
標籤:其他
上一篇:弱隔離級別 & 事務并發問題
下一篇:MySQL學習筆記
