MySQL經常使用命令基礎操做

MySQL啓動與更改密碼

mysql啓動基本原理說明:

/etc/init.d/mysqld是一個shell啓動腳本,啓動後最終會調用,mysqld_safe腳本,最後調用mysqld服務啓動mysql,咱們編輯/etc/init.d/mysqld,能夠看到腳本,啓動倆個進程mysqld和mysqld_safe,通常故障的時候咱們用mysqld_safe來啓動,mysql

關閉mysql

1 mysqladmin - uroot -p密碼 shut down
2 /etc/init.d/mysqld stop 3 kill USR2`cat path/pid`

優雅的關閉mysql可是不建議用killall殺掉全部的mysql進程,這樣會致使mysql數據庫起不來,因此網友遇到這樣狀況也不少,linux

咱們登錄mysql後想分清出那個是正式環境那個是測試環境,sql

命令行修改登錄提示符shell

mysql> prompt\u@king\s->
PROMPT set to '\u@king\s->'

配置文件修改登錄提示符數據庫

在my.cnf配置文件中[mysql]模塊下添加以下內容,保存後,無需重啓myysql,退出當前的session,從新登錄服務器

[mysql]
prompt=\\u@king\s->

登錄mysql

[root @king~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

更改root密碼

1 mysql> update user set password=password('123456') where user='root' and host='localhost';  #password('12345')是指定一個函數
2 Query OK, 1 row affected (0.00 sec)
3 Rows matched: 1  Changed: 1  Warnings: 0
4 mysql> flush privileges;              #刷新,沒刷新前是在內存裏面
5 Query OK, 0 rows affected (0.17 sec)
6 mysql>

說明,修改密碼都須要刷新一下哦,linux找回mysql root用戶密碼session

單實例mysql修改丟失root方法

1,首先中止mysqlide

/etc/init.d/mysql stop

2,使用--skip-grant-tables啓動mysql,忽略受權登錄驗證函數

 1 mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
 2 mysql -u root -p        #說明-p登錄時密碼是空,也能夠不加-p,親測哦(#^.^#)
 3 update mysql.user set password=password("newpassword") where user='root'and host='localhost'; 4 mysql> flush privileges; 5 mysql> quit 6 # /etc/init.d/mysql restart 7 # mysql -uroot -p 8 enter password: <輸入新設的密碼newpassword> 9 mysql>

多實例mysql修改丟失root方法

1關閉mysql測試

killall mysqld

2啓動時加--skip-grant-tables參數

mysql_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables& mysql -u root -p -S /data/3306/mysql.sock

3修改密碼

mysql>update mysql.user set password=password("newpassword") where user='root'and host='localhost';
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

SQl的分類

SQl結構化查詢語言包含6個部分

  1. 數據查詢語言(DQL(data query language)),做用從表中獲取數據,關鍵字select,
  2. 數據操做語言(DMl(data manipulation language))做用處理表中的數據insert ,update,delete
  3. 事務處理語言(TPL)關鍵字begin,commit和 rollback
  4. 數據控制語言(DCl)grant(受權) 和revoke
  5. 數據定義語言(DDl)createdrop在數據庫中建立新表或刪除表 alter
  6. 指針控制語言(CCl) declare cursor, fetch into 和update where current用於對一個或多個表單獨行的操做

查看數據庫

show databases;或show database link '';或select database;

 1 mysql> show databases;
 2 +--------------------+
 3 | Database           |
 4 +--------------------+
 5 | information_schema |
 6 | mysql              |
 7 | performance_schema |
 8 | student            |
 9 | test               |
10 +--------------------+
11 8 rows in set (0.01 sec)

建立數據庫

命令語法:create database <數據庫名> 注意庫名字不能數字開頭

 1 mysql> create database king;
 2 Query OK, 1 row affected (0.01 sec)
 3 
 4 mysql> show databases;
 5 +--------------------+
 6 | Database           |
 7 +--------------------+
 8 | information_schema |
 9 | king               |
10 | mysql              |
11 | performance_schema |
12 | student            |
13 | test               |
14 +--------------------+
15 6 rows in set (0.01 sec)
16 
17 mysql> show create  database king\G
18 *************************** 1. row ***************************
19        Database: king
20 Create Database: CREATE DATABASE `king` /*!40100 DEFAULT CHARACTER SET utf8 */
21 1 row in set (0.00 sec)
22 
23 mysql>

 建立一個指定字符集的數據庫

1 mysql> CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; #紅色指定編碼,藍色校驗規則。
2 Query OK, 1 row affected (0.01 sec)

刪除數據庫

drop  database <數據庫名字>

 1 mysql> drop database test;
 2 Query OK, 1 row affected (0.07 sec)
 3 
 4 mysql> show databases;
 5 +--------------------+
 6 | Database           |
 7 +--------------------+
 8 | information_schema |
 9 | king               |
10 | mysql              |
11 | performance_schema |
12 | student            |
13 +--------------------+
14 5 rows in set (0.01 sec)

 鏈接數據庫

命令:use <數據庫名>至關於linux下的cd切換目錄的命令,use是切換數據庫

 1 mysql> use king;
 2 Database changed
 3 mysql> select database();   #查看當前的數據庫,帶()至關於函數,
 4 +------------+
 5 | database() |
 6 +------------+
 7 | king       |
 8 +------------+
 9 1 row in set (0.00 sec)

查看數據庫

select database ();至關於linux下的pwd

 1 mysql> select version();    #查看當前的版本
 2 +-----------+
 3 | version() |
 4 +-----------+
 5 | 5.5.40    |
 6 +-----------+
 7 1 row in set (0.06 sec)
 8 mysql> select user();      #查看當前的用戶
 9 +----------------+
10 | user()         |
11 +----------------+
12 | root@localhost |
13 +----------------+
14 1 row in set (0.04 sec)
15 mysql> select now();      #查看當前的時間
16 +---------------------+
17 | now()               |
18 +---------------------+
19 | 2018-11-02 19:26:39 |
20 +---------------------+
21 1 row in set (0.02 sec)
22 mysql>

表操做

建立表並查看

create table <表名>(<字段名1><類型1>);

 1 mysql> create table im(id int(3) not null, name varchar(20) not null default'QQ'    #建立表id爲int類型,name爲varchar
 2 );
 3 Query OK, 0 rows affected (0.59 sec)
 4 mysql> show tables from king;              #從king數據庫中查看錶
 5 +----------------+
 6 | Tables_in_king |
 7 +----------------+
 8 | im |
 9 +----------------+
10 1 row in set (0.04 sec)
11 mysql> desc im;                    #查看錶結構
12 +-------+-------------+------+-----+---------+-------+
13 | Field | Type | Null | Key | Default | Extra |
14 +-------+-------------+------+-----+---------+-------+
15 | id | int(3) | NO | | NULL | |
16 | name | varchar(20) | NO | | QQ | |
17 +-------+-------------+------+-----+---------+-------+
18 2 rows in set (0.22 sec)
19 mysql>

show colums from im;查看錶結構

 

1 mysql> show create table im \G;        #\G Send command to mysql server,display result vertically向MySQL服務器發送命令,垂直顯示結果
2 *************************** 1. row ***************************
3        Table: im
4 Create Table: CREATE TABLE `im` (
5   `id` int(3) NOT NULL,
6   `name` varchar(20) NOT NULL DEFAULT 'QQ'
7 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
8 1 row in set (0.64 sec)

 

mysql表的字段類型

列類型 須要的存儲量
tinyint 1字節
smallint 2個字節
mediumint 3個字節
int 4個字節
integer 4個字節
bigint 8個字節
float(X) 4若是x<=24或8若是25<=X=53
float 4個字節
double 8個字節
double precision 8個字節
real 8個字節
decimal(M,D) M字節(D+2,若是m<D)
numeric(M,D) M字節(D+2,若是m<D)

爲表的字段建立索引

建立主鍵索引,查詢數據庫,按主鍵查詢是最快的,每個表只有一個主鍵列,可是能夠有多個普通的索引列,主鍵列要求列的內容必須惟一,而索引列不要求內容必須惟一。

create table student(
id int (4) not null auto_increment,
name char(20) not null,
age tinyint(2) not null default'0',
dept varchar(16) default null,
primary key (id),
key index_name(name));

1 mysql> create table student( id int (4) not null auto_increment, name char(20) n
2 ot null, age tinyint(2) not null default'0',dept varchar(16) default null,primar
3 y key (id), key index_name(name));
4 Query OK, 0 rows affected (0.18 sec)

auto_increment自增,primary key (id), 主鍵,key index_name(name));普通索引,

 1 mysql> desc student;        #查看建立的student表
 2 +-------+-------------+------+-----+---------+----------------+
 3 | Field | Type        | Null | Key | Default | Extra          |
 4 +-------+-------------+------+-----+---------+----------------+
 5 | id    | int(4)      | NO   | PRI | NULL    | auto_increment |
 6 | name  | char(20)    | NO   | MUL | NULL    |                |
 7 | age   | tinyint(2)  | NO   |     | 0       |                |
 8 | dept  | varchar(16) | YES  |     | NULL    |                |
 9 +-------+-------------+------+-----+---------+----------------+
10 4 rows in set (0.17 sec)

alter table student drop  primary key ;      刪除主鍵,測試不行,

1 mysql> alter table student drop  primary key ;
2 ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

               不正確的表定義;只能有一個自動COLUM它必須被定義爲一個密鑰

alter table student change id id int primary key auto_increment; 若是建立表忘記添加主鍵了,就執行這個。

建立普通索引分爲惟一索引和普通索引

alter table student drop index index_name; # index固定索引,index_name是你建立表的時候的索引

alter table student add index index_name(name); 

create index index_name on student (name(8)); # index固定索引(固定寫法),index_name 隨便寫但要見名知意, on student 在哪一個表上(student)8就是前8個字符建立索引。

create index index_name_dept on student (name,dept) 

按條件列查詢數據時,聯合索引是有前綴生效特性的 index(a,b,c)僅a,ab,abc三個查詢條件列能夠走索引

查看錶的索引

 1 mysql> show index from student\G;
 2 *************************** 1. row ***************************
 3         Table: student
 4    Non_unique: 0
 5      Key_name: PRIMARY
 6  Seq_in_index: 1
 7   Column_name: id
 8     Collation: A
 9   Cardinality: 0
10      Sub_part: NULL
11        Packed: NULL
12          Null:
13    Index_type: BTREE
14       Comment:
15 Index_comment:
16 *************************** 2. row ***************************
17         Table: student
18    Non_unique: 1
19      Key_name: index_name
20  Seq_in_index: 1
21   Column_name: name
22     Collation: A
23   Cardinality: 0
24      Sub_part: NULL
25        Packed: NULL
26          Null:
27    Index_type: BTREE
28       Comment:
29 Index_comment:
30 2 rows in set (0.00 sec)

建立惟一索引

create unique index uni on student(name);

 1 mysql> create unique index uni on student (name);
 2 Query OK, 0 rows affected (0.09 sec)
 3 Records: 0  Duplicates: 0  Warnings: 0
 4 mysql> desc student;
 5 +-------+-------------+------+-----+---------+----------------+
 6 | Field | Type        | Null | Key | Default | Extra          |
 7 +-------+-------------+------+-----+---------+----------------+
 8 | id    | int(4)      | NO   | PRI | NULL    | auto_increment |
 9 | name  | char(20)    | NO   | UNI | NULL    |                |
10 | age   | tinyint(2)  | NO   |     | 0       |                |
11 | dept  | varchar(16) | YES  |     | NULL    |                |
12 +-------+-------------+------+-----+---------+----------------+
13 4 rows in set (0.02 sec)

 

  1. 要在表的列上建立索引
  2. 索引會加快查詢的速度,可是會影響更新的速度
  3. 索引不是越多越好,要在頻繁查詢的where後的條件列上建立索引
  4. 小表或惟一值極少的列上不建索引,要在帶包以及不一樣內容多的列上建立索引

 

往表中插入數據

create table test(

id int(4) not null auto_increment,

name char(20) not null,

primary key (id)

);

 1 mysql> create table test(id int(4) not null auto_increment, name char(20) not n
 2 ll,primary key (id));
 3 Query OK, 0 rows affected (0.01 sec)
 4 mysql> show tables;
 5 +----------------+
 6 | Tables_in_king |
 7 +----------------+
 8 | student        |
 9 | test           |
10 +----------------+
11 2 rows in set (0.00 sec)
12 mysql> insert into test (id,name) values(1,'boy');  #插入數據,主鍵自增咱們直接插name,insert into test(name) values('new');
13 Query OK, 1 row affected (0.05 sec)
14 mysql> select *from test;
15 +----+------+
16 | id | name |
17 +----+------+
18 |  1 | boy  |
19 +----+------+
20 1 row in set (0.04 sec)
mysql> delete from test;
Query OK, 1 row affected (0.05 sec)
mysql> select *from test;
Empty set (0.00 sec)
mysql> insert into test (id,name) values(1,'boy'),(2,'new');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

查詢數據

select *from  表名;

1 mysql> select *from test;
2 +----+------+
3 | id | name |
4 +----+------+
5 |  1 | boy  |
6 |  2 | new  |
7 +----+------+
8 2 rows in set (0.47 sec)

指定條件查詢

1 mysql> select *from test where id =1;    #根據id查詢
2 +----+------+
3 | id | name |
4 +----+------+
5 |  1 | boy  |
6 +----+------+
7 1 row in set (0.09 sec)
1 mysql> select *from test where name='new';    #根據name查詢,字符串須要帶引號
2 +----+------+
3 | id | name |
4 +----+------+
5 |  2 | new  |
6 +----+------+
7 1 row in set (0.03 sec)

多個條件查詢and區交集,or取並集。排序 order by id desc(正序),sec(倒序)

1 mysql> select *from test where name='new'or id=1;
2 +----+------+
3 | id | name |
4 +----+------+
5 |  1 | boy  |
6 |  2 | new  |
7 +----+------+
8 2 rows in set (0.08 sec)

explain查看索引、

 1 mysql> explain select *from test where name='new'\G;
 2 *************************** 1. row ***************************
 3            id: 1
 4   select_type: SIMPLE
 5         table: test
 6          type: ALL
 7 possible_keys: NULL
 8           key: NULL
 9       key_len: NULL    #能夠看到索引爲空
10           ref: NULL
11          rows: 2
12         Extra: Using where
13 1 row in set (0.04 sec)

help explain;

修改表中指定條件固定列的數據

命令語法:update 表名 set 字段=新值,where 條件

 1 mysql> update test set name='wangxinxia' where id=2;
 2 Query OK, 1 row affected (0.90 sec)
 3 Rows matched: 1  Changed: 1  Warnings: 0
 4 mysql> desc test;
 5 +-------+----------+------+-----+---------+----------------+
 6 | Field | Type     | Null | Key | Default | Extra          |
 7 +-------+----------+------+-----+---------+----------------+
 8 | id    | int(4)   | NO   | PRI | NULL    | auto_increment |
 9 | name  | char(20) | NO   |     | NULL    |                |
10 +-------+----------+------+-----+---------+----------------+
11 2 rows in set (0.25 sec)
12 mysql> select* from test;
13 +----+------------+
14 | id | name       |
15 +----+------------+
16 |  1 | boy        |
17 |  2 | wangxinxia |
18 +----+------------+
19 2 rows in set (0.00 sec)

truncate  table test;和delete from test的區別

  1. truncate 更快,清空物理文件
  2. delete邏輯清楚,按行刪

刪除數據

 1 mysql> delete from student where id=3;
 2 Query OK, 1 row affected (0.30 sec)
 3 mysql> show tables;
 4 +----------------+
 5 | Tables_in_king |
 6 +----------------+
 7 | student        |
 8 | test           |
 9 +----------------+
10 2 rows in set (0.07 sec)
11 mysql> select * from student;
12 +----+----------+-----+-------+
13 | id | name     | age | dept  |
14 +----+----------+-----+-------+
15 |  1 | zhangsan |  20 | yanfa |
16 |  2 | zhaoliu  |  21 | dba   |
17 +----+----------+-----+-------+

增刪改表的字段

語法alter table 表名 add sex char(4); 

 1 mysql> alter table student add sex char (4);
 2 Query OK, 2 rows affected (0.54 sec)
 3 Records: 2  Duplicates: 0  Warnings: 0
 4 mysql> desc student;
 5 +-------+-------------+------+-----+---------+----------------+
 6 | Field | Type        | Null | Key | Default | Extra          |
 7 +-------+-------------+------+-----+---------+----------------+
 8 | id    | int(4)      | NO   | PRI | NULL    | auto_increment |
 9 | name  | char(20)    | NO   | UNI | NULL    |                |
10 | age   | tinyint(2)  | NO   |     | 0       |                |
11 | dept  | varchar(16) | YES  |     | NULL    |                |
12 | sex   | char(4)     | YES  |     | NULL    |                |
13 +-------+-------------+------+-----+---------+----------------+
14 5 rows in set (0.20 sec)

alter table 表名 add qq varchar(10)first;

 1 mysql> alter table student add  qq varchar(10) first;
 2 Query OK, 2 rows affected (0.06 sec)
 3 Records: 2  Duplicates: 0  Warnings: 0
 4 mysql> desc student;
 5 +-------+-------------+------+-----+---------+----------------+
 6 | Field | Type        | Null | Key | Default | Extra          |
 7 +-------+-------------+------+-----+---------+----------------+
 8 | qq    | varchar(10) | YES  |     | NULL    |                |
 9 | id    | int(4)      | NO   | PRI | NULL    | auto_increment |
10 | name  | char(20)    | NO   | UNI | NULL    |                |
11 | age   | tinyint(2)  | NO   |     | 0       |                |
12 | dept  | varchar(16) | YES  |     | NULL    |                |
13 | sex   | char(4)     | YES  |     | NULL    |                |
14 +-------+-------------+------+-----+---------+----------------+
15 6 rows in set (0.02 sec)

rename table 原表to新表

 1 mysql> show tables;
 2 +----------------+
 3 | Tables_in_king |
 4 +----------------+
 5 | student        |
 6 | test           |
 7 +----------------+
 8 2 rows in set (0.00 sec)
 9 mysql> rename table test to test1;
10 Query OK, 0 rows affected (0.05 sec)
11 
12 mysql> show tables;
13 +----------------+
14 | Tables_in_king |
15 +----------------+
16 | student        |
17 | test1          |
18 +----------------+
19 2 rows in set (0.00 sec)

alter table test1 rename to test;

mysql插入數據解決亂碼問題

  1. set names 庫的字符集,這種方法是臨時的每次進來以前都須要執行
  2. source test.sql在這個文件中插入set names 庫字符集
  3. 對已有數據,須要把數據導出去,從新建庫建表,在導進來。

建立/查看/刪除mysql系統的用戶

語法:

  1. create user '用戶名'@'主機名' identified by '用戶名';                   建立用戶
  2. select host,user from mysql.user;                                                     查看用戶
  3. drop user 用戶名@'%';注意能夠是單或者雙引號,但不能不加,刪除用戶
  4. delete from mysql.user where user='用戶名'  and host=‘@後面指定的主機名’;
 1 mysql> create user 'usrabc'@'%' identified by 'usrabc';  #建立usrabc用戶
 2 Query OK, 0 rows affected (0.49 sec)
 3 mysql> select host,user from mysql.user;         #查看全部用戶   
 4 +-----------+--------+
 5 | host | user |
 6 +-----------+--------+
 7 | % | usrabc |
 8 | localhost | root |
 9 +-----------+--------+
10 2 rows in set (0.00 sec)
11 mysql> drop user usrabc@'%';            #刪除usrabc用戶
12 Query OK, 0 rows affected (0.00 sec)

處理完用戶最好刷新一下權限,flush privileges;

建立mysql用戶及賦予用戶權限

經過help grant 查看幫助,比較經常使用的建立用戶的方法是:CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'

CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'
GRANT ALL ON db1.* TO 'jeffrey'@'localhost'          #這倆命令是先用create建立用戶,而後在受權
GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost'
GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR 90

經過grant命令建立用戶並受權,命令語法:grant all privileges on dbname.* to username@localhost identified by 'passwd';

grant all privileges on dbname.* to username@localhost identified by 'passwd'
受權命令 對應權限 目標:庫和表  用戶名和客戶端主機    用戶密碼
1 mysql> grant all privileges on king.* to liang@localhost identified by '123456';
2 #建立liang用戶,對king庫具有全部權限,容許從localhost主機登錄管理數據庫,密碼是123456.
3 Query OK, 0 rows affected (0.01 sec)
4 mysql> flush privileges;
5 Query OK, 0 rows affected (0.96 sec)
 1 mysql> show grants for liang@localhost;        #查看權限
 2 +-------------------------------------------------------------------------------
 3 -------------------------------+
 4 | Grants for liang@localhost
 5                                |
 6 +-------------------------------------------------------------------------------
 7 -------------------------------+
 8 | GRANT USAGE ON *.* TO 'liang'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74
 9 329105EE4568DDA7DC67ED2CA2AD9' |
10 | GRANT ALL PRIVILEGES ON `king`.* TO 'liang'@'localhost'
11                                |
12 +-------------------------------------------------------------------------------
13 -------------------------------+
14 2 rows in set (0.07 sec)

create和grant配合

1 mysql> create user zh@localhost identified by '123456';
2 Query OK, 0 rows affected (0.04 sec)
3 mysql> grant all on king.* to zh@localhost;
4 Query OK, 0 rows affected (0.00 sec)
5 mysql> flush privileges;
6 Query OK, 0 rows affected (0.00 sec)

受權局域網內主機遠程鏈接數據庫grant all privileges on king.* to liang@10.0.0.% identified by '123456';把localhost改成ip就能夠了。鏈接,mysql -uliang -p123456 -h10.0.0.% 

 1 mysql> show grants for zh@localhost;        #查看zh用戶權限
 2 +-------------------------------------------------------------------------------
 3 ----------------------------+
 4 | Grants for zh@localhost
 5 |
 6 +-------------------------------------------------------------------------------
 7 ----------------------------+
 8 | GRANT USAGE ON *.* TO 'zh'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329
 9 105EE4568DDA7DC67ED2CA2AD9' |
10 | GRANT ALL PRIVILEGES ON `king`.* TO 'zh'@'localhost'
11 |
12 +-------------------------------------------------------------------------------
13 ----------------------------+
14 2 rows in set (0.16 sec)
15 
16 mysql> revoke insert on king.* from zh@localhost;    #更改權限
17 Query OK, 0 rows affected (0.04 sec)
18 
19 mysql> show grants for zh@localhost;
20 +-------------------------------------------------------------------------------
21 --------------------------------------------------------------------------------
22 -------------------------------------------------------------+
23 | Grants for zh@localhost
24 
25 |
26 +-------------------------------------------------------------------------------
27 --------------------------------------------------------------------------------
28 -------------------------------------------------------------+
29 | GRANT USAGE ON *.* TO 'zh'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329
30 105EE4568DDA7DC67ED2CA2AD9'
31 |
32 | GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE T
33 EMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, A
34 LTER ROUTINE, EVENT, TRIGGER ON `king`.* TO 'zh'@'localhost' |
35 +-------------------------------------------------------------------------------
36 --------------------------------------------------------------------------------
37 -------------------------------------------------------------+
38 2 rows in set (0.06 sec)
1 mysql -uroot -p123456 -e "show grants for zh@localhost;"|grep -i grant  #用linux中grep命令過濾須要的權限
2 Grants for zh@localhost
3 GRANT USAGE ON *.* TO 'zh'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329
4 105EE4568DDA7DC67ED2CA2AD9'
5 GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE T
6 EMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, A
7 LTER ROUTINE, EVENT, TRIGGER ON `king`.* TO 'zh'@'localhost' 

 查看mysql的ALL PRIVILEGES權限mysql -uroot -p123456 -e "show grants for zh@localhost;"|grep -i grant|tail -1|tr ‘,’ ‘\n’  >a.txt     

 咱們在受權用戶最小的 知足業務需求的權限,而不是一味的受權ALl  PRIVILEGES。

相關文章
相關標籤/搜索