shell脚本一键自动安装mysql 5.7

post by rocdk890 / 2017-3-14 10:50 Tuesday linux技术
  最近开发要经常在不同环境下测试mysql 5.7,有时候要mysql同机共存多个mysql,今天先写个单机安装单个mysql,之后再写一键安装多个mysql的脚本.
  系统:centos 7.x(64位)
  软件版本:mysql 5.7.17
脚本:
cat auto_install_mysql5.7.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

# Check the network status
NET_NUM=`ping -c 4 www.baidu.com |awk '/packet loss/{print $6}' |sed -e 's/%//'`
if [ -z "$NET_NUM" ] || [ $NET_NUM -ne 0 ];then
        echo "Please check your internet"
        exit 1
fi

if [ -s mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz ];then
	echo -e "\033[40;31m mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz [found]\033[40;37m"
else
	#wget http://download.slogra.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
	wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
fi

user_mysql=`cat /etc/passwd|grep mysql|awk -F : '{print $1}'`
if [ -s /etc/my.cnf ]; then
	rm -f /etc/my.cnf
fi

if [ -z "$user_mysql" ];then
	groupadd mysql
	useradd -s /sbin/nologin -M -g mysql mysql
else
	echo "user mysql already exists!"
fi

rpm=`rpm -qa libaio|awk -F "-" '{print $1}'`
if [ -z $rpm ];then
	yum -y install libaio
else
 	echo -e "\033[40;31m libaio [found]\033[40;37m"
fi

mkdir -p /data/mysql
mkdir -p /var/log/mysql
touch /var/log/mysql/mysqld_safe.log
touch /var/log/mysql/general_query_log
touch /var/log/mysql/slow_query.log
touch /var/log/mysql/mysql-err.log

tar zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/log/mysql

/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql --pid-file=/data/mysql/mysql.pid
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
sed -i "s:^datadir=.*:datadir=/data/mysql:g" /etc/init.d/mysqld
cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
EOF

cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /data/mysql/mysql.sock

[mysql]
prompt = [\\u@\\h][\\d]>\\_
disable-auto-rehash
default-character-set = utf8
connect-timeout = 3

[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/mysql.pid
basedir = /usr/local/mysql
datadir = /data/mysql

character-set-server=utf8
default-storage-engine = innodb
lower_case_table_names=1

##################CONNECT#################
max_connections = 2000
max_connect_errors = 1000000
interactive_timeout = 28800
wait_timeout = 28800
connect_timeout = 20
back_log = 500
#FOR FAST CREATE THREAD
thread_cache_size = 300
open_files_limit = 10240
table_open_cache = 3000

###############THREAD BUFFER##############
###############FOR EVERY THREAD###########
sort_buffer_size = 1M
join_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
#net_buffer_length = 16k
tmp_table_size = 64M
max_allowed_packet = 128M
#FOR MEMORY ENGINE
max_heap_table_size = 64M

#################BUFFER##################
###############FOR ALL THREAD############
query_cache_type = 0
query_cache_size = 32M
query_cache_limit = 1M

#################LOG BUFFER#############
binlog_cache_size = 16M
max_binlog_cache_size = 32M
binlog_stmt_cache_size = 32M

#################LOG FILE###############
sync_binlog = 0
binlog_format = row
log-error = /var/log/mysql/mysql-err.log
log-bin = /data/mysql/master-bin
max_binlog_size = 512M
relay-log = /data/mysql/relay-bin
log_output = file
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow_query.log
long_query_time=10
general_log = 0
general_log_file = /var/log/mysql/general_query_log
expire-logs-days = 3

###############INNODB##################
#innodb_page_size = 16k
innodb_file_per_table = 1
#innodb_data_file_path = ibdata1:12M:autoextend
#innodb_log_file_size = 512M
#innodb_log_files_in_group = 3
#innodb_buffer_pool_size = 500M
#innodb_buffer_pool_instance=8
innodb_log_buffer_size = 8m
innodb_max_dirty_pages_pct = 90
innodb_thread_concurrency = 0
innodb_flush_method = O_DIRECT
innodb_flush_log_at_trx_commit = 0
innodb_lock_wait_timeout = 100
innodb_fast_shutdown = 1
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_io_capacity = 1000
innodb_use_native_aio = 1
innodb_stats_on_metadata = 0
innodb_strict_mode = 1
innodb_file_format = barracuda
innodb_file_format_max = barracuda
innodb_file_format_check = 1

###############MYISAM###################
key_buffer_size = 64M
key_cache_block_size = 64k
myisam_sort_buffer_size = 64M

#################REPLICATE###############
server_id =1
log_slave_updates=1
#binlog-do-db = brent
#binlog-ignore-db = mysql
#replicate_ignore_db=mysql
#replicate_do_table=brent.t1
#replicate_ignore_table=brent.t2

#######GTID#######
#gtid-mode = on
#enforce-gtid-consistency = true
#master-info-repository=TABLE
#master-info-repository=TABLE
#relay-log-info-repository=TABLE
#sync-master-info=1
#slave-parallel-workers=2
#binlog-checksum=CRC32
#master-verify-checksum=1
#slave-sql-verify-checksum=1
#binlog-rows-query-log_events=1
#report-port=3306
#report-host=localip
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
err-log = /var/log/mysql/mysqld_safe.log
pid-file = /data/mysql/mysql.pid
EOF

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
#echo 'export PATH=$PATH:/usr/local/bin' >> /etc/profile
source /etc/profile

service mysqld start
echo -e "\033[40;31mInstall mysql success! \033[40;37m"
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-688.html

标签: centos mysql 安装 shell 脚本 install 一键 自动 5.7 mysql5.7

  1. 2018-03-01 21:39
    @小头爸爸:我不知道别人的怎么做的,但我是在写的过程自己想好下一步做什么,写完后最后跑一遍.
  1. gravatar 小头爸爸
    2018-02-28 10:27
    请教楼主shell的编程经验,这些shell是如果一步一步调试出来的?或者说怎么确保每一段逻辑都是正确的,都说IDE是vi直接编辑,那么,这么大一段代码,写一段,保存一下,执行一下?

评论: