預設使用者有root超級管理者,要做一個網站,要連接配接mysql要一個使用者名和密碼,不可能是root,防止誤操作。Mysql服務裡面可以跑多個庫,是以需要給單獨的使用者作一些授權,隻需要他對某一個資料庫或者某個資料庫的某個表有權限。
grant all on . to 'user1' identified by 'passwd'; // grant是授權的意思 all全部的
1.mysql> grant all on . to 'user1'@'127.0.0.1' identified by '123'; //授權user1隻能通過127這個ip登入mysql(源ip) identified by密碼 .前面這個表示庫名,後面是表。Ip也可以使用%表示所有的ip,
Query OK, 0 rows affected (0.63 sec)
2.[root@localhost ~]# mysql -uuser1 -p123 -h127.0.0.1 //使用者登入。如果授權ip是localhost那麼可以不用-h
3.mysql> grant all on db1.* to 'user1'@'192.168.222.%' identified by '1';
4.[root@localhost ~]# mysql -uuser1 -p1 -h192.168.222.51
5.grant SELECT,UPDATE,INSERT on db1. to 'user2'@'192.168.133.1' identified by 'passwd';
6.grant all on db1. to 'user3'@'%' identified by 'passwd';
7.show grants;//檢視目前使用者的授權
mysql> show grants; //檢視權限必須進入要查詢的使用者裡面
+-------------------------------------------------------------------------------+
| Grants for [email protected].% |
| GRANT USAGE ON . TO 'user1'@'192.168.222.%' IDENTIFIED BY PASSWORD <secret> |
| GRANT ALL PRIVILEGES ON
db1
.* TO 'user1'@'192.168.222.%' |
2 rows in set (0.00 sec)
mysql> select user();
+----------------------+
| user() |
1 row in set (0.00 sec)