mysql 5.7-8.0 安装及安全配置


一、安装和初始化mysql数据库

# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

# mv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql/

#useradd mysql

# chown -R mysql:mysql  mysql

上传my.cnf 到/etc下

# yum install numactl* -y

#初始化数据库

# /usr/local/mysql/bin/mysqld  --defaults-file=/etc/my.cnf  --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data

#启动数据库,设置自动启动

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

Centos7:

# chkconfig --add mysqld

Ubuntu:

#systemctl enable mysqld

查看管理员密码:

在/home/mysql/error.log 查看最后一行:A temporary password is generated for root@localhost:初始密码

Centos7:

#/etc/init.d/mysqld start

Ubuntu:

#systemctl start mysqld

登录后修改管理员密码

(1)mysql 5.7:

set password=PASSWORD('新密码');

set sql_safe_updates=off;

update mysql.user set authentication_string=PASSWORD('新密码');

flush privileges;

(2)mysql 8.0:

/usr/local/mysql/bin/mysqladmin -u root -p'初始密码' password '新密码'

set sql_safe_updates=off;

alter user root@'localhost' identified with mysql_native_password by '新密码';

alter user 'mysql.sys'@'localhost' identified with mysql_native_password by '新密码';

alter user 'mysql.session'@'localhost' identified with mysql_native_password by '新密码';

alter user 'mysql.infoschema'@'localhost' identified with mysql_native_password by '新密码';

新建用户:

create user user@'%' identified with mysql_native_password by '密码';

grant all on *.* to user@'%';

二、安全项配置

三、配置文件my.cnf参考

# 2023-07-13
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port=3306
default-character-set=utf8mb4
socket = /tmp/mysql.sock

[mysqld]
#skip_grant_tables(root password reset)
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 4G

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /home/mysql/data
port = 3306
server-id = 10
socket = /tmp/mysql.sock
default-storage-engine=InnoDB
max_connections = 2048
max_connect_errors = 200
max_allowed_packet = 1024M
table_open_cache = 2048
transaction_isolation = REPEATABLE-READ
sort_buffer_size = 256M
read_buffer_size = 256M
read_rnd_buffer_size = 256M
join_buffer_size = 512M
bulk_insert_buffer_size = 512M
thread_cache_size = 2048
thread_stack = 512K
tmp_table_size = 1024M
max_heap_table_size = 1024M
ft_min_word_len = 1
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:8G

#数据库默认区分大小,1不区分大小写
lower_case_table_names=0

#数据库默认字符集,排序规则
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci

#log
#general_log = on
#general_log_file=/home/mysql/mysql.log
log-error=/home/mysql/error.log
slow_query_log = on
long_query_time=60
slow-query-log-file=/home/mysql/slowquery.log

#5.7安全策略插件
plugin-load=validate_password.so
validate-password=FORCE_PLUS_PERMANENT

# 会话超时时间
wait_timeout=36000
interactive_timeout=3600
#控制密码自动失效时间
default_password_lifetime=3600
#启用update和delete操作
#sql_safe_updates=0
#禁用访问底层文件系统
local_infile=0

# 最大尝试登陆失败3次
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=60000

#二进制日志
#mysql binlog日志文件保存的过期时间,过期后自动删除
#expire_logs_days = 7
#log-bin=mysql-bin
#binlog_format=mixed

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqldump]
quick
max_allowed_packet = 512M

[mysql]
no-auto-rehash
default-character-set=utf8mb4
safe-updates

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 16M
write_buffer = 16M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
open-files-limit = 8192