MySQL5.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;