Centos7自动化脚本一键安装MySQL5.7

本文最近一次更新于2021.11.03

MySQL5.7源码安装包下载地址

链接:https://pan.baidu.com/s/1N4Nh9YcYF_4cczFvweMDRQ
提取码:87fw

下载的文件请放在root目录下
按照以下方法安装会有个问题,那就是使用mysqldump等命令时,可能会提示socket文件找不到,需要添加参数--socket=/data/mysql/run/mysql.sock,或者把my.cnf里的socket对应目录路径改为/tmp/mysql.sock
以下脚本仅适用于测试环境,生产环境请谨慎使用!
本次使用了三个脚本。
分别复制执行以下脚本即可。
脚本1:配置my.cnf文件

#!/bin/bash
cat > /etc/my.cnf <<EOF
[mysql]
# CLIENT #
port = 3306
socket = /data/mysql/run/mysql.sock
disable-auto-rehash
default-character-set=gbk
[mysqld]
# GENERAL #
server_id = 128
port = 3306
user = mysql
explicit_defaults_for_timestamp=true
default-storage-engine = InnoDB
character_set_server = gbk
auto_increment_increment = 2
auto_increment_offset = 1
lower_case_table_names = 1
socket = /data/mysql/run/mysql.sock
pid_file = /data/mysql/run/mysqld.pid
# MyISAM #
key-buffer-size = 32M
myisam-recover-options = FORCE,BACKUP
# SAFETY #
max_allowed_packet = 134217728
max_connections = 8192
max_user_connections = 8000
open_files_limit = 65535
skip-name-resolve
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sysdate-is-now = 1
# DATA STORAGE #
basedir = /usr/local/mysql
datadir = /data/mysql/data/
tmpdir = /data/mysql/tmp
# BINARY LOGGING #
log-bin = /data/mysql/log/binlog/master-bin
log-bin-index = /data/mysql/log/binlog/master-bin.index
expire-logs-days = 15
sync-binlog = 1
binlog_format = ROW
#RELAY LOGGING
relay-log=/data/mysql/log/relaylog/master-relay-bin
relay-log-index=/data/mysql/log/relaylog/master-relay-bin.index
sync_relay_log=1
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 0
max-connections = 500
thread-cache-size = 50
open-files-limit = 65535
table-definition-cache = 1024
table-open-cache = 2048
# INNODB #
innodb_log_group_home_dir = /data/mysql/log/iblog
innodb_data_home_dir = /data/mysql/log/iblog
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 6G
# LOGGING #
general_log = off
log-error = /data/mysql/log/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = /data/mysql/log/mysql-slow.log
log_slave_updates=ON
EOF

脚本2:安装mysql
该脚本执行MySQL的安装,安装时采用--initialize-insecure选项不生成初始密码即安装完成后可以免密登录。

如果需要修改mysql安装位置,需要修改/etc/my.cnf里的basedir和下面./mysqld安装命令后的选项--basedir=/usr/local/mysql

#!/bin/bash
#新建mysql组 groupadd mysql
#新建mysql用户,指定组,不允许登录,不创建家目录。MySQL安装时不--user=mysql指定mysql用户好像是会报错或者提示,所以建议是新建mysql用户,也是基于安全考虑,哪怕数据库被黑,也不至于影响到root用户 useradd
-g mysql -s /sbin/nologin -M mysql
#-C解压到指定路径,/tmp有自动清空规则,Centos7是/usr/lib/tmpfiles.d/tmp.conf。
tar -xvf mysql-5.7.36-el7-x86_64.tar.gz -C /tmp
#有些人会做软链接到/usr/local/mysql。我这里是直接覆盖到这个目录,没做软链接。MySQL二进制安装包解压后的文件是有用处的,要么做软链接要么保留直接用
#因为/usr/local/mysql要有解压后的MySQL二进制文件里support-files/mysql.server默认配置的是/usr/local/mysql路径,这个在后面配置systemctl服务控制命令用到
mv /tmp/mysql-5.7.36-el7-x86_64 /usr/local/mysql
mkdir -pv /data/mysql/log/{iblog,binlog,relaylog}
mkdir -pv /data/mysql/{data,run,tmp}
chown -R mysql:mysql /data/mysql chmod -R 755 /data/mysql cd /usr/local/mysql/bin
#--initialize-insecure参数是用于创建一个密码为空的超级用户。--user=mysql指Run mysqld daemon as user。mysqld的帮助手册查看./mysqld --verbose --help ./mysqld --defaults-extra-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --initialize-insecure
#为什么不是在/etc/profile里改?其实/etc/profile和/etc/profile.d作用是一样的。只是profile里有指定循环读取profile.d下的环境配置文件。用profile.d好处在于可以实现解耦,避免频繁改动/etc/profile。变量$PATH前记得加\转义
echo "PATH=/usr/local/mysql/bin:\$PATH" >/etc/profile.d/mysqld.sh if [ -f /usr/local/mysql/support-files/mysql.server ]; then
#cp后,可以通过/etc/init.d/mysqld start启动mysqld cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld else echo "this is no mysql.server" fi sed -i 's/^mysqld_pid_file_path=.*$/mysqld_pid_file_path=\/data\/mysql\/run\/mysqld.pid/g' /etc/init.d/mysqld
#这里其实我们执行source /etc/profile也能让mysqld.sh生效 source
/etc/profile.d/mysqld.sh systemctl stop firewalld systemctl disable firewalld setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
#启动mysql。使用/etc/init.d/mysqld stop可停止mysql服务 /etc/init.d/mysqld start
echo "MySQL安装完成!如未正常启动,可尝试执行以下语句" echo "请执行'source /etc/profile.d/mysql.sh'" echo "请执行'/etc/init.d/mysqld start'"

有个地方我还没琢磨透,那就是按照以上操作后,其实也可以直接用systemctl status/stop/start mysqld来控制服务启停。但是按我的理解应该还不行,需要再额外配置。这个我再琢磨下

脚本3:MySQL初始化配置(可选)
该脚本内容主要配置MySQL密码为123456,以及删除一些默认用户

#!/bin/bash
mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DELETE FROM mysql.user WHERE host='localhost';"
mysql -e "DELETE FROM mysql.user WHERE host='localhost.localdomain';"
mysql -e "DELETE FROM mysql.user WHERE host='::1';"
mysql -e "SELECT host, user, authentication_string FROM mysql.user WHERE user = 'root';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '123456' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"

 PS:如果安装失败,或者想卸载,只需要删除/data/mysql和/usr/local/mysql就可以卸载干净

 

posted on 2018-09-30 14:15  标配的小号  阅读(1848)  评论(0编辑  收藏  举报

导航