詳解Mysql-5.7使用者管理、授權控制、日誌管理以及解決資料庫亂碼問題

來源:互聯網
上載者:User

標籤:text   bae   for   def   日誌功能   super   視圖   單位   rev   

簡介

1、使用者管理
2、授權控制
3、日誌管理
4、解決資料亂碼問題

實驗環境
  • 系統內容:centos7.4
  • 宿主機IP地址:192.168.100.71
  • yum掛載目錄:/mnt/sr0
命令步驟一、使用者管理

[[email protected]_1 ~]# mysql -uroot -p123 #登陸Mysql

1、建立使用者

mysql> create user ‘t01‘@‘localhost‘ identified by ‘123‘;

#建立"t01"使用者,賦予密碼為"123",並且只允許在本機登陸

t01:#建立的使用者名稱稱

@:#代表分隔字元

localhost:#代表允許在哪台主機登陸,值可為網段,也可為"%"代表任意主機
identified by:#設定的密碼

2、查看建立使用者位置

mysql> show databases; #查看資料庫
mysql> use mysql; #進入mysql資料庫

3、重新命名使用者

mysql> rename user ‘t01‘@‘localhost‘ to ‘u01‘@‘192.168.100.0/24‘;#將"t01"使用者修改為"u01",並且將允許登陸的主機"localhost"主機改為"192.168.100.0/24"網段
mysql> select user,host,authentication_string from mysql.user; #查看user表

4、刪除使用者

mysql> drop user ‘u01‘@‘192.168.100.0/24‘; #刪除"u01"使用者
mysql> select user,host,authentication_string from mysql.user #再次查看user表

5、設定加密密碼

mysql> select password(‘123‘);

mysql> create user ‘luxiaofeng‘@‘localhost‘ identified by *23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘**; #重新建立使用者,將密碼複製添加進去

mysql> select user,host,authentication_string from mysql.user
mysql> select user,host,authentication_string from user;

6、修改密碼

mysql> set password for ‘luxiaofeng‘@‘localhost‘ =password(‘123321‘);#修改密碼為"123321"
mysql> update mysql.user set authentication_string=‘777777‘ where user=‘luxiaofeng‘; #密碼修改為"777777"

二、授權系統管理權限列表
許可權 權限等級 許可權說明
CREATE 資料庫、表或索引 建立資料庫、表或索引許可權
DROP 資料庫或表 刪除資料庫或表許可權
GRANT OPTION 資料庫、表或儲存的程式 賦予許可權選項
REFERENCES 資料庫或表
ALTER 更改表,比如添加欄位、索引等
DELETE 刪除資料許可權
INDEX 索引許可權
INSERT 插入許可權
SELECT 查詢許可權
UPDATE 更新許可權
CREATE VIEW 視圖 建立視圖許可權
SHOW VIEW 視圖 查看視圖許可權
ALTER ROUTINE 預存程序 更改預存程序許可權
CREATE ROUTINE 預存程序 建立預存程序許可權
EXECUTE 預存程序 執行預存程序許可權
FILE 伺服器主機上的檔案訪問 檔案存取權限
CREATE TEMPORARY TABLES 伺服器管理 建立暫存資料表許可權
LOCK TABLES 伺服器管理 鎖表許可權
CREATE USER 伺服器管理 建立使用者權限
PROCESS 伺服器管理 查看進程許可權
RELOAD 伺服器管理 執行flush-hosts, flush-logs, flush-privileges, flush-status, flush-tables, flush-threads, refresh, reload等命令的許可權
REPLICATION CLIENT 伺服器管理 複製許可權
REPLICATION SLAVE 伺服器管理 複製許可權
SHOW DATABASES 伺服器管理 查看資料庫許可權
SHUTDOWN 伺服器管理 關閉資料庫許可權
SUPER 伺服器管理 執行kill線程許可權
1、賦予許可權

mysql> grant all on . to ‘luxiaofeng‘@‘localhost‘ identified by ‘123‘;

解析:

all:所有許可權

*:第一個"*"代表對應的資料庫

*:第二個"*"代表對應的資料表

luxiaofeng:賦予許可權的使用者

@:分隔字元

localhost:允許在哪台主機上登陸

identified by:設定密碼

[[email protected]_1 ~]# mysql -u luxiaofeng -p123

2、查看許可權

