1. 程式人生 > >MySQL5.7-數據庫的基本操作

MySQL5.7-數據庫的基本操作

charset ase l數據庫 insert name chang proc 編碼方式 -s

登錄MySQL數據庫

格式:mysql -u 用戶名 -h 主機名或IP地址 -P 端口號 -p 密碼

技術分享圖片

1、顯示數據庫

SHOW DATABASES;

MySQL5.7.20默認數據庫

  • information_schema
  • mysql #必須存在的,用於描述用戶訪問權限;
  • performance_schema
  • sys

2、創建數據庫

CREATE DATABASE 數據庫實例名; 技術分享圖片技術分享圖片
 1 實戰演練如下:
 2 1)#utf8編碼,創建一個名為:cuixiaozhao的MySQL數據庫實例。
 3 mysql> CREATE DATABASE cuixiaozhao DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
 4 Query OK, 1 row affected (0.01 sec)
 5 mysql> SHOW CREATE DATABASE cuixiaozhao;
 6 +-------------+----------------------------------------------------------------------+
 7 | Database    | Create Database                                                      |
 8 +-------------+----------------------------------------------------------------------+
 9 | cuixiaozhao | CREATE DATABASE `cuixiaozhao` /*!40100 DEFAULT CHARACTER SET utf8 */ |
10 +-------------+----------------------------------------------------------------------+
11 row in set (0.00 sec)
12 mysql>
13 2)#gbk編碼,創建一個名為:cuixiaosi的MySQL數據庫實例。
14 mysql> CREATE DATABASE cuixiaosi DEFAULT CHARSET gbk COLLATE gbk_chinese_ci;
15 Query OK, 1 row affected (0.00 sec)
16 mysql> SHOW CREATE DATABASE cuixiaosi;
17 +-----------+-------------------------------------------------------------------+
18 | Database  | Create Database                                                   |
19 +-----------+-------------------------------------------------------------------+
20 | cuixiaosi | CREATE DATABASE `cuixiaosi` /*!40100 DEFAULT CHARACTER SET gbk */ |
21 +-----------+-------------------------------------------------------------------+
22 row in set (0.00 sec)
23 mysql>
View Code 默認編碼格式為:latin1,不推薦使用,建議使用如下utf8編碼方式進行創建!
#utf8編碼
CREATE DATABASE 數據庫實例名 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#gbk編碼
CREATE DATABASE 數據庫實例名 DEFAULT CHARSET gbk COLLATE gbk_chinese_ci;

3、聲明查看數據的定義

SHOW CREATE DATABASE 數據庫實例名; 技術分享圖片
 1 舉例如下:
 2 
 3 mysql> SHOW CREATE DATABASE cuixiaozhao;#查看創建好的數據庫實例-
cuixiaozhao的定義。 4 +-------------+----------------------------------------------------------------------+ 5 | Database | Create Database | 6 +-------------+----------------------------------------------------------------------+ 7 | cuixiaozhao | CREATE DATABASE `cuixiaozhao` /*!40100 DEFAULT CHARACTER SET utf8
*/ | 8 +-------------+----------------------------------------------------------------------+ 9 1 row in set (0.00 sec) 10 11 mysql> SHOW CREATE DATABASE cuixiaosi;#查看創建好的數據庫實例-cuixiaosi的定義。 12 +-----------+-------------------------------------------------------------------+ 13 | Database | Create Database | 14 +-----------+-------------------------------------------------------------------+ 15 | cuixiaosi | CREATE DATABASE `cuixiaosi` /*!40100 DEFAULT CHARACTER SET gbk */ | 16 +-----------+-------------------------------------------------------------------+ 17 1 row in set (0.00 sec) 18 19 mysql>
View Code

4、使用數據庫實例

USE 數據庫實例名; 技術分享圖片
1 實戰演練如下:
2 mysql> USE cuixiaozhao;#使用數據庫實例cuixiaozhao;
3 Database changed
4 mysql>
View Code

5、刪除數據庫

DROP 數據庫實例名; 技術分享圖片
 1 實戰演練如下:
 2 mysql> DROP DATABASE cuixiaosi;
 3 Query OK, 0 rows affected (0.00 sec)
 4 
 5 mysql> SHOW DATABASES;
 6 +--------------------+
 7 | Database |
 8 +--------------------+
 9 | information_schema |
10 | cuixiaozhao |
11 | mysql |
12 | performance_schema |
13 | sys |
14 +--------------------+
15 5 rows in set (0.00 sec)
16 
17 mysql>
18 
19 註意:使用DROP DATABASE命令時要非常謹慎,MySQL不會給予任務提示確認信息,DROP DATABASE聲明刪除數據庫後,數據庫中存儲的所有數據表和數據也將一並被刪除且不可恢復。
View Code

6、MySQL用戶管理及授權

創建用戶

CREATE USER ‘用戶名‘@‘IP地址‘ IDENTIFIED BY ‘密碼‘;

