mysql源码安装脚本分享
#!/bin/bash
path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export path
clear;
sysname=""
sysbit=""
cpunum=""
ramtotal=""
ramswap=""
filemax=""
mysqlversion="percona-server-5.6.15-rel63.0"
mysqlline="http://www.percona.com/downloads/percona-server-5.6/latest/source"
mysqlpath="/usr/local/mysql"
mysqldatapath="$mysqlpath/data"
mysqllogpath="/var/log/mysql"
mysqlconfigpath="$mysqlpath/conf"
mysqlpass="test123"
system_check(){
[[ $(id -u) != '0' ]] && echo '[error] please use root to install puppet.' && exit;
egrep -i "centos" /etc/issue && sysname='centos';
egrep -i "ubuntu" /etc/issue && sysname='ubuntu';
[[ "$sysname" == '' ]] && echo '[error] your system is not supported this script' && exit;
sysbit='32' && [ `getconf word_bit` == '32' ] && [ `getconf long_bit` == '64' ] && sysbit='64';
cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
ramtotal=`free -m | grep 'mem' | awk '{print $2}'`;
ramswap=`free -m | grep 'swap' | awk '{print $2}'`;
filemax=`cat /proc/sys/fs/file-max`
}
install_base_packages()
{
system_check
if [ "$sysname" == 'centos' ]; then
echo '[yum-fastestmirror installing] ************************************************** >>';
yum -y install yum-fastestmirror;
cp /etc/yum.conf /etc/yum.conf.lnmp
sed -i 's:exclude=.*:exclude=:g' /etc/yum.conf
for packages in gcc gcc-c++ openssl-devel ncurses-devel wget crontabs iptables bison cmake automake make readline-devel logrotate openssl; do
echo "[${packages} installing] ************************************************** >>";
yum -y install $packages;
done;
mv -f /etc/yum.conf.lnmp /etc/yum.conf;
else
apt-get remove -y mysql-client mysql-server mysql-common;
apt-get update;
for packages in gcc g++ cmake make ntp logrotate cron bison libncurses5-dev libncurses5 libssl-dev openssl curl openssl; do
echo "[${packages} installing] ************************************************** >>";
apt-get install -y $packages --force-yes;apt-get -fy install;apt-get -y autoremove;
done;
fi;
}
install_mysql(){
install_base_packages
cd /tmp/
echo "[${mysqlversion} installing] ************************************************** >>";
[ ! -f ${mysqlversion}.tar.gz ] && wget -c ${mysqlline}/${mysqlversion}.tar.gz
tar -zxf /tmp/$mysqlversion.tar.gz;
cd /tmp/$mysqlversion;
groupadd mysql;
useradd -s /sbin/nologin -g mysql mysql;
cmake -dcmake_install_prefix=$mysqlpath -ddefault_charset=utf8 -ddefault_collation=utf8_general_ci -dwith_extra_charsets=complex -dwith_readline=on -denabled_local_infile=on -dwith_innodb_memcached=on -dwith_unit_tests=off;
make -j $cpunum;
make install;
for path in $mysqllogpath $mysqlpath $mysqlconfigpath/conf.d $mysqldatapath;do
[ ! -d $path ] && mkdir -p $path
chmod 740 $path;
chown -r mysql:mysql $path;
done
# eof **********************************
cat > $mysqlconfigpath/my.cnf<<eof;
[mysqld]
user = mysql
server-id = 1
pid-file = /var/run/mysqld.pid
socket = /var/run/mysqld.sock
port = 3306
basedir = $mysqlpath
datadir = $mysqldatapath
bind-address = 0.0.0.0
skip-name-resolve
skip-external-locking
thread_concurrency = `expr $cpunum \* 2`
max_connections = `expr $filemax \* $cpunum \* 2 / $ramtotal`
max_connect_errors = 30
table_open_cache = `expr $ramtotal + $ramswap`
max_allowed_packet = `expr $ramtotal \* 2 / 1000`m
binlog_cache_size = 4m
max_heap_table_size = `expr $ramtotal / 100`m
sort_buffer_size = `expr $ramtotal \* 2 / 1000`m
join_buffer_size = `expr $ramtotal \* 2 / 1000`m
query_cache_size = `expr $ramtotal / 100`m
thread_cache_size = 30
thread_concurrency = `expr $cpunum \* 4`
connect_timeout = 1200
wait_timeout = 1200
general_log = 1
general_log_file = $mysqllogpath/mysql.log
log_error = $mysqllogpath/mysql-err.log
slow_query_log = 1
slow_query_log_file = $mysqllogpath/mysql-slow.log
long_query_time = 3
log_bin = $mysqllogpath/mysql-bin
log_bin_index = $mysqllogpath/mysql-bin.index
expire_logs_days = 7
max_binlog_size = `expr $(df -m $mysqllogpath |awk 'nr==2{printf "%s\n",$4}') / 10000`m
default_storage_engine = innodb
innodb_buffer_pool_size = `expr $ramtotal / 100`m
innodb_log_buffer_size = 8m
innodb_file_per_table = 1
innodb_open_files = `expr $filemax \* $cpunum / $ramtotal`
innodb_io_capacity = `expr $filemax \* $cpunum / $ramtotal`
innodb_flush_method = o_direct
!includedir $$mysqlconfigpath/conf.d
[mysqld_safe]
open_files_limit = `expr $filemax / $cpunum / 100`
[isamchk]
key_buffer = 16m
[mysqldump]
quick
quote-names
max_allowed_packet = 16m
eof
# **************************************
$mysqlpath/scripts/mysql_install_db --user=mysql --defaults-file=$mysqlconfigpath/my.cnf --basedir=$mysqlpath --datadir=$mysqldatapath;
# eof **********************************
cat > /etc/ld.so.conf.d/mysql.conf<<eof
/usr/local/mysql/lib/mysql
/usr/local/lib
eof
# **************************************
ldconfig;
if [ "$sysbit" == '64' ] ; then
ln -s $mysqlpath/lib/mysql /usr/lib64/mysql;
else
ln -s $mysqlpath/lib/mysql /usr/lib/mysql;
fi;
cp $mysqlpath/support-files/mysql.server /etc/init.d/mysqld;
chmod 775 /etc/init.d/mysqld;
/etc/init.d/mysqld start;
ln -s $mysqlpath/bin/mysql /usr/bin/mysql;
ln -s $mysqlpath/bin/mysqladmin /usr/bin/mysqladmin;
$mysqlpath/bin/mysqladmin password $mysqlpass;
rm -rf $mysqldatapath/test;
# eof **********************************
mysql -hlocalhost -uroot -p$mysqlpass <<eof
use mysql;
delete from user where user='';
update user set password=password('$mysqlpass') where user='root';
delete from user where not (user='root');
drop user ''@'%';
flush privileges;
eof
# **************************************
echo "[ok] ${mysqlversion} install completed.";
}
install_mysql
上一篇: shell学习教程获取命令行参数示例
下一篇: shell实现字符编码转换工具分享
推荐阅读
-
Shell实现多级菜单系统安装维护脚本实例分享
-
MYSQL自动安装脚本的方法
-
CentOS 6.2编译安装Nginx1.0.14+MySQL5.5.22+PHP5.3.10步骤分享
-
CentOS下mysql定时备份Shell脚本分享
-
一个简洁的全自动安装LNMP服务器环境的Shell脚本分享
-
Centos7安装 mysql5.6.29 shell脚本
-
Ubuntu、Linux Mint一键安装Chrome浏览器的Shell脚本分享
-
centos7源码安装mysql5.7.17详细教程
-
Linux CentOS6.6系统中安装mysql源码包的方法
-
Linux下源码编译安装配置SVN服务器的步骤分享