1. 程式人生 > 其它 >mysql 建立資料庫 新增使用者 資料庫授權給某使用者

mysql 建立資料庫 新增使用者 資料庫授權給某使用者

一、建立mysql資料庫

1.建立資料庫語法

DROP DATABASE IF EXISTS `school_archives`;
CREATE DATABASE `school_archives` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `school_archives`;

推薦使用 utf8mb4 編碼。

二、建立使用者

1.新建使用者

 --建立了一個名為:test 密碼為:1234 的使用者
 create user 'test'@'localhost' identified by '1234';

注意:
此處的"localhost",是指該使用者只能在本地登入,不能在另外一臺機器上遠端登入。如果想遠端登入的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登入。也可以指定某臺機器可以遠端登入。

2.查詢使用者

--查詢使用者
select user,host from mysql.user;

3.刪除使用者

--刪除使用者“test”
drop user test@localhost ;
--若建立的使用者允許任何電腦登陸,刪除使用者如下
drop user test@'%';

4.更改密碼

--方法1,密碼實時更新;修改使用者“test”的密碼為“1122”
set password for test =password('1122');
--方法2,需要重新整理;修改使用者“test”的密碼為“1234”
update  mysql.user set  password=password('1234')  where user='test'
--重新整理
flush privileges;

5.使用者分配許可權

--授予使用者test通過外網IP對資料庫“testdb”的全部許可權
grant all privileges on 'testdb'.* to 'test'@'%' identified by '1234';  

--重新整理許可權
flush privileges; 

--授予使用者“test”通過外網IP對於該資料庫“testdb”中表的建立、修改、刪除許可權,以及表資料的增刪查改許可權
grant create,alter,drop,select,insert,update,delete on testdb.* to test@'%';	 

6.檢視使用者許可權

--檢視使用者“test”
show grants for test;

注意:修改完許可權以後 一定要重新整理服務,或者重啟服務,重新整理服務用:flush privileges;