对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