mysql8.0.22版本自动安装脚本
[root@localhost Dockerfile]# cat setup.sh
#!/bin/bash
#if [ $# -ne 2 ] ;then
# exit
#fi
export PATH=$PATH:/home/db/mysql/bin:/home/db/mysql/lib
# MASTER=false
SLAVE=$1
#slave_ip=$2
source /etc/profile
Init_env()
{
# path
mkdir -p /home/db/mysql /home/db/mysql/scripts /mysqlbackup /mysqldata /mysqldata/data /mysqldata/tmp /mysqldata/logs /mysqlbinlog /mysqlbinlog/relay /home/db/mysql/scripts
# group
groupadd mysql
# user
useradd -d /home/db/mysql -g mysql -m mysql
chown mysql:mysql /mysqldata
chown mysql:mysql /mysqldata/data
chown mysql:mysql /mysqldata/tmp
chown mysql:mysql /mysqldata/logs
chown mysql:mysql /mysqlbinlog
chown mysql:mysql /mysqlbinlog/relay
chown mysql:mysql /mysqlbackup
ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
if [ ! -f '/home/db/mysql/support-files/mysql.server' ]; then
# unzip mysql.tar.gz
echo "unzip mysql.tar.gz"
tar zxvf ./mysql8.0.22.tar.gz -C /home/db/mysql
fi
# mysqld
cp /home/db/mysql/support-files/mysql.server /etc/init.d/mysqld
# privileges
chmod +x /etc/init.d/mysqld
chmod o+x -R /home/db/mysql/bin
# ln /home/db/my.cnf /mysqldata/my.cnf
# my.cnf
echo "
[mysql]
port = 13306
# socket = /home/db/mysql/mysql.sock
socket = /mysqldata/mysql.sock
init-command = set names utf8mb4
prompt=\u@\h\\\R:\\\m:\\\s[\d]>
[mysqld]
server-id = 10 #denpend on server_id rule (slave:20/30/40)
port = 13306
user = mysql
#user = root
basedir = /home/db/mysql
#password=Root@123
default_authentication_plugin=mysql_native_password
############## M-S relpicate ###########
server-id = 10
log-bin=/mysqlbinlog/master-bin #slave-->/mysqlbinlog/slave[n]-bin
relay_log=/mysqlbinlog/relay/master-relay-bin #slave--/mysqlbinlog/relay/slave-relay-bin
#read_only=ON
secure-file-priv='/tmp'
datadir = /mysqldata/data
tmpdir = /mysqldata/tmp
# socket = /home/db/mysql/mysql.sock
socket = /mysqldata/mysql.sock
character_set_server=utf8mb4
collation_server= utf8mb4_bin
event_scheduler=OFF
lower_case_table_names=1
pid-file=/mysqldata/data/mysqld.pid
autocommit=0
log_timestamps=SYSTEM
binlog_rows_query_log_events=off
# show_compatibility_56=on
############# replication #############
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000 # 1 second
#rpl_semi_sync_slave_enabled=1
############# binlog #############
# log-bin=/mysqlbinlog/master-bin #slave-->/mysqlbinlog/slave[n]-bin
binlog_cache_size=2M
# expire_logs_days=10
binlog_expire_logs_seconds=864000
############# replication #############
master_info_repository=table
relay_log_info_repository=table
gtid-mode=ON
enforce-gtid-consistency=ON
log-slave-updates=1
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=4
# relay_log=/mysqlbinlog/relay/master-relay-bin #slave--/mysqlbinlog/relay/slave-relay-bin
############# slow log #############
slow_query_log=1
slow_query_log_file = /mysqldata/logs/mysql_slow.log
long_query_time = 1
############# error log #############
log-error =/mysqldata/logs/mysql_error.log
############# thread #############
max_connections = 4000 #dependent on machine
key_buffer_size = 256M
max_allowed_packet = 32M
table_open_cache = 4000
table_open_cache_instances = 4 #CPU COUNT
sort_buffer_size = 8M
read_rnd_buffer_size=1M
join_buffer_size = 2M
tmp_table_size = 64M
max_heap_table_size = 128M
############# innodb #############
innodb_data_file_path=ibdata1:24M:autoextend
innodb_buffer_pool_size = 4G #physical memory’s 50%
innodb_buffer_pool_instances = 4 #CPU COUNT
innodb_log_file_size = 1GB
innodb_log_files_in_group = 4
innodb_log_buffer_size = 32M
innodb_lock_wait_timeout = 600
innodb_thread_concurrency = 4 #CPU COUNT
innodb_flush_method=O_DIRECT
innodb_read_io_threads =16
innodb_write_io_threads =16
innodb_io_capacity = 800
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:2G
innodb_flush_log_at_timeout=2
log_bin_trust_function_creators=1
transaction_isolation=read-committed
innodb_undo_directory=/mysqldata/data
innodb_undo_log_truncate=1
innodb_undo_tablespaces=4
innodb_rollback_segments=128
innodb_max_undo_log_size=2G
innodb_purge_rseg_truncate_frequency=16
innodb_max_undo_log_size=2147483648
############# safe #############
# plugin-load=validate_password.so
# validate-password=FORCE_PLUS_PERMANENT
############# other #############
init_file=/home/db/mysql/scripts/performance_collection
default-time-zone=SYSTEM
[client]
port = 13306
user=root
# socket = /home/db/mysql/mysql.sock
socket = /mysqldata/mysql.sock
password=Root@123
#[mysqladmin]
#user=root
#password=Root@123" > /etc/my.cnf
# /home/db/mysql/scripts/performance_collection
echo "
UPDATE performance_schema.setup_consumers SET enabled = 'yes';
UPDATE performance_schema.setup_instruments SET enabled='yes' ,timed='yes';" > /home/db/mysql/scripts/performance_collection
# /etc/security/limits.conf
echo "
mysql soft nofile 655360
mysql soft stack 10240
mysql soft nproc 65536" >> /etc/security/limits.conf
# /etc/selinux/config
sed -i 's/\SELINUX/\#SELINUX/g' /etc/selinux/config
sed -i 's/\SELINUXTYPE/\#SELINUXTYPE/g' /etc/selinux/config
# /home/db/.bash_profile
echo "
export MYSQL_HOME=/home/db/mysql
export PATH=\$PATH:/home/db/mysql/bin:/home/db/mysql/lib
export PS1='\$LOGNAME\@'`hostname`:'\$PWD''\$ '
if [ -t 0 ]; then
stty intr ^C
fi
" > /home/db/mysql/.bash_profile
# /etc/profile
echo "
export PATH=\$PATH:/home/db/mysql/bin:/home/db/mysql/lib" >> /etc/profile
# /etc/sysctl.conf
echo "
fs.aio-max-nr = 4194304
fs.file-max = 6815744
kernel.shmall = 167772160
kernel.shmmax = 687194767360
kernel.shmmni = 4096
kernel.sem = 10000 40960000 10000 4096
net.ipv4.ip_local_port_range = 20000 65535
#避免OOM
vm.swappiness=1 " >> /etc/sysctl.conf
chown mysql.mysql -R /home/db/*
chmod 765 -R /home/db/mysql/bin/*
}
InitUser()
{
echo '==>init root user password to Root@0101'
tmp_password=`cat /mysqldata/logs/mysql_error.log | grep "A temporary password is generated for" | awk -F 'host:' '{print $2}' | tr -d ' '`
mysql -uroot -p$tmp_password --connect-expired-password -e "alter user root@'localhost' identified by 'Root@0101'; flush privileges;"
mysql -uroot -pRoot@0101 -e "use mysql;update user set host='%' where user='root';flush privileges"
mysql -uroot -pRoot@0101 -e "create user repl@'%' identified by 'Repl@0101';
grant replication slave on *.* to repl@'%';
create user patrol@'127.0.0.1' identified by 'Patrol@0101';
grant select, process, replication client on *.* to patrol@'127.0.0.1';
create user mymon@'%' identified by 'Mymon@0101';
grant select,process, replication slave, show databases, show view ,replication client on *.* to mymon@'%';
create user applaud@'%' identified by 'Applaud@0101';
grant select on *.* to applaud@'%';
create user xtrabk@'127.0.0.1' identified by 'Xtrabk@0101';
grant reload, lock tables, process, replication client on *.* to xtrabk@'127.0.0.1';
create user mha@'%' identified by 'Mha@0101';
grant all on *.* to mha@'%';
flush privileges;
reset master;"
mysql -uroot -pRoot@0101 -e "INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_master_enabled = 1;SET GLOBAL rpl_semi_sync_master_timeout = 1000;SET GLOBAL rpl_semi_sync_slave_enabled = 1;"
sed -i 's/\#rpl_semi_sync/rpl_semi_sync/g' /etc/my.cnf
}
Stop_db()
{
mysqladmin shutdown -u root -pRoot@0101 --socket=/home/db/mysql/mysql.sock
}
Start_db_finaly()
{
mysqld_safe --defaults-file=/etc/my.cnf
}
CreateMaster()
{
echo 'Config my.cnf and Create master db'
}
CreateSlave()
{
echo 'Config slave my.cnf'
id=`date +%s | cut -c 1-8`
sed -i "s/server-id = 10/server-id = $id/g" /etc/my.cnf
sed -i "s/master-bin/slave$id-bin/g" /etc/my.cnf
sed -i "s/master-relay-bin/slave-relay-bin/g" /etc/my.cnf
}
CreateDB()
{
echo "==>create and init db"
/home/db/mysql/bin/mysqld --initialize --user=mysql >/dev/null 2>/dev/null
echo "==>sed /home/db/mysql/bin/mysqld_safe"
sed -i 's/\/usr\/local\/mysql\/data/\/mysqldata\/data/g' /home/db/mysql/bin/mysqld_safe
sed -i 's/\/usr\/local\/mysql/\/home\/db\/mysql/g' /home/db/mysql/bin/mysqld_safe
chown mysql:mysql /mysqldata
chown mysql:mysql /mysqldata/data
chown mysql:mysql /mysqldata/tmp
chown mysql:mysql /mysqldata/logs
chown mysql:mysql /mysqlbinlog
chown mysql:mysql /mysqlbinlog/relay
chown mysql:mysql /mysqlbackup
}
Start_db()
{
echo "==>/home/db/mysql/bin/mysqld_safe"
/home/db/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf >/dev/null 2>/dev/null &
while true
do
pfile=/mysqldata/data/mysqld.pid
if [ ! -f $pfile ] ;then
sleep 3
else
pid=`cat /mysqldata/data/mysqld.pid`
status=`ps -ef | grep $pid |grep -v grep | wc -l`
if [ "$status" -eq 1 ] ; then
break
fi
sleep 3
fi
done
}
file=/mysqldata/logs/mysql_error.log
if [ ! -f $file ]; then
if [ ${SLAVE} == 'true' ] ;then
CreateSlave
fi
Init_env
CreateDB
Start_db
InitUser
Stop_db
Start_db
if [ ${SLAVE} == 'true' ]; then
mysql -uroot -pRoot@0101 -e "CHANGE MASTER TO MASTER_HOST='${MASTER_IP}',MASTER_PORT=13306,MASTER_USER='repl',MASTER_PASSWORD='Repl@0101', MASTER_AUTO_POSITION=1;start slave;"
fi
tail -f /mysqldata/logs/mysql_error.log
#Start_db_finaly
else
echo "==>/home/db/mysql/bin/mysqld_safe"
Start_db_finaly
fi