1. 程式人生 > >CentOS_6.7指令碼靜默安裝MySQL5.6

CentOS_6.7指令碼靜默安裝MySQL5.6

對shell不是很熟悉, 斷斷續續的寫了一個晚上,終於搞定

使用方式,root使用者解壓到某個目錄下, 執行 ./autoinstall_mysql_5.6.sh 即可, 後面如果不加引數, 則預設安裝3306埠的例項,可以加埠引數改變預設埠。

比如 ./autoinstall_mysql_5.6.sh 3307

指令碼會新增環境變數 $MYSQL_HOME指向二進位制可執行程式的目錄

指令碼會同時在home下新建3個sh,分別用於啟動mysql,停止mysql和本機OS免登mysql

機器需要安裝yum和wget,perl,用於下載二進位制可執行檔案。

yum install -y wget

yum install -y perl

yum install -y libaio

如果是要裝mysql5.7的話,可能需要調整一下指令碼, 修改裡面的安裝檔名,還有5.7的scripts目錄已經遷移到了bin目錄,需要做一些調整。

不過目前看5.6還是主流。

指令碼預設會在/mysql/server下建立二進位制執行檔案

在/mysql/下建立例項資料/日誌檔案

主要的指令碼內容如下

autoinstall_mysql_5.6.sh

#port
port=3306
if [ $# -gt 0 ]; then
	port=$1
fi

echo 'install mysql service on port:'${port}

yum install -y wget
yum install -y perl
yum install -y libaio

#const
basedir=/mysql/server
instance_name=my${port}
instance_home=/mysql/${instance_name}
file_name=mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
download_mysql_url=https://dev.mysql.com/get/Downloads/MySQL-5.6/${file_name}

# create user and group
num=`cat /etc/group | grep mysql | wc -l`
if [ $num -eq 0 ]; then
	groupadd mysql
fi
num=`cat /etc/passwd | grep mysql | wc -l`
if [ $num -eq 0 ]; then
	useradd -r -g mysql -s /bin/false mysql
fi

function create_instance_home(){
	mkdir -p ${instance_home}/log/{binlog,iblog}
	mkdir -p ${instance_home}/data
	mkdir -p ${instance_home}/tmp
	chown -R mysql:mysql ${instance_home}
}

#env
if [ ! $MYSQL_HOME ]; then
	echo 'export MYSQL_HOME='${basedir} >> ~/.bash_profile
	echo 'export PATH=$PATH:$MYSQL_HOME/bin' >> ~/.bash_profile
	echo 'please exec "source ~/.bash_profile" command first, then re-exec this install-shell'
	exit 0
fi

# create server
if [ ! -f ${basedir}/bin/mysql ]; then
	mkdir -p ${basedir}
	if [ ! -f ${file_name} ]; then
		wget ${download_mysql_url}
	fi
	tempdir=`mktemp -d`
	tar xf ${file_name} --strip-components 1 -C ${tempdir}
	mv ${tempdir}/* ${basedir} && rm -rf ${tempdir}
fi

# recreate
if [ -d ${instance_home} ]; then
	echo "instance_home ${instance_home} exists, remove it ? Y/N"
	read choose
	case ${choose} in
		Y | y)
			rm -rf ${instance_home} ;;
		*)
			exit 0
	esac
fi


# create instance
create_instance_home

# sed shell and config
rm -f ~/start_${instance_name}.sh
sed -e 's:${instance_home}:'${instance_home}':g' start_mysql.sh > ~/start_${instance_name}.sh
chmod +x ~/start_${instance_name}.sh

rm -f ~/stop_${instance_name}.sh
sed -e 's:${instance_home}:'${instance_home}':g' stop_mysql.sh > ~/stop_${instance_name}.sh
chmod +x ~/stop_${instance_name}.sh

rm -f ~/login_${instance_name}.sh
sed -e 's:${instance_home}:'${instance_home}':g' login_mysql.sh > ~/login_${instance_name}.sh
chmod +x ~/login_${instance_name}.sh

sed -e 's:${basedir}:'${basedir}':g'  -e 's:${port}:'${port}':g' -e 's:${instance_home}:'${instance_home}':g' my.cnf > ${instance_home}/my.cnf
chown mysql:mysql ${instance_home}/my.cnf

cd ${MYSQL_HOME}
./scripts/mysql_install_db --defaults-file=${instance_home}/my.cnf --user=mysql

my.cnf

[client]
port=${port}
socket=${instance_home}/mysql.sock

[mysql]
pid_file=${instance_home}/mysql.pid

[mysqld]
autocommit=1
general_log=on
explicit_defaults_for_timestamp=true

# system
basedir=${basedir}
datadir=${instance_home}/data
max_allowed_packet=1g
max_connections=3000
max_user_connections=2800
open_files_limit=65535
pid_file=${instance_home}/mysql.pid
port=${port}
server_id=10${port}
skip_name_resolve=ON
socket=${instance_home}/mysql.sock
tmpdir=${instance_home}/tmp

#binlog
log_bin=${instance_home}/log/binlog/log-bin
binlog_cache_size=32768
binlog_format=row
expire_logs_days=7
log_slave_updates=ON
max_binlog_cache_size=2147483648
max_binlog_size=524288000
sync_binlog=100

#logging
log_error=${instance_home}/log/error.log
slow_query_log_file=${instance_home}/log/slow.log
log_queries_not_using_indexes=0
slow_query_log=1
log_slave_updates=ON
log_slow_admin_statements=1
long_query_time=1

#relay
relay_log=${instance_home}/log/relaylog
relay_log_index=${instance_home}/log/relay.index
relay_log_info_file=${instance_home}/log/relay-log.info

#slave
slave_load_tmpdir=${instance_home}/tmp
slave_skip_errors=OFF

#innodb
innodb_data_home_dir=${instance_home}/log/iblog
innodb_log_group_home_dir=${instance_home}/log/iblog
innodb_adaptive_flushing=ON
innodb_adaptive_hash_index=ON
innodb_autoinc_lock_mode=1
innodb_buffer_pool_instances=8

#default
innodb_change_buffering=inserts
innodb_checksums=ON
innodb_buffer_pool_size= 128M
innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend
innodb_doublewrite=ON
innodb_file_format=Barracuda
innodb_file_per_table=ON
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_io_capacity=1000
innodb_lock_wait_timeout=10
innodb_log_buffer_size=67108864
innodb_log_file_size=1048576000
innodb_log_files_in_group=4
innodb_max_dirty_pages_pct=60
innodb_open_files=60000
innodb_purge_threads=1
innodb_read_io_threads=4
innodb_stats_on_metadata=OFF
innodb_support_xa=ON
innodb_use_native_aio=OFF
innodb_write_io_threads=10

[mysqld_safe]
datadir=${instance_home}/data


login_mysql.sh

cd $MYSQL_HOME
./bin/mysql -S ${instance_home}/mysql.sock


start_mysql.sh

cd $MYSQL_HOME
./bin/mysqld_safe --defaults-file=${instance_home}/my.cnf --user=mysql &


stop_mysql.sh

cd $MYSQL_HOME
./bin/mysqladmin -S ${instance_home}/mysql.sock shutdown


共5個檔案。
也可以通過百度雲盤下載:

http://pan.baidu.com/s/1jIJVaKI

end