【轉】MySQL使用者管理及SQL語句詳解
1.1 MySQL使用者管理
1.1.1 使用者的定義
使用者名稱+主機域
mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+-------------------------------------------+| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | 127.0.0.1 | | | znix | 172.16.1.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | clsn | 172.16.1.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | 10.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |+--------+------------+-------------------------------------------+ 6 rows in set
1.1.2 使用者的作用
1、使用者登入
2、用於管理資料庫及資料
1.1.3 連線資料庫
定義使用者:使用者名稱+主機域,密碼
定義許可權:對不同的物件進行許可權(角色)定義
命令:
grant 許可權 on 許可權範圍 to 使用者 identified by '密碼'
許可權
對資料庫的讀、寫等操作 (insert update、select、delete、drop、create等)
角色
資料庫定義好的一組許可權的定義 (all privileges、replication slave等)
許可權範圍
全庫級別: *.* 單庫級別:clsn.* 單表級別:clsn.t1
使用者
'clsn'@'localhost' 本地 'clsn'@'192.168.66.149' 'clsn'@'192.168.66.%' 'clsn'@'192.168.66.14%'
1.1.4 【練習題】按照要求建立使用者
使用者只能通過10.0.0.0/24網段訪問,使用者名稱為clsn 密碼為123
這個使用者只能對clsn資料庫下的物件進行增insert create、改update 、查select;
建立命令:
grant select,create,insert,update on clsn.* to 'clsn'@'10.0.0.%' identified by '123';
檢視使用者許可權
mysql> show grants for [email protected]'172.16.1.%'\G *************************** 1. row *************************** Grants for clsn@172.16.1.%: GRANT USAGE ON *.* TO 'clsn'@'172.16.1.%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' 1 row in set (0.00 sec)
檢視當前存在的使用者:
select user,host from mysql.user;
建立使用者語法
CREATE USER '使用者'@'主機' IDENTIFIED BY '密碼';
示例:
create user 'clsn'@'localhost' identified by 'clsn123';
注意這個樣建立的使用者只有連線許可權
企業裡建立使用者一般是授權一個內網網段登入,最常見的網段寫法有兩種。
方法1:172.16.1.%(%為萬用字元,匹配所有內容)。
方法2:172.16.1.0/255.255.255.0,但是不能使用172.16.1.0/24,是個小遺憾。
標準的建使用者方法:
create user 'web'@'172.16.1.%' identified by 'web123';
檢視使用者對應的許可權
show grants for oldboy@localhost\G
1.1.5 使用者刪除
刪除使用者語法:
drop user 'user'@'主機域'
【練習】使用者優化:只保留
| root | 127.0.0.1 | | root | localhost |
特殊的刪除方法:(慎用,儘量不要直接去修改表)
mysql> delete from mysql.user where user='clsn' and host='localhost'; Query OK, 1 row affected (0.00 sec) mysql> flush privileges;
1.1.6 使用者授權
給使用者授權
# 建立使用者 create user 'clsn'@'localhost' identified by 'clsn123'; # 檢視使用者 select user,host from mysql.user; # 授權所有許可權給clsn使用者 GRANT ALL ON *.* TO 'clsn'@'localhost'; # 檢視clsn使用者的許可權 SHOW GRANTS FOR 'clsn'@'localhost'\G
建立使用者的同時授權
grant all on *.* to [email protected]'172.16.1.%' identified by 'clsn123'; # 重新整理許可權 flush privileges; #<==可以不用。
建立使用者然後授權
create user 'clsn'@'localhost' identified by 'clsn123'; GRANT ALL ON *.* TO 'clsn'@'localhost';
授權和root一樣的許可權
grant all on *.* to [email protected]'localhost' identified by 'clsn123' with grant option;
授權給使用者select,create,insert,update 許可權
grant select,create,insert,update on clsn.* to 'clsn'@'10.0.0.%' identified by '123';
回收許可權
REVOKE INSERT ON *.* FROM clsn@localhost;
可以授權的使用者許可權
INSERT,SELECT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE
【示例】部落格授權收回示例
grant select,insert,update,delete,create,drop on blog.* to 'blog'@'172.16.1.%' identified by 'blog123'; revoke create,drop on blog.* from 'blog'@'172.16.1.%';
授權部落格類的最多許可權:select,insert,update,delete
1.2 MySQL 客戶端工具
1.2.1 MysQL客戶端命令介紹
mysql命令客戶端
用於資料庫連線管理
將 使用者SQL 語句傳送到伺服器
mysqladmin命令 :命令列管理工具
mysqldump命令 :備份資料庫和表的內容
1.2.2 mysql命令說明
用於連線資料庫
用於管理資料庫通過下列方式進行管理
命令介面自帶命令
DDL:資料定義語言
DCL:資料控制語言
DML:資料操作語言
mysql命令介面自帶命令說明
命令 |
命令說明 |
\h 或 help 或 ? |
獲取幫助 |
\G |
格式化輸出(行轉列) |
\T 或 tee |
記錄操作日誌 tee /tmp/mysql.log |
\c 或 CTRL+c |
退出mysql |
\s 或 status |
檢視資料庫狀態資訊 |
\. 或 source |
mysql> source /tmp/world.sql |
\! |
使用shell中的命令 mysql> \! cat /etc/redhat-release CentOS release 6.9 (Final) |
\u 或use |
use world show databases 看當前所有資料庫的名字 show tables 檢視當前use到的資料庫所有的表 show tables from world 檢視目標資料庫下的表 |
快捷鍵 |
上下翻頁、TAB鍵、ctrl +C 、ctrl +L |
1.2.2.1 mysql中help命令的使用
在mysql命令列中輸入mysql或 ?都可以檢視幫助
mysql> help
使用 help contents檢視完整的sql類別列表幫助
mysql> help contents
有關特定 SQL 類別或語句的幫助
mysql> help Account Management
檢視 grant 的幫助
mysql> help GRANT
有關與狀態相關的 SQL 語句的幫助
mysql> help status
1.2.2.2 source命令的使用
在 mysql 中處理輸入檔案:
如果這些檔案包含 SQL 語句,則稱為“指令碼檔案”或“批處理檔案”。
使用 SOURCE 命令:
mysql> SOURCE /data/mysql/world.sql
或者使用非互動式:(儘量避免使用mysql 匯入資料,會產生大量的無用日誌)
mysql</data/mysql/world.sql
1.2.3 mysqladmin命令說明
基本語法
mysqladmin -u<name> -p<password> commands
命令說明圖表
功能選項 |
說明 |
mysqladmin -u使用者 -p密碼 ping |
“強制迴應 (Ping)”伺服器。 |
mysqladmin -u使用者 -p密碼 shutdown |
關閉伺服器。 |
mysqladmin -u使用者 -p密碼 create databasename |
建立資料庫。 |
mysqladmin -u使用者 -p密碼drop databasename |
刪除資料庫 |
mysqladmin -u使用者 -p密碼 version |
顯示伺服器和版本資訊 |
mysqladmin -u使用者 -p密碼 status |
顯示或重置伺服器狀態變數 |
mysqladmin -u使用者 -p密碼 password |
設定口令 |
mysqladmin -u使用者 -p密碼 flush-privileges |
重新重新整理授權表。 |
mysqladmin -u使用者 -p密碼 flush-logs |
重新整理日誌檔案和快取記憶體。 |
以上資訊通過mysqladmin --help 獲得 |
1.2.4 mysqldump簡單說明
mysqldump是一款資料庫備份工具。
命令幫助及基本語法:
[[email protected] ~]# mysqldump --help Dumping structure and contents of MySQL databases and tables. Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS]
情參照mysqldump --help
1.3 SQL語句入門
1.3.1 DDL語句(資料定義語言)
定義範圍:
庫 :名字、特性
表:表名字、列
1.3.1.1 資料庫檢視
檢視資料庫--檢視全部
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | clsn | +--------------------+
檢視資料庫--模糊匹配
mysql> show databases like "%s%"; +--------------------+ | Database (%s%) | +--------------------+ | information_schema | | clsn | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
檢視你相關的幫助
mysql> ? show databases; Name: 'SHOW DATABASES' Description: Syntax: SHOW {DATABASES | SCHEMAS} [LIKE 'pattern' | WHERE expr]
1.3.1.2 資料庫操作
建立一個數據庫
mysql> create database haha;
通過show 命令能夠檢視建立的資料庫的格式
mysql> show create database haha; +----------+---------------------------------------------------------------+ | Database | Create Database | +----------+---------------------------------------------------------------+ | haha | CREATE DATABASE `haha` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+---------------------------------------------------------------+ 1 row in set (0.00 sec)
建立資料庫時定義字元編碼
mysql> create database clsn charset utf8 ; mysql> show create database clsn; 查詢資料庫定義資訊。
存在的資料庫修改字元編碼:
mysql> alter database clsn charset gbk;
修改資料庫編碼格式示例
mysql> alter database haha charset gbk -> ; Query OK, 1 row affected (0.00 sec) mysql> show create database haha; +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | haha | CREATE DATABASE `haha` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+--------------------------------------------------------------+ 1 row in set (0.00 sec)
標準修改系統
ALTER DATABASE [db_name] CHARACTER SET charset_name COLLATE collation_name; ALTER DATABASE clsn CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
檢視支援的字符集和校對規則.
mysql> show character set; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 |
刪除資料庫
mysql> drop database haha; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database |
切庫
mysql> use clsn; Database changed
檢視當前所在資料庫
mysql> select database(); +------------+ | database() | +------------+ | clsn | +------------+ 1 row in set (0.00 sec)
檢視當前登陸的使用者
mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
檢視庫裡面的表
mysql> show tables; +----------------+ | Tables_in_clsn | +----------------+ | t1 | | t2 | | test | +----------------+ 3 rows in set (0.00 sec)
1.3.1.3 DDL語句之管理表
表的屬性:
欄位、資料型別、索引
預設:字符集、引擎
表定義(列):
表名、列名
列屬性(資料型別、列約束)
建立表
create table t3 (id int);
建立更多的表;
create table test(id int); create table t1(idcard int ,name char(30),sex char(4));
查看錶結構
mysql> desc t2 -> ; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | idcard | int(11) | YES | | NULL | | | name | char(30) | YES | | NULL | | | sex | char(4) | YES | | NULL | | | addr | char(4) | NO | | NULL | | +--------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
檢視建表語句
mysql> show create table t2\G *************************** 1. row *************************** Table: t2 Create Table: CREATE TABLE `t2` ( `idcard` int(11) DEFAULT NULL, `name` char(30) DEFAULT NULL, `sex` char(4) DEFAULT NULL, `addr` char(4) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
1.3.1.4 修改表的定義
修改表名字
mysql> rename table t3 to haha; mysql> show tables ; +----------------+ | Tables_in_clsn | +----------------+ | haha |
修改表名字,方法二。
mysql> alter table haha rename to people; mysql> show tables; +----------------+ | Tables_in_clsn | +----------------+ | people |
修改表結構
mysql> alter table people add addr char(40) NOT NULL; mysql> desc people; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | addr | char(40) | NO | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
指定新增年齡列到name列後面的位置,示例如下:
mysql> alter table people add age int(4) after name; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc people; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | addr | char(40) | NO | | NULL | | | name | int(40) | YES | | NULL | | | age | int(4) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
通過下面的命令在第一列新增qq欄位。
mysql> alter table people add telnum int first; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc people; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | telnum | int(11) | YES | | NULL | | | id | int(11) | YES | | NULL | | | addr | char(40) | NO | | NULL | | | name | int(40) | YES | | NULL | | | age | int(4) | YES | | NULL | | +--------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
同時新增多個列定義:
mysql> alter table people add id1 int first ,add sex char(4) after name ; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc people; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | id1 | int(11) | YES | | NULL | | | telnum | int(11) | YES | | NULL | | | id | int(11) | YES | | NULL | | | addr | char(40) | NO | | NULL | | | name | int(40) | YES | | NULL | | | sex | char(4) | YES | | NULL | | | age | int(4) | YES | | NULL | | +--------+----------+------+-----+---------+-------+ 7 rows in set (0.00 sec)
刪除表結構:
mysql> alter table people drop sex; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc people; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | id1 | int(11) | YES | | NULL | | | telnum | int(11) | YES | | NULL | | | id | int(11) | YES | | NULL | | | addr | char(40) | NO | | NULL | | | name | int(40) | YES | | NULL | | | age | int(4) | YES | | NULL | | +--------+----------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
修改表定義
mysql> alter table people modify name char(20); Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc people; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | id1 | int(11) | YES | | NULL | | | telnum | int(11) | YES | | NULL | | | id | int(11) | YES | | NULL | | | addr | char(40) | NO | | NULL | | | name | char(20) | YES | | NULL | | | age | int(4) | YES | | NULL | | +--------+----------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
修改列名:
mysql> alter table people change name people_name char(30) ; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc people; +-------------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------+------+-----+---------+-------+ | id1 | int(11) | YES | | NULL | | | telnum | int(11) | YES | | NULL | | | id | int(11) | YES | | NULL | | | addr |