Centos7.3 之mysql5.7二进制脚本一键安装

时间:2021-03-21 21:01:25

#!/bin/bash

#注意,该脚本是在centos7.3非生产环境下测试的,其他版本的系统可能不适用,要根据情况修改。需要先下载好mysql二进制包到本地(我一般都是在root家目录下操作,文件也都是放在root家目录下),另外确认root家目录下不存在mysql目录且,/data和/usr/local/不存在mysql目录
rpm -qa |grep mysql
groupadd mysql
useradd -g mysql mysql
echo "解压中,请勿操作"
tar -xf mysql*
echo "解压完成"
rm -rf *.tar.gz
mv mysql* mysql
mkdir /data
mv mysql /data/mysql
ln -sv /data/mysql /usr/local/
cd /usr/local/mysql
mkdir -pv /data/mysql/data
mkdir -pv /data/mysql/log/iblog
mkdir -pv /data/mysql/log/binlog
mkdir -pv /data/mysql/log/relaylog
mkdir -pv /data/mysql/run
mkdir -pv /data/mysql/tmp
chown -R mysql:mysql /data/mysql
chmod -R 755 /data/mysql

cat > /etc/my.cnf <<EOF
# 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.

#[mysqld]

# 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 = 128M

# 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 = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# 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

[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 = /data/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


cd /data/mysql/bin
./mysqld --defaults-extra-file=/etc/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql --initialize-insecure #最后这个初始化安全是用来不初始化密码的,也就是不用密码的

./mysql_ssl_rsa_setup --basedir=/data/mysql --datadir=/data/mysql/data #MySQL 5.7.6 版本之后,MySQL SSL Certificate and RSA Key Generation Utility,好像不执行也可以

echo "PATH=/data/mysql/bin:$PATH" >/etc/profile.d/mysql.sh

if [ -f /data/mysql/support-files/mysql.server ]; then
cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
else
echo "不存在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.d/mysql.sh
service mysqld start
echo "mysql安装完成"
echo "注意:如果识别不了mysql命令,请执行source /etc/profile.d/mysql.sh"
echo "注意:启动mysql时,需要使用service mysqld start,使用systemctl start mysqld 可能还识别不出mysql,启动mysql后,查看端口起来后才能进入mysql,mysql -uroot -p直接回车即可进入数据库"
chkconfig --add mysqld #设置开机自启
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

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 user WHERE mysql.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;"

 本文已经过二次修改,上面脚本经测试没有什么大问题,可以正常跑下去安装mysql数据库