[SQL] MariaDB & MySQL 数据的查询 (转载)

MySQL基本查询语句

MySQL基本查询语句是掌握MySQL数据查询的基本操作,处理数据所必要掌握的,提高处理数据的能力,速度要熟练掌握基本的查询语句。

1.limit字句

select * from limit 3;查询前三条数据

select * from limit 5,3;查询从第5+1条开始的后三条数据

2.函数:平均数,总和,最大值,最小值和总信息数

平均数:select AVG(age) as 平均 from info;查询info表的平均年龄

总和:select SUM(age) as 年龄总和 from lx;查询lx表格的年龄的总和

最大值:select MAX(age) as 最大年龄 from lx; 查询lx表格里年龄的最大值

最小值:select MIN(age) as 最小年龄 from lx; 查询lx表格中的年龄的最小值

总信息数:select COUNT(*) as 总人数 from lx; 查询lx中的信息条数

3.where子句中的经常使用的运算符:比较运算符和逻辑运算符

比较运算符:
大于> 小于< 小于等于<= 大于等于>= 等于 = 不等于<> !=

区间 :between…and…
1.select age from student where age>=18 and age<=22;等价于
2.select age from student where age between 18 and 22;

查想查的信息:in
select age from student where age in(19,20,50);
查年龄是19.20.50的人

模糊查询:like
select age from student where name like ‘王%’; 查询姓王的人 %表示任意长度任意字符

不为空:is null

逻辑运算符:
同时成立:and。任一成立:or。不成立:not

4.笛卡尔积:
select 表1.name,表2.score from 表1 s(表1缩写),表2 g(表2缩写) where s.id = g.gradeId;

5.表连接
内连接:select s.name,g.score from student s inner join grade g on s.id=g.gradeId;

左外连接:select s.name,g.score from student s Left join grade g on s.id=g.gradeId

左外连接:select s.name,g.score from student s right join grade g on s.id=g.gradeId
————————————————
版权声明:本文为CSDN博主「我是超级小白」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lady88888888/article/details/103018101

注明:所有转载内容皆直接从被转载文章网页的标题和内容的文本中复制而来

CC 4.0 BY-SA 版权协议网址:https://creativecommons.org/licenses/by-sa/4.0/deed.z

[SQL] MariaDB & MySQL 表结构的修改 (转载)

MYSQL的修改表结构SQL语句:

-- 查看创表语句
SHOW CREATE TABLE t_login;

-- 查看表结构
desc t_login1;

-- 增加列
ALTER TABLE t_login1 ADD COLUMN COL_1 VARCHAR(10);

-- 修改列
ALTER TABLE t_login1 MODIFY COLUMN COL_1 INT(10);

-- 修改列名称
ALTER TABLE t_login1 CHANGE COL_1 COL_2 VARCHAR(50);

-- 删除列
ALTER TABLE t_login1 DROP COLUMN COL_2;

-- 修改整个表的字符集
ALTER TABLE t_login1 DEFAULT CHARACTER SET UTF8;

-- 修改某个字段的字符集
ALTER TABLE t_login1  CHANGE stu_num stu_num varchar(20) CHARACTER SET utf8;
————————————————
版权声明:本文为CSDN博主「h_j_c_123」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/h_j_c_123/article/details/95035434

注明:所有转载内容皆直接从被转载文章网页的标题和内容的文本中复制而来

CC 4.0 BY-SA 版权协议网址:https://creativecommons.org/licenses/by-sa/4.0/deed.z

[内容] MariaDB & MySQL 安全调优思路

内容一:对网络数据进行加密传输

1.1 生成 SSL

1.1.1 创建 CA 证书
# openssl genrsa 2048 > ca-key.pem
Generating RSA private key, 2048 bit long modulus
..+++
...................................+++
e is 65537 (0x10001)

# openssl req -new -x509 -nodes -days 3600 -key ca-key.pem -out ca.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:ca
Email Address []:

(注意:创建 CA 证书、服务端证书、客户端证书时 Common Name 必须要有值,且必须相互不一样)

1.1.2 创建服务端证书,并去除加密,并使用刚刚的 CA 证书进行签名
# openssl req -newkey rsa:2048 -days 3600 -nodes -keyout server-key.pem -out server-req.pem
Generating a 2048 bit RSA private key
.............+++
...+++
writing new private key to 'server-key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

# openssl rsa -in server-key.pem -out server-key.pem
writing RSA key

# openssl x509 -req -in server-req.pem -days 3600 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
Signature ok
subject=/C=XX/L=Default City/O=Default Company Ltd
Getting CA Private Key

(注意:创建 CA 证书、服务端证书、客户端证书时 Common Name 必须要有值,且必须相互不一样)

1.1.3 创建客户端证书,并去除加密,并使用刚刚的 CA 证书进行签名
# openssl req -newkey rsa:2048 -days 3600 -nodes -keyout client-key.pem -out client-req.pem
Generating a 2048 bit RSA private key
..................+++
..............................................+++
writing new private key to 'client-key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

# openssl rsa -in client-key.pem -out client-key.pem
writing RSA key

# openssl x509 -req -in client-req.pem -days 3600 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
Signature ok
subject=/C=XX/L=Default City/O=Default Company Ltd
Getting CA Private Key
1.1.4 对生成的证书进行验证
# openssl verify -CAfile ca.pem server-cert.pem client-cert.pem
server-cert.pem: OK
client-cert.pem: OK

1.2 将 SSL 添加到 MariaDB & MySQL

1.2.1 将 SSL 放在指定的位置
# mv ca.pem /home/mysql/sslconfig/ca.pem
# mv server-cert.pem /home/mysql/sslconfig/server-cert.pem
# mv server-key.pem /home/mysql/sslconfig/server-key.pem
1.2.2 修改配置文件
# vim /etc/my.cnf

