mysql-8.0.34安装

一、基础配置

1. 下载安装包

Mysql官网下载地址

注意下载glibc-2.12或者2.17的就行,2.28的可能服务器还需要做升级。

2. 将安装包传到服务其中,并建立数据目录和日志目录

mkdir -p /data/app/mysql
mkdir -p /data/app/mysql/data
mkdir -p /data/app/mysql/logs

3. 创建mysql用户并授权

groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /data/app/mysql

4. 创建配置文件

vim /etc/my.cnf

[mysqld]
server-id=1
port=7001
basedir=/data/app/mysql/mysql-8.0.34
datadir=/data/app/mysql/data
lower_case_table_names=1
sql-mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
log_bin = mysql-bin
binlog_format = ROW
max_connections=10000
skip_name_resolve=1
innodb_buffer_pool_size = 6G
# 设置全局字符集
character-set-server=utf8mb4
# 设置错误日志
log-error =/data/app/mysql/logs/mysql-error.log
# 指定密码复杂度插件
plugin-load=validate_password.so
# 指定密码复杂度策略
validate_password_policy=STRONG
# 指定连接失败处理插件
plugin-load-add=connection_control.so
connection-control=FORCE_PLUS_PERMANENT
connection-control-failed-login-attempts=FORCE_PLUS_PERMANENT
# 配置连接失败策略
connection_control_failed_connections_threshold=3
connection_control_min_connection_delay=1000
connection_control_max_connection_delay=90000
# 超时时间配置
wait_timeout = 1800
interactive_timeout=1800
# 开启日志
general-log=1
# 加载审计日志插件
[client]
port=7001
default-character-set=utf8mb4

4. 初始化数据库

cd /data/app/mysql/mysql-8.0.34/bin
./mysqld --initialize --datadir=/data/app/mysql/data --user=mysql

5. 查看默认密码

6 设置开机自启

cp /data/mysql/mysql-8.0.34/support-files/mysql.server /etc/rc.d/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig --list

赋予权限

chmod +x /etc/init.d/mysql

1

添加服务

chkconfig --add mysql

二、以下是安全相关配置,也是参考等保三级配置

1. mysql数据库密码复杂度配置

1.1 查询配置

show variables like 'validate_password%';
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'validate_password';

1.2 安装配置插件

INSTALL PLUGIN validate_password SONAME 'validate_password.so';

1.3 持久化配置

/etc/my.cnf 的[mysqld]下进行配置

plugin-load=validate_password.so
validate_password.policy=STRONG

1.4 安装之后

mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | ON     |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | STRONG |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.01 sec)

1.5 配置说明

validate_password_dictionary_file:密码策略文件,策略为 STRONG 才需要
validate_password_length:密码长度
validate_password_mixed_case_count:大小写字母
validate_password_number_count :数字
validate_password_special_char_count:特殊字符

1.6 测评参考要求

一般要求密码长度不小于 6 位,密码复杂度至少包括数字、大小写字母、特殊字符至少两种

以上

2. 配置mysql数据库密码有效期

2.1 查看当前有效期

show global variables like 'default_password_lifetime';

2.2 配置密码有效期

临时配置,单位为天,配置90天

SET GLOBAL default_password_lifetime = 90;

持久化配置,在/etc/my.cnf 的[mysqld]下进行配置,单位为天,配置90天

default_password_lifetime=90

配置完成后

mysql> show global variables like 'default_password_lifetime';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| default_password_lifetime | 90    |
+---------------------------+-------+
1 row in set (0.01 sec)

3. 配置数据库登录失败处理措施

3.1 查询当前配置

max_connect_errors:最大请求连接失败次数,当 mysql 数据库收到来自同一主机的连接请求,但请求失败次数超设定的次数后,mysql 会拒绝后续这一主机后续的连接请求。

mysql> show variables like '%max_connect_errors%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
+--------------------+-------+
1 row in set (0.00 sec)

3.2 检查是否可以进行设置(插件是否安装)

如下显示则为没有安装

mysql> show variables like '%connection_control%';
Empty set (0.01 sec)

3.3 安装插件

INSTALL PLUGIN connection_control SONAME 'connection_control.so';
install plugin connection_control_failed_login_attempts soname 'connection_control.so';

持久化配置,在/etc/my.cnf的[mysqld]下添加如下配置

plugin-load-add=connection_control.so
connection-control=FORCE_PLUS_PERMANENT
connection-control-failed-login-attempts=FORCE_PLUS_PERMANENT
connection_control_failed_connections_threshold=3
connection_control_min_connection_delay=1000
connection_control_max_connection_delay=90000

3.4 参数说明

connection_control_failed_connections_threshold:单个用户登录失败(由于密码错误引起)次数上限,默认 3 次
connection_control_max_connection_delay:失败上限之后再次尝试登录前最小等待时间,单位 ms
connection_control_min_connection_delay:失败上限之后再次尝试登录前最小等待时间,默认 1 秒(1000ms)

3.5 配置完成后

mysql> show variables like '%connection_control%';
+-------------------------------------------------+-------+
| Variable_name                                   | Value |
+-------------------------------------------------+-------+
| connection_control_failed_connections_threshold | 3     |
| connection_control_max_connection_delay         | 90000 |
| connection_control_min_connection_delay         | 1000  |
+-------------------------------------------------+-------+
3 rows in set (0.01 sec)

3.6 测评参考要求

一般建议登录失败次数不大于 5 次,锁定时间不小于 1 分钟

4. 设置数据库超时时间

4.1 检查当前设置

mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show variables like '%timeout';
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| connect_timeout                   | 10       |
| delayed_insert_timeout            | 300      |
| have_statement_timeout            | YES      |
| innodb_flush_log_at_timeout       | 1        |
| innodb_lock_wait_timeout          | 50       |
| innodb_rollback_on_timeout        | OFF      |
| interactive_timeout               | 28800    |
| lock_wait_timeout                 | 31536000 |
| mysqlx_connect_timeout            | 30       |
| mysqlx_idle_worker_thread_timeout | 60       |
| mysqlx_interactive_timeout        | 28800    |
| mysqlx_port_open_timeout          | 0        |
| mysqlx_read_timeout               | 30       |
| mysqlx_wait_timeout               | 28800    |
| mysqlx_write_timeout              | 60       |
| net_read_timeout                  | 30       |
| net_write_timeout                 | 60       |
| replica_net_timeout               | 60       |
| rpl_stop_replica_timeout          | 31536000 |
| rpl_stop_slave_timeout            | 31536000 |
| slave_net_timeout                 | 60       |
| ssl_session_cache_timeout         | 300      |
| wait_timeout                      | 28800    |
+-----------------------------------+----------+
23 rows in set (0.01 sec)

4.2 参数解释

Connect_time:mysql 客户端在尝试与 mysql 服务器建立连接时,mysql 服务器返回错误握手协议前等待客户端数据包的最大时限。默认 10 秒。
Wait_timeout:等待超时时间、默认值 28800 秒(8 小时)
interactive_timeout:交互式连接超时时间(mysql 工具、mysqldump 等)
wait_timeout:非交互式连接超时时间

4.3 测评要求

一般建议等待超时时间不大于 30 分钟。

4.4 设置时间(目前这两个参数设置还在存疑,没有理解)

修改配置文件[mysqld]模块下添加,设置为30分钟

wait_timeout:设置连接超时时间(等待超时时间)

interactive_timeout: 设置查询超时时间。

interactive_timeout对交互式客户端连接生效,wait_timeout对非交互式客户端连接生效。一旦会话登陆成功如果想要会话级别修改超时参数,不管交互式还是非交互式都是修改wait_timeout(set wait_timeout)参数才会生效。只有在新建立连接时,interactive_timeout 才有可能覆盖 wait_timeout的值,同时也只有 新建连接时,interactive_timeout才有用。其他任何情况,控制空闲连接超时的都是wait_timeout。

wait_timeout = 1800
interactive_timeout = 1800

4.5 设置完成后

mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 1800  |
+---------------+-------+
1 row in set (0.00 sec)

5. 设置mysql 数据库用户及用户允许登录的 IP

5.1 查询当前设置

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.01 sec)

5.2 参数说明

(1) Host 列指定了允许用户登录所使用的 IP,比如 user=root Host=192.168.1.1。
(2) 这里的意思,就是说 root 用户只能通过 192.168.1.1 的客户端去访问。
(3) 而%是个通配符,如果 Host=192.168.1.%,那么就表示只要是 IP 地址前缀为"192.168.1.",的客户端都可以连接。
(4) 如果 Host=%,表示所有 IP 都有连接权限

5.3 修改连接IP

比如,修改mysql用户只能192.168.101段的IP可以连接

update user set Host='192.168.101.%' where User='root';
flush privileges;

6. 设置mysql日志

6.1 查看当前是否开启

general_log:记录所有到达 MySQL Server 的 SQL 语句记录,VALUE 值为 ON,说明已开。

mysql> show variables like 'general_log%';
+------------------+------------------------------------+
| Variable_name    | Value                              |
+------------------+------------------------------------+
| general_log      | OFF                                |
| general_log_file | /data/app/mysql/data/localhost.log |
+------------------+------------------------------------+
2 rows in set (0.01 sec)

6.2 开启日志

临时开启

set global general_log=on;

持久化,修改配置文件

general-log=1

6.3 查看bin_log日志状态

记录了所有的 DDL 和 DML(除了数据查询语句)语句,VALUE 值为 ON,说明已开。

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.01 sec)

持久化配置,修改配置文件

log_bin = mysql-bin
binlog_format = ROW

7. 查看数据库审计功能是否开启

数据库未安装 audit 审计插件,返回值为空

mysql> show variables like '%audit%';
Empty set (0.00 sec)
mysql> 

8. 查询是否通过加密方式进行远程管理

mysql> show variables like '%have_ssl%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| have_ssl      | YES   |
+---------------+-------+
1 row in set (0.00 sec)

9. mysql创建用户

CREATE USER 'admin'@'%' IDENTIFIED BY 'Masterpassw0rd!';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%';

三、主从配置

从节点加入

注意: mysql8 必须要有GET_MASTER_PUBLIC_KEY=1这个配置

CHANGE MASTER TO MASTER_HOST='master_host',MASTER_PORT=master_port,MASTER_USER='slave_user',MASTER_PASSWORD='password', GET_MASTER_PUBLIC_KEY=1, MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=4557;

停止从节点

STOP SLAVE;

清除从节点配置

RESET SLAVE;

开启从节点

START SLAVE;
相关推荐
夜泉_ly2 小时前
MySQL -安装与初识
数据库·mysql
qq_529835353 小时前
对计算机中缓存的理解和使用Redis作为缓存
数据库·redis·缓存
月光水岸New5 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6755 小时前
数据库基础1
数据库
我爱松子鱼5 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo6 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser7 小时前
【SQL】多表查询案例
数据库·sql
Galeoto7 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)7 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231117 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql