Mariadb常用管理操作

一 Mariadb常用管理操作

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

1.1 創建數據庫

>create database <db_name>;      #快速創建數據庫
----------------------------------------------
>create database <db_name> default character set utf8 collate utf8_general_ci;       #創建數據庫並設置字符集為utf-8
>show create database <db_name>;   #查看數據庫字符集

1  修改數據庫的字符集
    alter database <db_name> character set utf8;
    
2  修改表的字符集
   alter table <table_name> character set utf8;
   
3  修改字段的字符集
   alter table <table_name> change <Field> <Field1> <Field2> character set utf8;   #一般不會使用

1.2 刪除數據庫

>drop database <db_name>;          #刪除數據庫

1.3 使用數據庫

>use <db_name>;           #使用數據庫
>select database();       #查看當前連接的數據庫

1.4 創建用戶

創建登陸數據庫的用戶,以及登陸的IP限制等

>create user 'test01'@'localhost' identified by 'password';       #只是創建一個用戶,沒有任何瀏覽數據庫的權限
----------------------------------------------------------

>grant all on test_db1.* to 'test02'@'localhost' identified by '123456';    #創建一個用戶'test02',並授權他可以對'test_db1'進行查詢,更新,更改,刪除操作,
#'localhost'指的是只能在本機才可以登陸

select user from mysql.user\G         #查看Mysql內用戶,從'mysql庫的user表'里查詢'user'字段
select user,host from mysql.user\G

1.5 刪除用戶

drop user 'test01'@localhost

二 Mariadb數據庫的權限管理

2.1 用戶連接數據庫權限

1 只允許來自於本地連接數據庫
    grant all on test_db1.* to 'test02'@'localhost' identified by '123456';     #'localhost'代表只允許本地登陸
---------------------------------------------------------------
2 允許局域網本網段連接數據庫
    grant all on test_db1.* to 'test02'@'192.168.1.%' identified by '123456';   #'192.168.1.%'允許192.168.1.0網段主機連接
---------------------------------------------------------------
3 允許任意地址連接數據庫
    grant all on test_db1.* to 'test02'@'%' identified by '123456';             #'%'表示允許任意地址連接數據庫    

2.2 用戶數據庫庫權限

#授權用戶在test_db1數據中擁有,查詢,更新,插入,刪除權限
> grant select,update,insert,delete on test_db1.* to 'test02'@'%' identified by '123456';  
  • select,查詢權限
  • update,更新權限
  • insert,插入權限
  • delete,刪除權限

2.3 用戶權限回收

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

說完基礎的數據庫權限和操作,下一次所說跟表相關的內容