在linux 上安装mysql单实例shell脚本
在centos 6.5环境测试通过
#!/bin/bash
user=mysql
group=mysql
port=3306
basedir=/usr/local/mysql
datadir=/data/mysql/mysql_${port}/data
sourcefile=$1
mysqlprofile=/etc/my.cnf
logfile=/tmp/mysqlinstall.log
nowtime=`date '+%y-%m-%d %h:%m:%s'`
retval=0
usage(){
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 1
fi
[ ! -d $basedir ]&& mkdir -p $basedir
[ ! -d $datadir ]&& mkdir -p $datadir
content=`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 1
fi
content1=`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 1
fi
check_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 1
fi
if [ ! -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 1
else
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"
fi
if [ $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 1
fi
#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.cnf
datapath=${datadir%/*}
[ ! -d "${datapath}/tmp" ] && mkdir -p ${datapath}/tmp
[ ! -d "${datapath}/logs" ] && mkdir -p ${datapath}/logs
if [ ! -f $mysqlprofile ];then
nowtime=`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 = 3306
socket = /tmp/mysql.sock
# the mysql server
[mysqld]
# basic
port = $port
user = $user
basedir = $basedir
datadir = $datadir
tmpdir = $datapath/tmp
socket = /tmp/mysql.sock
log-bin = $datapath/logs/mysql-bin
log-error = $datapath/logs/error.log
slow-query-log-file = $datapath/logs/slow.log
skip-external-locking
skip-name-resolve
log-slave-updates
server-id =2163306
explicit_defaults_for_timestamp = 1
character-set-server = utf8
slow-query-log
binlog_format = mixed
max_binlog_size = 128m
binlog_cache_size = 1m
expire-logs-days = 5
back_log = 500
long_query_time=1
max_connections=1100
max_user_connections=1000
max_connect_errors=1000
wait_timeout=100
interactive_timeout=100
connect_timeout = 20
slave-net-timeout=30
max-relay-log-size = 256m
relay-log = relay-bin
transaction_isolation = read-committed
performance_schema=0
#myisam_recover
key_buffer_size = 64m
max_allowed_packet = 16m
#table_cache = 3096
table_open_cache = 6144
table_definition_cache = 4096
sort_buffer_size = 128k
read_buffer_size = 1m
read_rnd_buffer_size = 1m
join_buffer_size = 128k
myisam_sort_buffer_size = 32m
tmp_table_size = 32m
max_heap_table_size = 64m
query_cache_type=0
query_cache_size = 0
bulk_insert_buffer_size = 32m
thread_cache_size = 64
#thread_concurrency = 32
thread_stack = 192k
skip-slave-start
# innodb
innodb_data_home_dir = $datadir
innodb_log_group_home_dir = $datapath/logs
innodb_data_file_path = ibdata1:1g:autoextend
innodb_buffer_pool_size = 500m
#48g
#innodb_buffer_pool_size = 33g
innodb_buffer_pool_instances = 8
#innodb_additional_mem_pool_size = 16m
innodb_log_file_size = 1024m
innodb_log_buffer_size = 16m
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 0
innodb_lock_wait_timeout = 10
innodb_sync_spin_loops = 40
innodb_max_dirty_pages_pct = 90
innodb_support_xa = 0
innodb_thread_concurrency = 0
innodb_thread_sleep_delay = 500
innodb_file_io_threads = 4
innodb_concurrency_tickets = 1000
log_bin_trust_function_creators = 1
innodb_flush_method = o_direct
innodb_file_per_table
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_io_capacity = 2000
innodb_file_format = barracuda
innodb_purge_threads=1
innodb_purge_batch_size = 32
innodb_old_blocks_pct=75
innodb_change_buffering=all
innodb_stats_on_metadata=off
[mysqldump]
quick
max_allowed_packet = 128m
#myisam_max_sort_file_size = 10g
[mysql]
no-auto-rehash
max_allowed_packet = 128m
prompt = '(product)\u@\h [\d]> '
default_character_set = utf8
eof
retval=$?
fi
nowtime=`date '+%y-%m-%d %h:%m:%s'`
[ $retval -eq 0 ] && echo -e "\e[0;36m${nowtime}[info]:create my.cnf sucess.\e[m"
#initializing datadir
if [ -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=$?
fi
if [ $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 1
fi
#add env variables
grep "$basedir/bin" /etc/profile > /dev/null 2>&1
if [ $? -ne 0 ];then
nowtime=`date '+%y-%m-%d %h:%m:%s'`
echo "export path=\$path:$basedir/bin">>/etc/profile
source /etc/profile
fi
#start mysql
if [ ! -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=$?
fi
if [ $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=0
else
nowtime=`date '+%y-%m-%d %h:%m:%s'`
echo -e "\e[0;36m${nowtime}[error]:starting mysql.. fail!\e[m"
exit 1
fi
#setting account security
if [ $retval -eq 0 ];then
mysql -uroot -p123456 2>/dev/null <<eof
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
delete from mysql.user where password = '';
flush privileges;
eof
retval=$?
fi
nowtime=`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 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