標籤:
一、修改使用者登入密碼:
mysql> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql> quit
Bye
[[email protected] ~]# mysqladmin -uroot -p password --修改使用者密碼
Enter password:
New password:
Confirm new password:
[[email protected] ~]# mysql -uroot -p --以新密碼登入
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
……
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.10 sec)
二、開啟使用者遠程登入許可權
--加-h參數遠程登入mysql資料庫提示如下錯誤
C:\Users\Administrator>mysql -uroot -p -h192.168.1.204
Enter password: *****
ERROR 1130 (HY000): Host ‘192.168.1.123‘ is not allowed to connect to this MySQL server
錯誤分析:主機‘192.168.1.123‘不允許串連到mysql資料庫(沒許可權)。
[[email protected] ~]# myslq -uroot -p --本地登入
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> select user,password,host from user; --查看使用者資訊,的確root帳號只允許本地登入。
+------+-------------------------------------------+-------------+
| user | password | host |
+------+-------------------------------------------+-------------+
| root | *76C1B58DE0F08B3169E76CEDD6DD814B32A36F78 | localhost |
| root | *76C1B58DE0F08B3169E76CEDD6DD814B32A36F78 | rhel204.com |
| root | *76C1B58DE0F08B3169E76CEDD6DD814B32A36F78 | 127.0.0.1 |
| root | *76C1B58DE0F08B3169E76CEDD6DD814B32A36F78 | ::1 |
+------+-------------------------------------------+-------------+
4 rows in set (0.00 sec)
mysql> grant all privileges on *.* to [email protected]‘%‘ identified by ‘rusky‘; --授權
Query OK, 0 rows affected (0.03 sec)
或:grant all on db1.* to [email protected]‘%‘; --授權使用者username1從任一用戶端遠程登入資料庫db1,並允許對庫db1做所有操作。
"%"表示任何主機都可以遠程登入到該伺服器上訪問。
*.*表示所有庫的所有對象。
如果要限制只有某台機器可以訪問,將%換成相應的IP即可,如:
GRANT ALL PRIVILEGES ON *.* TO [email protected]‘192.168.1.123’; --可省略IDENTIFIED BY ‘密碼‘;或with grant option;
mysql> flush privileges; --重新整理許可權
Query OK, 0 rows affected (0.04 sec)
C:\Users\Administrator>mysql -uroot -p -h192.168.1.204 --root帳號遠程登入
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
……
mysql>
三、建立使用者時就限制使用者的許可權:
create user ‘lxj‘@‘%‘ identified by ‘123123‘;
@後面參數指定該使用者在哪個主機上可以登陸,如果是本機使用者可用localhost, 如果想讓該使用者可以從任意遠程主機登陸,可以使用萬用字元%.
mysql使用者修改登入密碼及開啟遠程登入