MYSQL命令行下創建數據庫和用戶並授權

先以root用戶登錄mysql:

C:\Users\XXX>mysql -u root -p

輸入密碼後登錄,接下來操作如下:

1、創建數據庫

語法:create schema [數據庫名稱] default character set utf8 collate utf8_general_ci; 

  採用create schema和create database創建數據庫的效果一樣。

示例:create schema spring_boot_demo default character set utf8 collate utf8_general_ci;

2、創建用戶

語法:create user '[用戶名稱]'@'%' identified by '[用戶密碼]';

  密碼8位以上,包括:大寫字母、小寫字母、數字、特殊字符

  %:匹配所有主機,該地方還可以設置成‘localhost’,代表只能本地訪問,例如root賬戶默認爲‘localhost‘

示例:create user 'szh'@'localhost' identified by '123456';

3、用戶授權數據庫

grant select,insert,update,delete,create on [數據庫名稱].* to [用戶名稱]@'%';

  *代表整個數據庫

示例:grant select,insert,update,delete,create on spring_boot_demo.* to szh@'localhost';

4、立即啓用修改

flush  privileges ;

5、取消用戶szh所有數據庫(表)的所有權限

revoke all on *.* from szh;

6、刪除用戶szh

delete from mysql.user where user='szh';

7、刪除數據庫

drop database [schema名稱|數據庫名稱];

PS : 在操作過程中如果遇到錯誤

"The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement"

則先進行一下刷新操作:

mysql> flush privileges; 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章