mysql> show grants for ‘luxiaofeng‘@‘localhost‘;

3、撤銷許可權

[[email protected]_1 ~]# mysql -uroot -p123 #使用"root"身份進行登陸
mysql> revoke all on . from ‘luxiaofeng‘@‘localhost‘; #撤銷"luxiaofeng"使用者的所有許可權
mysql> show grants for ‘luxiaofeng‘@‘localhost‘;

三、日誌管理1、錯誤記錄檔包含了當mysqld啟動和停止時,以及伺服器在運行過程中發生任何錯誤時的相關資訊

[[email protected]_1 ~]# cd /usr/local/mysql/data/
[[email protected]_1 ~]# ls

[[email protected]_1 ~]# vim /etc/my.cnf #修改主設定檔

在[mysqld]選項中添以下參數:

log-error=/usr/local/mysql/data/mysql_error.log #指定日誌路徑

[[email protected]_1 data]# systemctl restart mysqld.service #重啟服務
[[email protected]_1 data]# ls

2、通用查詢日誌MySQL所有串連以及語句被記錄到的記錄檔。注意這種日誌就是用來記錄在MySQL上執行過的SQL語句,包括DDL和DML以及特殊命令如SET,而不僅僅是select語句。

[[email protected]_1 ~]# vim /etc/my.cnf

在[mysqld]選項中添以下參數:

general_log=ON #開啟通用查詢日誌
general_log_file=/usr/local/mysql/data/mysql_general.log #指定日誌路徑

[[email protected]_1 ~]# systemctl restart mysqld.service

[[email protected]_1 ~]# ls /usr/local/mysql/data/

3、二進位日誌包含了所有更新了資料或者已經潛在更新了資料的所有語句,記錄了資料的更改,其主要目的是在恢複資料時能夠最大可能地恢複資料庫。

[[email protected]_1 ~]# vim /etc/my.cnf

在[mysqld]選項中添以下參數:

log_bin=mysql-bin #開啟二進位日誌功能

[[email protected]_1 ~]# systemctl restart mysqld.service
[[email protected]_1 ~]# ls /usr/local/mysql/data/

mysql> create table student(id int not null primary key auto_increment,name varchar(10),score decimal(5,2)); #建立一張"student"表
mysql> insert into student(name,score) values(‘zhangsan‘,90); #添加資料
mysql> insert into student(name,score) values(‘lisi‘,95);
mysql> insert into student(name,score) values(‘wangwu‘,96);
mysql> quit;

[[email protected]_1 ~]# cd /usr/local/mysql/data/ #日誌目錄下
[[email protected]_1 data]# mysqlbinlog --no-defaults mysql-bin.000002 #查看二進位檔案

4、慢速查詢日誌記錄所有執行事件超過long_query_time秒的SQL語句。long_query_time:逾時可用於找到執行時間長的查詢,以用於最佳化。

[[email protected]_1 ~]# vim /etc/my.cnf

在[mysqld]選項中添以下參數:

slow_query_log=ON #開啟慢速查詢日誌
slow_query_log_file=/usr/local/mysql/data/mysql_slow.log #指定日誌路徑
long_query_time=5 #設定逾時時間,單位秒,超出設定時間,記為慢

[[email protected]_1 ~]# systemctl restart mysqld.service

[[email protected]_1 ~]# ls /usr/local/mysql/data/

[[email protected]_1 ~]# mysql -u root -p123 #登陸mysql
mysql> select sleep(6);

[[email protected]_1 ~]# cat /usr/local/mysql/data/mysql_slow.log

四、解決資料亂碼問題法一:修改設定檔法

[[email protected]_1 ~]# vim /etc/my.cnf

在[client]、[mysqld]中添加相應字元集:[client]

defaults-character-set=utf8

[msyqld]

character_set_server=utf8

法二:建立庫、表指定字元集

[[email protected]_1 ~]# mysql -u root -p123
mysql> create database auth default character set utf8 collate utf8_general_ci; #建立庫
mysql> use auth;
mysql> create table users(user_name CHAR(16) NOT NULL,user_passwd CHAR(48) DEFAULT ‘‘,PRIMARY KEY(user_name))ENGINE=InnoDB DEFAULT CHARSET=utf8; #建立表
mysql> desc users;

詳解Mysql-5.7使用者管理、授權控制、日誌管理以及解決資料庫亂碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.