对于多台服务器安装MySQL数据库,采用手工安装,是非常繁琐的,如果采取脚本批量安装,就非常的方便了,具体操作方法如下:
mkdir -p /Apps
mkdir -p /data
Chmod +x ./installMysql5.7.32
./installMysql5.7.32.sh
#!/bin/bash
port=`ss -tanl | grep 3306 |wc -l`
if [ $port -eq 1 ]; then
echo "3306 is exists please kill 3306!!"
exit
fi
if [ ! -d "/apps/" ];then
echo "The Directory:/apps/ no exist"
exit
fi
if [ ! -f "/apps/mysql-5.7.32-el7-x86_64.tar.gz" ];then
echo "The Directory:/apps/ No mysql-5.7.32-el7-x86_64.tar.gz please mysql-5.7.32-el7-x86_64.tar.gz to /apps/"
exit
fi
if [ ! -f "/apps/installMysql5.7.32.sh" ];then
echo "The Directory:/apps/ No installMysql5.7.32.sh please copy installMysql5.7.32.sh to /apps/"
exit
fi
if [ ! -d "/data/" ];then
echo "The Directory:/data/ no exist please mkdir /data"
exit
fi
echo "<---------------------begin--------------------->"
cd /apps/
tar -zxvf mysql-5.7.32-el7-x86_64.tar.gz
mv mysql-5.7.32-el7-x86_64 mysql
echo 'export PATH=/apps/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
source /etc/profile
rm -rf /data/*
mkdir /data/mysql
mkdir /data/mysql/data
mkdir /data/mysql/tmp
mkdir /data/mysql/var
mkdir /data/mysql/log
touch /data/mysql/log/slow.log
touch /data/mysql/log/mysqld.log
i=`cat /etc/passwd | cut -f1 -d':' | grep -w mysql -c`
if [ $i -le 0 ]; then
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql
fi
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /apps/mysql
chown mysql:mysql /apps/mysql
if [ -f "/etc/my.cnf" ];then
rm -f /etc/my.cnf
fi
server_id=`date +%Y%m%d%H%M%S`
cat > /etc/my.cnf << 'EOF'
[mysqld]
############# GENERAL #############
autocommit = ON
character_set_server = utf8
collation_server = utf8_general_ci
explicit_defaults_for_timestamp = ON
lower_case_table_names = 1
port = 3306
#read_only = ON
transaction_isolation = READ-COMMITTED
user =mysql
innodb_file_per_table = 1
#skip-grant-tables
############### PATH ##############
basedir = /apps/mysql
datadir = /data/mysql/data
tmpdir = /data/mysql/tmp
socket = /data/mysql/var/mysql.sock
pid_file = /data/mysql/var/mysql.pid
innodb_data_home_dir = /data/mysql/data
log_error = /data/mysql/log/error.log
general_log_file = /data/mysql/log/general.log
slow_query_log_file = /data/mysql/log/slow.log
#log_bin = /data/mysql/log/mysql-bin
#log_bin_index = /data/mysql/log/mysql-bin.index
relay_log = /data/mysql/log/relay-log
relay_log_index = /data/mysql/log/relay-log.index
############# INNODB #############
innodb_flush_method = O_DIRECT
innodb_buffer_pool_size = 30G
innodb_log_file_size = 1G
innodb_log_files_in_group = 4
innodb_flush_log_at_trx_commit = 2
innodb_support_xa = ON
innodb_strict_mode = ON
innodb_data_file_path = ibdata1:256M;ibdata2:256M:autoextend
innodb_temp_data_file_path = ibtmp1:512M:autoextend
innodb_lock_wait_timeout = 5
innodb_undo_tablespaces = 3
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 5120M
innodb_lock_wait_timeout = 50
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_io_capacity = 1200
innodb_io_capacity_max = 2400
innodb_lru_scan_depth = 512
####### CACHES AND LIMITS #########
interactive_timeout = 86400
wait_timeout = 86400
lock_wait_timeout = 50
max_allowed_packet = 256M
max_connect_errors = 2000
max_connections = 2000
max_user_connections = 2000
join_buffer_size = 4M
sort_buffer_size = 4M
#table_definition_cache = 5000
#table_open_cache = 8000
#table_open_cache_instances = 4
#thread_cache_size = 9
#thread_stack = 256K
tmp_table_size = 32M
max_heap_table_size = 32M
binlog_cache_size = 1M
############# SAFETY ##############
#local_infile = OFF
#{$validate_password_enable}plugin-load = validate_password.so
#skip_name_resolve = ON
#sql_mode = STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY
############# LOGGING #############
#general_log = 0
log_queries_not_using_indexes = OFF
log_throttle_queries_not_using_indexes = 0
log_slow_admin_statements = ON
log_error_verbosity = 3
long_query_time = 3
slow_query_log = ON
log_timestamps = SYSTE
############# REPLICATION #############
#binlog_checksum = CRC32
#master_verify_checksum = ON
#slave_sql_verify_checksum = ON
binlog_format = ROW
binlog_rows_query_log_events = ON
expire_logs_days = 3
max_binlog_size = 256M
enforce_gtid_consistency = ON
gtid_mode = ON
log_slave_updates = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
server_id=000001
#skip_slave_start = ON
#slave_net_timeout = 4
sync_binlog = 100
sync_master_info = 10000
sync_relay_log = 10000
sync_relay_log_info = 10000
slave_parallel_workers = 4
slave_parallel_type = LOGICAL_CLOCK
relay_log_recovery = ON
slave_skip_errors = all
[mysqld_safe]
log-error=/data/mysql/log/mysqld.log
pid-file=/data/mysql/var/mysqld.pid
[client]
#default-character-set=utf8
[mysql]
default-character-set=utf8
EOF
sed -i "s/server_id=000001/server_id=$server_id/g" /etc/my.cnf
cd /apps/mysql/bin
mysqld --initialize --user=mysql --basedir=/apps/mysql --datadir=/data/mysql/data
mysql_ssl_rsa_setup --datadir=/data/mysql/data
mysqld_safe > /dev/null 2>&1 &
sleep 15
ln -s /apps/mysql/bin/msql /usr/bin
passwd1=`cat /data/mysql/log/error.log | grep password | awk -F "root@localhost:" '{print $2}'| awk -F " " '{print $1}'`
echo "initial password:"$passwd1
passwd2="'test123'"
passwd3="test123"
echo "<---------------modify passwd------------------>"
mysql -uroot -h127.0.0.1 -p${passwd1} --connect-expired-password -e "alter user 'root'@'localhost' identified by ${passwd2}"
mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "grant all privileges on *.* to root@'%' identified by ${passwd2};"
echo "The root password has been changed:"$passwd3
mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';"
mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "grant replication slave,replication client on *.* to replic_user identified by 'test123';"
echo "The cluster user name have been created:replic_user"
echo "The cluster password have been created:test123"
mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "FLUSH PRIVILEGES;"
echo "<---------------end------------------>"
脚本内容可以根据自己的数据库版本进行调整,此脚本已在安装生产环境使用过,数据库的参数,可以根据自己的服务器配置进行调整。