一 Mariadb常用管理操作

純乾貨,沒有一點廢話,全是使用頻率最高和常用的操作,運維必不可少的基礎資料。

1.1 建立資料庫

  1. >create database <db_name>; #快速建立資料庫
  2. ----------------------------------------------
  3. >create database <db_name> default character set utf8 collate utf8_general_ci; #建立資料庫並設定字符集為utf-8
  4. >show create database <db_name>; #檢視資料庫字符集
  5. 1 修改資料庫的字符集
  6. alter database <db_name> character set utf8;
  7. 2 修改表的字符集
  8. alter table <table_name> character set utf8;
  9. 3 修改欄位的字符集
  10. alter table <table_name> change <Field> <Field1> <Field2> character set utf8; #一般不會使用

1.2 刪除資料庫

  1. >drop database <db_name>; #刪除資料庫

1.3 使用資料庫

  1. >use <db_name>; #使用資料庫
  2. >select database(); #檢視當前連線的資料庫

1.4 建立使用者

建立登陸資料庫的使用者,以及登陸的IP限制等

  1. >create user 'test01'@'localhost' identified by 'password'; #只是建立一個使用者,沒有任何瀏覽資料庫的許可權
  2. ----------------------------------------------------------
  3. >grant all on test_db1.* to 'test02'@'localhost' identified by '123456'; #建立一個使用者'test02',並授權他可以對'test_db1'進行查詢,更新,更改,刪除操作,
  4. #'localhost'指的是隻能在本機才可以登陸
  5. select user from mysql.user\G #檢視Mysql內使用者,從'mysql庫的user表'裡查詢'user'欄位
  6. select user,host from mysql.user\G

1.5 刪除使用者

  1. drop user 'test01'@localhost

二 Mariadb資料庫的許可權管理

2.1 使用者連線資料庫許可權

  1. 1 只允許來自於本地連線資料庫
  2. grant all on test_db1.* to 'test02'@'localhost' identified by '123456'; #'localhost'代表只允許本地登陸
  3. ---------------------------------------------------------------
  4. 2 允許區域網本網段連線資料庫
  5. grant all on test_db1.* to 'test02'@'192.168.1.%' identified by '123456'; #'192.168.1.%'允許192.168.1.0網段主機連線
  6. ---------------------------------------------------------------
  7. 3 允許任意地址連線資料庫
  8. grant all on test_db1.* to 'test02'@'%' identified by '123456'; #'%'表示允許任意地址連線資料庫

2.2 使用者資料庫庫許可權

  1. #授權使用者在test_db1資料中擁有,查詢,更新,插入,刪除許可權
  2. > grant select,update,insert,delete on test_db1.* to 'test02'@'%' identified by '123456';
  • select,查詢許可權
  • update,更新許可權
  • insert,插入許可權
  • delete,刪除許可權

2.3 使用者許可權回收

  1. #把'test02'賬號的,插入和查詢許可權取消
  2. >revoke insert,select on test_db1.* from test02@'%';

說完基礎的資料庫許可權和操作,下一次所說跟表相關的內容