當前使用的user及host: 添加用戶 mysql5以前版本直接使用 INSERT 向 mysql 表中插入mysql用戶了,mysql5之後不可以這樣操作 增加用戶 {授予用戶指定數據表許可權 [使用 GRANT 命令 對用戶進行相應授權]} IDENTIFIED BY 指定用戶的登錄密碼 ALL ...
當前使用的user及host:
mysql> select USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
添加用戶
mysql5以前版本直接使用 INSERT 向 mysql 表中插入mysql用戶了,mysql5之後不可以這樣操作
mysql> insert into mysql.user(Host,User,Password) values('localhost','test_user',password('123123'));
ERROR 1062 (23000): Duplicate entry 'localhost-test_user' for key 'PRIMARY'
增加用戶 {授予用戶指定數據表許可權 [使用 GRANT 命令 對用戶進行相應授權]}
mysql> GRANT all privileges ON table1.* TO
'test_user'@'localhost' IDENTIFIED BY '123123' WITH GRANT OPTION;
Query OK, 0 rows affected (0.02 sec)
IDENTIFIED BY 指定用戶的登錄密碼
ALL PRIVILEGES 是表示所有許可權,也可以使用 select、update 等許可權
*.\ 中前面的*號用來指定資料庫名,後面的*號用來指定表名
TO 表示將許可權賦予某個用戶
ON 用來指定許可權針對哪些庫和表
'test_user'@'localhost' 表示test_user用戶,@後面接限制的主機,可以是IP、IP段、功能變數名稱以及%,%表示任何地方
WITH GRANT OPTION 這個選項表示該用戶可以將自己擁有的許可權授權給別人
需要刷新系統許可權表[flush privilege] 該用戶才能生效登錄
mysql> flush privileges;
刪除用戶
mysql> drop user 'test_user'@'localhost';
查看當前用戶的許可權
mysql> SHOW GRANTS;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '\*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
查看某個用戶的許可權
mysql> show grants for 'test_user'@'localhost'
+------------------------------------------------------------------------------------------------------------+
| Grants for test_user@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test_user'@'localhost' IDENTIFIED BY PASSWORD '\*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1' |
| GRANT ALL PRIVILEGES ON table1.* TO 'test_user'@'localhost' WITH GRANT OPTION |
+------------------------------------------------------------------------------------------------------------+
對賬戶重命名
mysql> rename user 'test_user'@'localhost' to 'bb'@'localhost';
修改密碼
1.用set password命令
mysql> SET PASSWORD FOR 'test_user'@'localhost' = PASSWORD('123456');
2.用 mysqladmin [進入bin目錄]
備註:{格式: mysqladmin -u用戶名 -p舊密碼 password 新密碼]
/usr/bin$ mysqladmin -utest_user -p123456 password 123123
mysqladmin: Can't turn off logging; error: 'Access denied; you need (at least one of) the SUPER privilege(s) for this operation'
3.用 update 直接編輯 user 表
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set PASSWORD = PASSWORD('123123') where user = 'test_user';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0