1、标准用户
groupadd mysql useradd mysql -g mysql -s /sbin/nologin2、解压并授权
tar xf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /asop/ mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql5.7.18 chown -R mysql.mysql /home/mysql3、创建相关目录
mkdir -p /asop/mysql5.7.18/logs/{sock,tmpdir,log,innodb_ts,innodb_log,undo,slowlog,binlog,relaylog} mkdir -p /asop/mysql5.7.18/data4、创建链接文件
ln –s /asop/mysql5.7.18 /usr/local/mysql5、创建环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile source /etc/profile6、创建配置文件
#参考配置文件 vi /etc/my.cnf [client] socket=/asop/mysql5.7.18/logs/sock/mysql.sock default-character-set = utf8 local-infile=0 [mysqld] user=mysql port=3307 basedir = /usr/local/mysql character-set-server=utf8 default-storage-engine = INNODB server-id=1 collation_server = utf8_bin log_timestamps=SYSTEM socket=/asop/mysql5.7.18/logs/sock/mysql.sock pid-file=/asop/mysql5.7.18/logs/sock/mysql.pid datadir=/asop/mysql5.7.18/data tmpdir=/asop/mysql5.7.18/logs/tmpdir log-error=/asop/mysql5.7.18/logs/log/error.log slow_query_log = on long_query_time = 5 slow_query_log_file=/asop/mysql5.7.18/logs/slowlog/slow-query.log log-bin=/asop/mysql5.7.18/logs/binlog/mysql-bin relay-log=/asop/mysql5.7.18/logs/relaylog/mysql-relay-bin innodb_data_home_dir = /asop/mysql5.7.18/logs/innodb_ts innodb_log_group_home_dir = /asop/mysql5.7.18/logs/innodb_log innodb_undo_directory = /asop/mysql5.7.18/logs/undo/ local-infile=0 skip_symbolic_links=yes max_user_connections=200 max_connections =2000 #validate_password_policy=STRONG #validate_password_length = 9 #validate_password_mixed_case_count= 5 #validate_password_number_count= 2 #validate_password_special_char_count= 1 #connection_control_failed_connections_threshold= 3 #connection_control_max_connection_delay =1800000 #connection_control_min_connection_delay= 600000 wait_timeout=1800 interactive_timeout=1800 [mysqldump] quick max_allowed_packet = 2G default-character-set = utf87、初始化数据库
cd /asop/mysql5.7.18/bin ./mysqld ----initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data8、添加系统服务
cp -ar /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig mysqld on修改默认端口
vi /etc/my.cnf port=3307清除用户
#只需要查验即可,新库(5.7+)一般不会存在特殊用户 select user,host from mysql.user; delete from mysql.user where uer not in ('mysql.sys','mysql.session','mysqlxsys','root','mysql.infoschema') or host not in ('localhost');设置本机管理员密码
set password for 'root'@'localhost' = password('P@ssw0rd'); flush privileges;清除默认库
#只需要查验即可,新库(5.7)不存在测试库 show databases; drop database test;创建远程管理员账户
#避免直接使用root作为远程管理账户 grant all on *.* to 'asop'@'%' identified by 'P@ssw0rd' with grant option;确保读取本地文件的参数设置为失效
Mysql命令行下,使用如下命令: show variables where variable_name = 'local_infile'; 查看结果是否为OFF。 如果该命令为ON,则数据库用户可以通过LOAD DATA INFILE 或者 SELECT local_file 读取到数据库所在操作系统本地的文件,在这种情况下,需要在mysql配置文件中新增如下内容: [mysqld] local-infile=0 [client] local-infile=0 或者临时生效使用 set global local_infile=off; 然后重启数据库服务。开启错误日志
show variables like 'log_error'; #确保结果不为空,建议将日志保存至非系统分区 vi /etc/my.cnf [mysqld_safe] log-error=/asop/mysql57/log/errorlog/mysql_error.log检查是否存在匿名账户
select * from mysql.user where user=''; #存在则删除 delete from user where user='';禁用符号链接
vi /etc/my.cnf [mysqld] skip_symbolic_links=yes限制单个用户的连接数量
#0为不限制,可以无限建立连接,直到数据库无法提供响应 #查看现有配置 show variables like 'max_user_connections'; vi /etc/my.cnf [mysqld] max_user_connections=200设置MySQL的最大连接数
#默认设置为151,增大此值可以使数据库响应更多的连接 #查看现有配置 show variables like 'max_connections'; vi /etc/my.cnf [mysqld] max_connections = 3000开启慢查询日志
#查询慢查询的定义时间 show variables like 'long_query_time'; #查询慢查询配置及路径 show status like '%slow_queries%'; show variables like '%slow%'; vi /etc/my.cnf slow_query_log = on long_query_time = 5 slow_query_log_file=/asop/mysql5.7.18/logs/slowlog/slow-query.log设置密码复杂度策略
#MySQL密码复杂度依赖于validate_password插件,需要确认所用的版本中是否支持此插件 #查看MySQL安装目录下面的 $mysql_basedir/lib/plugin 中是否存在validate_password.so文件 #检查mysql是否已经安装validate_password插件 show plugins; #安装插件 install plugin validate_password soname 'validate_password.so' #查看默认策略 show variables like 'validate_password%'; vi /etc/my.cnf #设置策略为最高级,需要同时满足长度和复杂度的要求,且要求密码不能存放到字典中 validate_password_policy STRONG #密码要求的最小长度 validate_password_length= 13 #需要满足的大小写字母数 validate_password_mixed_case_count= 5 #需要满足的数字的个数 validate_password_number_count= 2 #需要满足的特殊字符数 validate_password_special_char_count= 1开启登陆失败处理功能
#MySQL密码复杂度依赖于connection_control插件,需要确认所用的版本中是否支持此插件 #查看MySQL安装目录下面的 $mysql_basedir/lib/plugin 中是否存在connection_control.so文件 #插件安装 install plugin connection_control soname 'connection_control.so' show variables like 'connection%'; vi /etc/my.cnf #允许密码失败的次数 connection_control_failed_connections_threshold= 3 #锁定的最长时间 connection_control_max_connection_delay =1800000 #锁定的最短时间(毫秒) connection_control_min_connection_delay= 600000设置超时时间
#查看当前超时时间设置,根据使用场景来进行设置,若使用连接池进行连接,需要调整连接池的空闲丢弃连接的时间(maxidletime)小于以下设置的两个值,若需要维持连接,则使用连接池进行检查是否存在空闲连接,并发送一条查询命令,维持连接,避免出现 连接池将已经被MySQL丢弃的超时连接继续分配给连接池使用,导致程序报错。 show global variables like '%timeout%'; vi /etc/my.cnf wait_timeout=1800 interactive_timeout=1800设置资源使用及审计功能
#根据场景进行设置 参考:https://blog.csdn.net/woshiji594167/article/details/103313980