MySQL5.7 常用用戶操作


MySQL5.7 常用用戶操作

之前的一篇博文講述了安裝MySQL,但是我們在安裝后MySQL之后的操作中一般不使用root用戶來進行相應的操作,所以要新建用戶,並賦予相應的權限后,才能更好的使用和管理數據庫。
mysql版本:5.7

1. 新建用戶

create user 'username'@'host' identified by 'password';

例子:

create user 'user'@'localhost' identified by '123456';
create user 'user'@'localhost' identified by '';

username : 你將創建的用戶名
host : 指定該用戶在哪個主機上可以登陸,此處的"localhost",是指該用戶只能在本地登錄,不能在另外一台機器上遠程登錄,如果想遠程登錄的話,將"localhost"改為"%",表示在任何一台電腦上都可以登錄;也可以指定某台機器可以遠程登錄;
password : 該用戶的登陸密碼,密碼可以為空,如果為空則該用戶可以不需要密碼登陸服務器。

MySQL5.7 mysql.user表password字段改為 authentication_string;

2. 授權

grant privileges on databasename.tablename to 'username'@'host';

privileges : 用戶的操作權限,如SELECT , INSERT , UPDATE 等。如果要授予所的權限則使用ALL。
databasename : 數據庫名
tablename : 表名
如果要授予該用戶對所有數據庫和表的相應操作權限則可用*表示, 如*.*.

## 賦予user對數據庫store下的所有表的查看和增添權利
grant select, insert on store.* to 'user'@'localhost';

3. 創建用戶時授權

grant all privileges on store.* to user@'%' identified by '123456';
flush privileges;

4. 設置與更改用戶密碼(root)

update mysql.user set authentication_string=password("新密碼") where User="test" and Host="localhost"; 
flush privileges;

5. 撤銷用戶權限

## 具體信息可以用命令show grants for 'username'@'host'; 查看.
revoke privilege on databasename.tablename from 'username'@'host';

6. 刪除用戶

drop user 'username'@'host';

7. 查看用戶的授權

show grants for user@localhost;

8. 顯示當前用戶信息

select user();

9. 重置root密碼

  在my.cnf的[mysqld]字段加入skip-grant-tables,然后重啟mysql服務,這時的mysql不需要密碼即可登錄數據庫。然后進入mysql:

use mysql;
update user set password=password('新密碼') WHERE User='root';
flush privileges;

  運行之后最后去掉my.cnf中的skip-grant-tables,重啟mysqld即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM