MySQL5.7(二)数据库的基本操作
登录MySQL数据库
格式:mysql -u 用户名 -h 主机名或IP地址 -P 端口号 -p 密码
1、显示数据库
MySQL5.7.20默认数据库
- information_schema
- mysql #必须存在的,用于描述用户访问权限;
- performance_schema
- sys
2、创建数据库
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>
#utf8编码
CREATE DATABASE 数据库实例名 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#gbk编码
CREATE DATABASE 数据库实例名 DEFAULT CHARSET gbk COLLATE gbk_chinese_ci;
3、声明查看数据的定义
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>
4、使用数据库实例
1 实战演练如下:
2 mysql> USE cuixiaozhao;#使用数据库实例cuixiaozhao;
3 Database changed
4 mysql>
5、删除数据库
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声明删除数据库后,数据库中存储的所有数据表和数据也将一并被删除且不可恢复。
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>
查看权限
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>
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地址为%)
1 mysql> mysql> SHOW ENGINES; 2 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 3 | Engine | Support | Comment | Transactions | XA | Savepoints | 4 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 5 | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | 6 | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | 7 | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | 8 | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | 9 | MyISAM | YES | MyISAM storage engine | NO | NO | NO | 10 | CSV | YES | CSV storage engine | NO | NO | NO | 11 | ARCHIVE | YES | Archive storage engine | NO | NO | NO | 12 | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | 13 | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | 14 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 15 9 rows in set (0.00 sec)
存储引擎的选择
1、如果要提供提交、回滚和崩溃恢复能力的事务安全(ACID兼容)能力,并要求实现并发控制,InnoDB是个很好的选择;
2、如果数据表主要用于插入和查询记录,则MyISAM引擎能提供较高的处理效率;
3、如果只是临时存放数据,数据量并不大并且不需要较高的数据安全性,可以选择将数据保存在内存中的Memory引擎,MySQL中使用该引擎作为临时表,存放查询的中间结果。
4、如果只有INSERT和SELECT操作,可以选择Arichive引擎,Archive存储引擎支持高并发的插入操作,但是本身并不是事物安全的,Archive存储引擎非常适合存储归档数据;
总结,使用哪种存储引擎需要灵活总结,使用合适的存储引擎,将会提高整个数据库的性能!