在:

......
[mysqld]

下面添加:

ssl-ca=/home/mysql/sslconfig/ca.pem
ssl-cert=/home/mysql/sslconfig/server-cert.pem
ssl-key=/home/mysql/sslconfig/server-key.pem
......

(补充:这里以 MariaDB 数据库的配置文件是 /etc/my.cnf 为例)

1.2.3 重启数据库
# systemctl restart mariadb

(补充:这里以重启 MariaDB 数据库为例)

1.3 验证 SSL
1.3.1 查看 have_ssl 和 ssl 变量
> show variables like 'have_%ssl';
> show variables like '%ssl%';

(补充:如果它们的参数为 yes ,表示服务端已经开启 SSL)

1.3.2 查看 SSL 的状态
> show status like 'ssl_cipher'

(补充:如果出现 “SSL:Cipher in use is DHE-RSA-AES256-SHA“ 则表示客户端已经使用 SSL 连接了)

1.3.3 确保所有数据库用户使用 SSL
> grant usage on <database>.<table> to '<user>'@'<host>' reouter ssl;
1.3.4 用户通过 SSL 连接数据库的方法
> mysql -u <user> -p -h <host> --ssl-ca=/home/mysql/sslconfig/ca.pem

内容二:开启审计

(如果是 MariaDB 和 MySQL 5.7)

> set global log_warning=2;

(如果是 MySQL 8.0 及以上版本开启审计)

> set global general_log = on;
> set global log_timestamps = SYSTEM;

[步骤] MariaDB & MySQL root 密码的重置

注意:

在重置 MariaDB & MySQL 的 root 密码之前要先安装 MariaDB & MySQL

正文:

步骤一:免密进入数据库

1.1 在 MariaDB&MySQL 文件中添加免密登录参数

# vim /etc/my.cnf

将部分内容修改如下:

......
[mysqld]
skip-grant-tables
......

1.2 使修改的配置生效

1.2.1 MariaDB 使修改的配置生效
# systemctl restart mariadb

(注意:只有当重置 MariaDB 的时候才执行这一步)

1.2.2 MySQL 使修改的配置生效
# systemctl restart mysqld

(注意:只有当重置 MariaDB 的时候才执行这一步)

步骤二:将 root 密码设置为空

2.1 不使用密码进入数据库

# mysql -u root -p

(补充:当提示输入密码时直接敲回车)

2.2 清空 root 用户密码

2.2.1 MariaDB 和 MySQL 5.7 及以下的版本将 root 密码设置为空
> update mysql.user set password='' where user='root';

(注意:只有当是重置 MariaDB 和 MySQL 5.7 及以下版本密码的时候才需要执行这一步)

2.2.2 MySQL 8.0 及以上的版本将 root 密码设置为空
> update mysql.user set authentication_string='' where user='root';

(注意:只有当是重置 MySQL 8.0 及以上版本密码的时候才需要执行这一步)

2.3 退出数据库

> quit;

步骤三:给 root 设置新密码

3.1 在 MariaDB&MySQL 文件中将免密登录参数注释掉

# vim /etc/my.cnf

将部分内容修改如下:

......
[mysqld]
# skip-grant-tables
......

3.2 使修改的配置生效

3.2.1 MariaDB 使修改的配置生效
# systemctl restart mariadb

(注意:只有当重置 MariaDB 的时候才执行这一步)

3.2.2 MySQL 使修改的配置生效
# systemctl restart mysqld

(注意:只有当重置 MySQL 的时候才执行这一步)

3.3 进入数据库

> mysql -u root -p

(补充:当提示输入密码时直接敲回车)

3.4 给 root 设置新密码

> alter user 'root'@'localhost' identified by '<password>';

3.5 退出数据库

> quit;

[实验] MySQL 的安装 (通过 RPM 软件包实现)

纪念:站主于 2019 年 11 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

软件准备:

在 MySQL 的官网上下载安装数据库所需要的软件包 MySQL:

https://dev.mysql.com/downloads/mysql/

正文:

步骤一:系统环境要求

1) 服务器的系统需要是 CentOS Linux 7 版本
2) 服务器系统需要有软件源

步骤二:部署安装 MySQL 的环境

2.1 删除系统上的 MariaDB

# systemctl stop mariadb
# rm -rf /var/lib/mysql/*
# rpm -e --nodeps mariadb-server mariadb 

2.2 确保当前目录下拥有如下安装包

# ls
mysql-community-client-8.0.18-1.el7.x86_64.rpm
mysql-community-common-8.0.18-1.el7.x86_64.rpm
mysql-community-devel-8.0.18-1.el7.x86_64.rpm
mysql-community-libs-8.0.18-1.el7.x86_64.rpm
mysql-community-server-8.0.18-1.el7.x86_64.rpm

(补充:这里要安装的是 MySQL 是 8.0.18 社区版)

步骤三:安装 MySQL 数据库

# yum -y localinstall mysql-community-*

步骤四:启动 MySQL 数据库

# systemctl start mysqld

步骤五:修改 MySQL 数据库的 root 密码

5.1 显示初始的 root 密码

# grep 'temporary password' /var/log/mysqld.log 
2019-11-09T09:37:31.347523Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wAA!w,?e#M2J

(补充:这里查出来的密码是 wAA!w,?e#M2J)

5.2 进入数据库

# mysql -u root -p'wAA!w,?e#M2J'

(补充:这里使用的密码是 wAA!w,?e#M2J)

5.3 修改 root 密码

> alter user user() identified by '<password>';

5.4 退出数据库

> quit;