修改用戶

RENAME USER ‘用戶名‘@‘IP地址‘ TO ‘新/舊用戶名‘@‘新/舊IP地址‘;

修改密碼

SET PASSWORD FOR ‘用戶名‘@‘IP地址‘ = PASSWORD(‘新密碼‘);

刪除用戶

DROP USER ‘用戶名‘@‘IP地址‘;

技術分享圖片
 1 實戰演練如下:
 2 mysql> CREATE USER cuixiaozhao@localhost IDENTIFIED BY Ab123456!@#$;
 3 
 4 Query OK, 0 rows affected (0.00 sec)
 5 
 6 mysql> RENAME USER cuixiaozhao@localhost TO zhaoxiaocui@localhost;
 7 Query OK, 0 rows affected (0.00 sec)
 8 
 9 mysql> SET PASSWORD FOR zhaoxiaocui@localhost = PASSWORD($#@!654321bA);
10 Query OK, 0 rows affected, 1 warning (0.00 sec)
11 
12 mysql> DROP USER zhaoxiaocui@localhost;
13 Query OK, 0 rows affected (0.00 sec)
14 
15 mysql>
View Code

查看權限

SHOW GRANTS FOR ‘用戶名‘@‘IP地址‘;
授權

GRANT ALL ON *.* TO ‘cuixiaozhao‘@‘localhost‘;

撤銷授權

REVOKE ALL ON *.* FROM ‘cuixiaozhao‘@‘localhost‘;

技術分享圖片
 1 實戰演練如下:
 2 
 3 mysql> CREATE USER cuixiaozhao@localhost IDENTIFIED BY Ab123456!@#$;
 4 
 5 Query OK, 0 rows affected (0.00 sec)
 6 
 7 mysql> SHOW GRANTS FOR cuixiaozhao@localhost;
 8 +-------------------------------------------------+
 9 | Grants for cuixiaozhao@localhost |
10 +-------------------------------------------------+
11 | GRANT USAGE ON *.* TO cuixiaozhao@localhost |
12 +-------------------------------------------------+
13 1 row in set (0.00 sec)
14 
15 mysql> GRANT ALL ON *.* TO cuixiaozhao@localhost;
16 Query OK, 0 rows affected (0.00 sec)
17 
18 mysql> SHOW GRANTS FOR cuixiaozhao@localhost;
19 +----------------------------------------------------------+
20 | Grants for cuixiaozhao@localhost |
21 +----------------------------------------------------------+
22 | GRANT ALL PRIVILEGES ON *.* TO cuixiaozhao@localhost |
23 +----------------------------------------------------------+
24 1 row in set (0.00 sec)
25 
26 mysql> REVOKE ALL ON *.* FROM cuixiaozhao@localhost;
27 Query OK, 0 rows affected (0.00 sec)
28 
29 mysql> SHOW GRANTS FOR cuixiaozhao@localhost;
30 +-------------------------------------------------+
31 | Grants for cuixiaozhao@localhost |
32 +-------------------------------------------------+
33 | GRANT USAGE ON *.* TO cuixiaozhao@localhost |
34 +-------------------------------------------------+
35 1 row in set (0.00 sec)
36 
37 mysql>
View Code

all privileges 除grant外的所有權限;
select 僅查權限;
select,insert 查和插入權限;
usage 無訪問權限;
alter 使用alter table;
alter routine 使用alter procedure和drop procedure;
create 使用create table;
create routine 使用create procedure;
create temporary tables 使用create temporary tables;
create user 使用create user、drop user、rename user和revoke all privileges;
create view 使用create view;
delete 使用delete;
drop 使用drop table;
execute 使用call和存儲過程;
file 使用select into outfile 和 load data infile;
grant option 使用grant 和 revoke;
index 使用index;
insert 使用insert;
lock tables 使用lock table;
process 使用show full processlist;
select 使用select;
show databases 使用show databases;
show view 使用show view;
update 使用update;
reload 使用flush;
shutdown 使用mysqladmin shutdown(關閉MySQL);
super 使用change master、kill、logs、purge、master和set global,還允許mysqladmin調試登錄;
replication client 服務器位置的訪問;
replication slave 主從復制時使用;

7、其他項目說明

1、對於目標數據庫以及內部其他:
數據庫名.*          #指定數據庫中的所有表
數據庫名.表         #指定數據庫中的某張表
數據庫名.存儲過程    #指定數據庫中的存儲過程
*.*               #所有數據庫中的所有表
2、用戶名及IP地址
‘用戶名‘@‘IP地址‘ #用戶只能在該IP下才能訪問 ‘用戶名‘@‘192.168.1.%‘ #用戶只能在該IP段下才能訪問(通配符%表示任意) ‘用戶名‘@‘%‘ #用戶可以在任意IP下訪問(默認IP地址為%)
8、FLUSH PRIVILEGES;#將數據讀取到內存中,從而立即生效。

MySQL5.7-數據庫的基本操作