首页 > 代码库 > MySQL5.6 一键安装脚本
MySQL5.6 一键安装脚本
#!/bin/sh
#*************************
#Created By: yixianwei.cn
#*************************
yum install -y grep procps coreutils xfsprogs util-linux-ng e2fsprogs libselinux-utils chkconfig wget yum-utils gawk libaio cronie xz
if [ -b /dev/sdb ] ;then
if [ -z "`mount |grep /dev/sdb`" ] ;then
mkfs.xfs -b size=4096 -d sectsize=4096 -i attr=2 -l lazy-count=1,sectsize=4096 -L data /dev/sdb
if [ -z "`blkid /dev/sdb`" ] ;then
uuidgen | xargs tune2fs /dev/sdb -U
fi
sdbuuid="`blkid -o value -s UUID /dev/sdb`"
if [ -z "`grep $sdbuuid /etc/fstab`" ] ;then
echo "UUID=$sdbuuid /data xfs rw,noatime,noikeep,allocsize=16M,attr2,largeio,inode64 0 0" >> /etc/fstab
echo "echo \"deadline\" >/sys/block/sdb/queue/scheduler" > /etc/profile.d/skyeye_mysql.sh
sh /etc/profile.d/skyeye_mysql.sh
fi
mkdir -p /data
mount -a
else
mkdir -p /data
fi
else
mkdir -p /data
fi
if [ -z "`grep -i mysql /etc/security/limits.conf`" ] ;then
echo "mysql - proc 10240" >> /etc/security/limits.conf
echo "mysql - nofile 65535" >> /etc/security/limits.conf
ulimit -u 10240
ulimit -n 65535
fi
if [ -z "`grep -i tcp_timestamps /etc/sysctl.conf`" ] ;then
echo "net.ipv4.tcp_timestamps = 1" >> /etc/sysctl.conf
fi
if [ -z "`grep -i tcp_tw_recycle /etc/sysctl.conf`" ] ;then
echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf
fi
if [ -z "`grep -i tcp_tw_reuse /etc/sysctl.conf`" ] ;then
echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
fi
if [ -z "`grep -i swappiness /etc/sysctl.conf`" ] ;then
echo "vm.swappiness = 1" >> /etc/sysctl.conf
fi
if [ -z "`grep -i aio-max-nr /etc/sysctl.conf`" ] ;then
echo "fs.aio-max-nr = 1048576" >> /etc/sysctl.conf
fi
if [ -z "`grep -i ip_local_port_range /etc/sysctl.conf`" ] ;then
echo "net.ipv4.ip_local_port_range = 10000 65535" >> /etc/sysctl.conf
fi
sysctl -p
setenforce 0
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/‘ /etc/selinux/config
service iptables stop
service ip6tables stop
chkconfig iptables off
chkconfig ip6tables off
service mysql stop
service mysqld stop
mysqld_multi --user=root stop
killall -9 mysqld_safe
killall -9 mysqld
oldmysqlserver="`rpm -qa|egrep -i mysql-server`"
if [ -n "$oldmysqlserver" ] ;then
yum erase -y $oldmysqlserver
fi
yum localinstall -y mysql-community-release-el6-5.noarch.rpm
mysqlrepo="`yum repolist all | egrep -i mysql[0-9]+-community |grep enabled|awk ‘{print $1}‘`"
yum-config-manager --disable $mysqlrepo
yum-config-manager --enable mysql56-community
yum install -y mysql-community-client*`uname -m`.rpm mysql-community-devel*`uname -m`.rpm mysql-community-server*`uname -m`.rpm mysql-community-common*`uname -m`.rpm mysql-community-libs*`uname -m`.rpm mysql-community-libs-compat*`uname -m`.rpm
service mysqld stop
mkdir -p /data/mysql
chown mysql:mysql /data/mysql
if [ ! -d /data/mysql/mysqldata3306 ] ;then
mkdir -p /data/mysql/mysqldata3306
mkdir -p /data/mysql/mysqldata3306/mydata
mkdir -p /data/mysql/mysqldata3306/binlog
mkdir -p /data/mysql/mysqldata3306/innodb_ts
mkdir -p /data/mysql/mysqldata3306/innodb_log
mkdir -p /data/mysql/mysqldata3306/relaylog
mkdir -p /data/mysql/mysqldata3306/tmpdir
mkdir -p /data/mysql/mysqldata3306/log
mkdir -p /data/mysql/mysqldata3306/sock
chown -R mysql:mysql /data/mysql/mysqldata3306
fi
if [ -f /etc/my.cnf ] ;then
mv -f /etc/my.cnf "/etc/my.cnf.`date +%s`.bak"
fi
if [ -f /etc/mysql/my.cnf ] ;then
mv -f /etc/mysql/my.cnf "/etc/mysql/my.cnf.`date +%s`.bak"
fi
if [ -f /usr/etc/my.cnf ] ;then
mv -f /usr/etc/my.cnf "/usr/etc/my.cnf.`date +%s`.bak"
fi
if [ -f ~/.my.cnf ] ;then
mv -f ~/.my.cnf "~/.my.cnf.`date +%s`.bak"
fi
if [ -f /root/.my.cnf ] ;then
mv -f /root/.my.cnf "/root/.`date +%s`.bak"
fi
if [ -f /var/lib/mysql/.my.cnf ] ;then
mv -f /var/lib/mysql/.my.cnf "/var/lib/mysql/.`date +%s`.bak"
fi
mysqlhome="`grep ^mysql /etc/passwd |awk -F‘:‘ ‘{print $6}‘`"
if [ -f "$mysqlhome/.my.cnf" ] ;then
mv -f $mysqlhome/.my.cnf "$mysqlhome/.`date +%s`.bak"
fi
bpsize="`free -m |grep Mem:|awk ‘{print int($2/2)==$2/2?$2/2:int($2/2)+1}‘`M"
iocapacity="`df -B GB --total /data |grep total|awk ‘{sub(/GB/,\"\",$2);print ($2/300==int($2/300)?$2/300:int($2/300)+1)*100}‘`"
cat >> /etc/my.cnf <<EOF
#*************************
#Created By: yixianwei.cn
#*************************
[mysqld]
user=mysql
pid-file=/data/mysql/mysqldata3306/sock/mysql.pid
socket=/data/mysql/mysqldata3306/sock/mysql.sock
datadir=/data/mysql/mysqldata3306/mydata
tmpdir=/data/mysql/mysqldata3306/tmpdir
skip-name-resolve
performance_schema=OFF
transaction-isolation=READ-COMMITTED
character_set_server=utf8
collation_server=utf8_general_ci
lower_case_table_names=1
explicit_defaults_for_timestamp
group_concat_max_len=1048576
max_connections=800
max_connect_errors=9999
wait_timeout=172800
interactive_timeout=172800
max_allowed_packet=64M
read_rnd_buffer_size=2M
query_cache_type=1
query_cache_size=32M
query_cache_limit=16K
log-error=/data/mysql/mysqldata3306/log/error.log
log_warnings=2
long_query_time=0.3
slow_query_log
slow_query_log_file=/data/mysql/mysqldata3306/log/slow-query.log
server-id=3306
log-bin-index=/data/mysql/mysqldata3306/binlog/mysql-bin.index
#log-bin=/data/mysql/mysqldata3306/binlog/mysql-bin
binlog-format=MIXED
binlog_cache_size=256K
sync_binlog=1
expire_logs_days=15
log-slave-updates
sysdate-is-now
innodb_support_xa=0
relay-log-index=/data/mysql/mysqldata3306/relaylog/mysql-relay-bin.index
relay-log=/data/mysql/mysqldata3306/relaylog/mysql-relay-bin
innodb_buffer_pool_size=${bpsize}
innodb_data_home_dir=/data/mysql/mysqldata3306/innodb_ts
innodb_data_file_path=ibdata1:256M:autoextend
innodb_open_files=7168
innodb_flush_log_at_trx_commit=1
innodb_log_file_size=4G
innodb_log_group_home_dir=/data/mysql/mysqldata3306/innodb_log
innodb_adaptive_flushing_lwm=30
innodb_flush_method=O_DIRECT
innodb_io_capacity=${iocapacity}
[mysql]
no-auto-rehash
prompt="\\u@\\h : \\d \\r:\\m:\\s> "
default-character-set=utf8
socket=/data/mysql/mysqldata3306/sock/mysql.sock
[mysqldump]
user=skyeyebackup
password=skyeyebackup
socket=/data/mysql/mysqldata3306/sock/mysql.sock
hex-blob
max_allowed_packet=2G
default-character-set=utf8
single-transaction
EOF
chown mysql:mysql /etc/my.cnf
chmod 400 /etc/my.cnf
if [ ! -d /data/mysql/mysqldata3306/mydata/mysql ] ;then
mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql/mysqldata3306/mydata --skip-name-resolve
chkconfig mysqld on
service mysqld start
mysqlstarted="no"
while [ "$mysqlstarted" = "no" ]
do
sleep 1
if [ "`mysql -u root -n -N -s -r -e \"select 1\"`" = "1" ] ;then
mysqlstarted="yes"
fi
done
mysql -u root -n -e "delete from mysql.user where user=‘‘ or host not in (‘localhost‘,‘127.0.0.1‘);delete from mysql.db;flush privileges;create database skyeye;grant select,insert,update,delete on skyeye.* to ‘skyeye‘@‘127.0.0.1‘ identified by password ‘*A6EC8FBFBBF786D565C09125BC6DE94B18C99E05‘;grant super,process,select on *.* to ‘skyeyemonitor‘@‘127.0.0.1‘ identified by password ‘*8A91882A516AD0D2459D2F1400140C0A95D67C01‘; grant select on skyeye.* to ‘skyeyebackup‘@‘localhost‘ identified by password ‘*001F19F6905729491F70765BB39900EA9284531E‘;grant process on *.* to ‘skyeyebackup‘@‘localhost‘;"
else
service mysqld start
fi
if [ ! -d /data/mysql/bin ] ;then
mkdir -p /data/mysql/bin
chown mysql:mysql /data/mysql/bin
fi
if [ ! -f /data/mysql/bin/skyeye_mysql_backup.sh ] ;then
cp skyeye_mysql_backup.sh /data/mysql/bin/skyeye_mysql_backup.sh
chown mysql:mysql /data/mysql/bin/skyeye_mysql_backup.sh
chmod 100 /data/mysql/bin/skyeye_mysql_backup.sh
echo "0 3 * * * root /data/mysql/bin/skyeye_mysql_backup.sh" >/etc/cron.d/skyeye_mysql
service crond start
chkconfig crond on
fi
if [ ! -d /data/mysql/backup ] ;then
mkdir -p /data/mysql/backup
chown mysql:mysql /data/mysql/backup
fi
将上图中的文件下载之后 和脚本放在同一个目录下 执行脚本即可安装
MySQL5.6 一键安装脚本