第五章 MySQL基本操作

一、设置数据库密码

#1.简单的设置方式
[root@db01 ~]# mysqladmin -uroot password '123'
Warning: Using a password on the command line interface can be insecure.

#2.安全的设置方式
[root@db02 ~]# mysqladmin -uroot password
New password: 123
Confirm new password: 123

二、使用密码登录数据库

#1.正确的登陆方式(不标准,不安全)
[root@db01 ~]# mysql -uroot -p123
[root@db01 ~]# mysql -u root -p123

#2.正确的登陆方式(标准,安全)
[root@db01 ~]# mysql -u root -p
Enter password:

#3.错误的登陆方式(会把密码当成数据库)
[root@db01 ~]# mysql -u root -p 123
Enter password: 
ERROR 1049 (42000): Unknown database '123'

for password options, the password value is optional:  If you use a -p or --password option and specify the password value, there must be no space between -p or --password= and the password following it.
#如果使用-p或者--password参数,他们与密码之间不能有空格

If you use a -p or --password option but do not specify the password value, the client program prompts you to enter the password. The password is not displayed as you enter it. This is more secure than giving the password on the command line. Other users on your system may be able to see a password specified on the command line by executing a command such as ps auxw. 
#如果只写-p或--password参数什么都不接,意思是让你手动输入密码

#4.登录数据库后直接进入库中
[root@db01 ~]# mysql -u root -p mysql
Enter password: 

三、查询数据库用户

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | db01      |
| root | db01      |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

四、删除没有用的用户(优化)

mysql> drop user root@'::1';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'db01';
Query OK, 0 rows affected (0.00 sec)

mysql> delete from mysql.user where host='db01';
Query OK, 1 row affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

五、企业中误删除所有用户故障案例

1.不小心删除了所有用户

#1.查看用户
mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | db02      |
| root | db02      |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

#2.误删除用户
mysql> delete from mysql.user where 1=1;
Query OK, 6 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
Empty set (0.00 sec)

#3.删除所有用户后,仍然可以登录,重启后就不能登录了
[root@db02 ~]# systemctl restart mysqld
[root@db02 ~]# mysql -uroot -p123
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

2.解决方案一:

1)停止数据库

[root@db02 ~]# systemctl stop mysqld

2)跳过授权表和网络启动数据库

#1.只跳过授权表启动
[root@db02 ~]# mysqld_safe --skip-grant-tables &

#2.跳过授权表和网络启动
[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# mysqld_safe --skip-grant-tables --skip-networking &

3)插入一个用户

#1.使用grant授权创建用户失败
mysql> grant all on *.* to root@'localhost' identified by '123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

#2.使用create创建用户失败
mysql> create user root@'localhost';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

#3.使用insert语句插入新用户
mysql> use mysql
mysql> insert into user(user,host,password) values('root','localhost','123');
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
#原因:因为表中的字段很多不能为空,默认值又是空,所以必须指定不为空的字段
mysql> insert into user(user,host,password,ssl_cipher,x509_issuer,x509_subject) values('root','localhost',PASSWORD('123'),'','','');
Query OK, 1 row affected (0.00 sec)
#插入用户可以成功,但是用户权限全是N,没有任何权限的用户没有用

#4.正确方法
mysql> insert into mysql.user values ('localhost','root',PASSWORD('123'),
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'',
'',
'',
'',0,0,0,0,'mysql_native_password','','N');

#查看插入用户的权限
mysql> select * from mysql.user\G

4)正常启动数据库

[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# systemctl start mysqld
[root@db02 ~]# mysql -uroot -p123

3.解决方案二:

1)停止数据库

[root@db02 ~]# systemctl stop mysqld

2)跳过授权表和网络启动数据库

#1.只跳过授权表启动
[root@db02 ~]# mysqld_safe --skip-grant-tables &

#2.跳过授权表和网络启动
[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# mysqld_safe --skip-grant-tables --skip-networking &

3)插入一个用户

#1.刷新授权表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#2.授权一个用户
mysql> grant all on *.* to root@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

#3.查看用户权限
mysql> select * from mysql.user\G

#4.管理员用户中授权权限为 N,所以要修改这个值
mysql> update mysql.user set Grant_priv='Y' where Host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

#5.或者再授权时直接加上参数包含 grant 权限
mysql> grant all on *.* to root@'localhost' identified by '123' with grant option;
Query OK, 0 rows affected (0.00 sec)

4)正常启动数据库

[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# systemctl start mysqld
[root@db02 ~]# mysql -uroot -p123

六、MySQL体系结构

1.客户端与服务端模型

1)mysql是一个典型的C/S服务结构

1.mysql客户端
    mysql
    mysqladmin
    mysqldump

2.mysqld一个二进制程序,后台的守护进程
	单进程
	多线程

2)MySQL的两种连接方式

1.TCP/IP的连接方式
2.套接字连接方式

3.举例:
	1)TCP/IP连接
	mysql -uroot -p -h127.0.0.1
	mysql -uroot -p -h127.0.0.1 -S /tmp/mysql.sock
	
	2)socket连接
	mysql -uroot -p -hlocalhost
	mysql -uroot -p123

4.注意:
	1)不一定加了 -h 就是TCP/IP连接,localhost就是特别的一个
	2)socket连接比TCP/IP连接速度快,因为不需要建立连接,不需要经过三次握手四次挥手

七、MySQL服务构成

1.实例

1.MySQL的后台进程+线程+预分配的内存结构。
2.MySQL在启动的过程中会启动后台守护进程,并生成工作线程,预分配内存结构供MySQL处理数据使用。

3.什么是实例:一个进程 + 多个线程 + 一个预分配的内存空间
4.多实例:多个进程 + 多个线程 + 多个预分配的内存空间

2.MySQL服务程序结构

1)连接层

1.验证用户身份,验证用户名和密码是否匹配
2.提供了两种连接方式
3.连接层提供了一个与sql层交互的线程

2)SQL层

1.接收连接层传过来的sql语句
2.验证sql语句的语法
3.验证sql语句的语义(DDL,DQL,DML,DCL)
4.解析器:解析sql语句,生成执行计划
5.优化器:从执行计划中选择最优的一条
6.执行器:执行选出来的计划
	1)与存储引擎层建立交互的线程
	2)将要执行的sql语句传到存储引擎层
7.如果有缓存,读取缓存
8.记录binlog日志

3)存储引擎层

1.建立与sql交互的线程
2.接收sql层传来的sql语句
3.与磁盘交互,获取数据,并返回给sql层

3.mysql的逻辑结构

1.库
2.表:元数据+真实数据行
3.元数据:列+其它属性(行数+占用空间大小+权限)
4.列:列名字+数据类型+其他约束(非空、唯一、主键、非负数、自增长、默认值)

1)MySQL逻辑结构与Linux系统对比

MySQL Linux
目录
show databases; ls -l /
use mysql cd /mysql
文件
show tables; ls
二维表=元数据+真实数据行 文件=文件名+文件属性

4.MySQL物理结构

1.MySQL的最底层的物理结构是数据文件,也就是说,存储引擎层,打交道的文件,是数据文件。
2.存储引擎分为很多种类(Linux中的FS)
3.不同存储引擎的区别:存储方式、安全性、性能
posted @ 2020-10-26 16:58  年少纵马且长歌  阅读(132)  评论(0编辑  收藏  举报