Install MySQL Single instance shell script on Linux
In the CentOS 6.5 environment test passed
#!/bin/bashuser=mysqlgroup=mysqlport=3306basedir=/usr/local/mysqldatadir=/data/mysql/mysql_${port}/ Datasourcefile=$1mysqlprofile=/etc/my.cnflogfile=/tmp/mysqlinstall.lognowtime= ' date ' +%Y-%m-%d %H:%M :%s ' Retval=0usage () { nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e ' \ e[0;36m${nowtime}[info]:usage: ' basename $0 ' mysql_source_file\e[m "}if [ $# != 1 ];then nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e "\e[0;36m${nowtime}[error]:input arguments error!\e[m" Usage exit 1fi[ ! -d $basedir ]&& mkdir -p $basedir [ ! -d $datadir ]&& mkdir -p $datadircontent = ' ls $basedir ' if [ ' x$content ' != ' x ' ];then nowtime= ' date ' +%y-%m-%d % h:%m:%s ' echo -e "\e[0;36m${nowtime}[error": ' $basedir ' is not null.\e[m ' exit 1ficontent1= ' ls $datadir ' if [ "x$content1" != "x" ];then Nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[error]: ' $ DataDir ' is not null.\e[m ' exit 1ficheck_port= ' netstat -na | grep ": ${port}" |awk ' {print $4} ' |grep ": ${port}" ' if [ "X$check_port" != "x" ];then nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e "\e[0;36m${nowtime}[error": ' $port ' port already used!\e[m ' exit 1fiif [ ! -f $sourcefile ];then nowtime= ' date ' +%Y-% m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[error]: ' $sourcefile ' IS Not exists.\e[M " usage exit 1else nowtime= ' date ' +%Y-%m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[info]:extract mysql install file.\e[m " tar xzf $sourcefile --strip-components 1 -c $ Basedir retval=$? nowtime= ' date ' +%y-%m-%d %h:%m:%s ' [ $retval -eq 0 ] && echo -e "\e[0;36m${nowtime}[info]: Extract mysql install file complete.\e[m "fiif [ $retval -ne 0 ];then nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e "\e[0;36m${nowtime}[error]:extract file error,please check your package.\e[m" echo 1fi#create group if not exists egrep "^ $group" /etc/group >& /dev/null if [ $? -ne 0 ] then groupadd $group nowtime= ' date ' +%y- %m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[info]:add group...\e[m "fi #create user if not exists egrep " ^ $user " /etc/passwd >& /dev/null if [ $? -ne 0 ] then useradd -g $group $user nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[ Info]:add user...\e[m "fi#create my.cnfdatapath=${datadir%/*}[ ! -d " ${datapath}/tmp " ] && mkdir -p ${datapath}/tmp[ ! -d "${datapath}/logs" ] && mkdir -p ${datapath}/logsif [ ! -f $mysqlprofile ]; Thennowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[info]:starting Create my.cnf.\e[m "cat > $mysqlprofile <<EOF[client]port = 3306socket = /tmp/mysql.sock# The MySQL server[mysqld]# Basicport = $portuser = $userbasedir = $basedirdatadir = $datadirtmpdir = $datapath/tmpsocket = /tmp/mysql.socklog-bin = $datapath/logs/mysql-binlog-error = $datapath/logs/error.logslow-query-log-file = $datapath/logs/ Slow.logskip-external-lockingskip-name-resolvelog-slave-updatesserver-id =2163306explicit_defaults_for_ Timestamp = 1character-set-server = utf8slow-query-logbinlog_format = mixedmax_ binlog_size = 128mbinlog_cache_size = 1mexpire-logs-days = 5back_log = 500long_query_time=1max_connections=1100max_user_connections=1000max_connect_errors=1000wait_timeout= 100interactive_timeout=100connect_timeout = 20slave-net-timeout=30max-relay-log-size = 256mrelay-log = relay-bintransaction_isolation = read-committedperformance_schema=0#myisam _recoverkey_buffer_size = 64mmax_allowed_packet = 16m#table_cache = 3096table_ open_cache = 6144table_definition_cache = 4096sort_buffer_size = 128kread_buffer_size = 1mread_rnd_buffer_size = 1Mjoin_buffer_size = 128Kmyisam_sort_buffer_size = 32Mtmp_table_size = 32mmax_heap_table_size = 64mquery_cache_type=0query_cache_size = 0bulk_insert_buffer_size = 32Mthread_cache_size = 64#thread_concurrency = 32thread_stack = 192kskip-slave-start# innodbinnodb_data_home_dir = $datadirinnodb _log_group_home_dir = $datapath/logsinnodb_data_file_path = ibdata1:1g:autoextendinnodb_buffer_pool_size = 500m#48g#innodb_buffer_pool_size = 33ginnodb_buffer_pool_instances = 8#innodb_additional_mem_pool_size = 16minnodb_log_file_size = 1024minnodb_log_buffer _size = 16minnodb_log_files_in_group = 3innodb_flush_log_at_trx_commit = 0innodb _lock_wait_timeout = 10innodb_sync_spin_loops = 40innodb_max_dirty_pages_pct = 90innodb_support_xa = 0innodb_thread_concurrency = 0innodb_thread_sleep_delay = 500innodb_file_io_threads = 4innodb_concurrency_tickets = 1000log_bin_trust_function_creators = 1innodb_flush_method = O_DIRECTinnodb_file_per_tableinnodb_read_io_threads = 16innodb_write_io_threads = 16innodb_io_capacity = 2000innodb_file_format = Barracudainnodb_purge_threads=1innodb_purge_batch_size = 32innodb_old_blocks_pct=75innodb_change_ Buffering=allinnodb_stats_on_metadata=off[mysqldump]quickmax_allowed_packet = 128m#myisam_max_sort_ file_size = 10g[mysql]no-auto-rehashmax_allowed_packet = 128mprompt = ' (product) \[email protected]\h [\d]> ' Default_character_set = utf8eofretval=$?finowtime= ' date ' +%y-%m-%d %h:%m:%S ' [ $retval -eq 0 ] && echo -e "\e[0;36m${nowtime}[info": Create my.cnf sucess.\e[m "#Initializing datadirif [ -d $datapath ]; then chown -r mysql:mysql ${datapath%/*} cd $basedir chown -r mysql:mysql * ./scripts/mysql_install_db -- user= $user --datadir= $datadir > $logfile 2>&1 retval=$? fiif [ $retval -ne 0 ];then nowtime= ' date ' +%Y-%m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[error]:mysql initializing Fail!\e[m " exit 1fi#Add env variablesgrep " $basedir/bin " /etc/ profile > /dev/null 2>&1if [ $? -ne 0 ];then nowtime= ' date ' +%y-%m-%d %h:%m:%s " echo " export path=\ $PATH: $basedir/bin ">>/etc/profile source /etc/profilefi#start mysqlif [ ! -f "/etc/init.d/mysqld" ];then cp $basedir/support-files/mysql.server /etc/init.d/mysqld /etc/init.d/mysqld start >/dev/null 2>&1 retval=$?fiif [ $retval -eq 0 ];then $basedir/bin/mysqladmin -u root password ' 123456 ' 2> $logfile nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e ' \e[0;36m${nowtime}[info]:starting mysql. success!\e[m " retval=0else nowtime= ' date ' +%Y-%m-%d %H:%M:%S " echo -e " \e[0;36m${nowtime}[error]:starting mysql. fail!\e[m "&NBSP;&NBSP;&NBSP;EXIT&NBSP;1FI#SETTING&Nbsp;account securityif [ $retval -eq 0 ];thenmysql -uroot -p123456 2>/dev/null <<EOFGRANT ALL PRIVILEGES ON *.* TO ' root ' @ '% ' IDENTIFIED BY ' 123456 ' WITH GRANT OPTION;delete from mysql.user where password = "; flush privileges; Eofretval=$?finowtime= ' date ' +%y-%m-%d %h:%m:%s ' [ $retval -eq 0 ] & & echo -e "\e[0;36m${nowtime}[info]:mysql initial password success!\e[m" if [ $retval -eq 0 ];then nowtime= ' date ' +%y-%m-%d %h:%m:%s ' ' echo -e ' \e[0;36m${nowtime}[info]:mysql initial password: 123456 \e[m " echo -e " \e[0;36m${nowtime}[info]:mysql basedir: $basedir \e[m " echo -e " \E[0;36M${NOWTIME}[INFO]:MYSQL&Nbsp;datadir: $datadir \e[m " echo -e " \e[0;36m${nowtime}[info]:mysql Install complete. \e[m "Else nowtime= ' date ' +%y-%m-%d %h:%m:%s ' echo -e "\e[0;36m${nowtime}[error]:change mysql password fail!\e[m" fi
Test results:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8A/E7/wKiom1g-jYLCH1sKAACKHyEvJQI189.png-wh_500x0-wm_3 -wmp_4-s_2890843265.png "style=" Float:none; "title=" Run Result "alt=" wkiom1g-jylch1skaackhyevjqi189.png-wh_50 "/>
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/8A/E3/wKioL1g-jYOgyys7AABq0qcMZBk040.png-wh_500x0-wm_3 -wmp_4-s_2513581164.png "style=" Float:none; "title=" B.png "alt=" Wkiol1g-jyogyys7aabq0qcmzbk040.png-wh_50 "/>
The MySQL configuration file Parameters section can also be optimized.
Automatically install MySQL scripts