1. 程式人生 > >MySQL新增新使用者、為使用者建立資料庫、為新使用者分配許可權

MySQL新增新使用者、為使用者建立資料庫、為新使用者分配許可權

登入MySQL

[[email protected]_0_2_33_centos /]#mysql -u root -p

新增新使用者

允許本地 IP 訪問 localhost, 127.0.0.1

mysql>create user 'test'@'localhost' identified by '123456';

 允許外網 IP 訪問

mysql>create user 'test'@'%' identified by '123456';

重新整理授權

mysql>flush privileges;

為使用者建立資料庫

mysql>create
database test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

為新使用者分配許可權 授予使用者通過外網IP對於該資料庫的全部許可權

mysql>grant all privileges on `testdb`.* to 'test'@'%' identified by '123456';

授予使用者在本地伺服器對該資料庫的全部許可權 

mysql>grant all privileges on `testdb`.* to 'test'@'localhost' identified by '123456';

重新整理許可權

mysql>flush privileges;

退出 root 重新登入

mysql> exit;

用新帳號 test 重新登入,由於使用的是 % 任意IP連線,所以需要指定外部訪問IP

[[email protected]_0_2_33_centos /]# mysql -u test -h 115.28.203.224 -p 

在Ubuntu伺服器下,MySQL預設是隻允許本地登入,因此需要修改配置檔案將地址繫結給註釋掉:

   # Instead of skip-networking the default is now to listen only on

   # localhost which is more compatible and is not less secure.

  #bind-address = 127.0.0.1

  #註釋掉這一行就可以遠端登入了 不然會報如下錯誤:

   ERROR 2003 (HY000): Can't connect to MySQL server on 'host' (111)