mysql源码安装脚本分享

所属分类: 脚本专栏 / linux shell 阅读数: 1801
收藏 0 赞 0 分享

复制代码 代码如下:

#!/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脚本中计算字符串长度的5种方法

这篇文章主要介绍了Shell脚本中计算字符串长度的5种方法,来自于个人Shell脚本长期的开发经验,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现把进程负载均衡到多核CPU中

这篇文章主要介绍了Shell脚本实现把进程负载均衡到多核CPU中,可以把进程指定运行在某个CPU中,需要的朋友可以参考下
收藏 0 赞 0 分享

5个Shell脚本编程入门练习例子

这篇文章主要介绍了5个Shell脚本编程入门例子,涵盖了各种操作,又有一些游戏的性质,作为入门练习例子是不很不错的,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本编程中常用的数学运算实例

这篇文章主要介绍了Shell脚本编程中常用的数学运算实例,包含最基本的加减乘除,还有质数、偶数的判断等,需要的朋友可以参考下
收藏 0 赞 0 分享

5个实用的shell脚本面试题和答案

这篇文章主要介绍了5个实用的shell脚本面试题和答案,给出的脚本堪称编码规范,麻雀虽小,异常处理,友好提示,一应俱全,值得学习,需要的朋友可以参考下
收藏 0 赞 0 分享

使用bash shell删除目录中的特定文件的3种方法

这篇文章主要介绍了使用bash shell删除目录中的特定文件的3种方法,分别为扩展模式匹配符、GLOBIGNORE 变量和find 命令,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell获取文件的文件名和扩展名的例子

这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell多线程操作及线程数控制实例

这篇文章主要介绍了Shell多线程操作及线程数控制实例,文中从单线程实现一个需求开始,不断加入代码实现多线程以及线程数的控制功能,需要的朋友可以参考下
收藏 0 赞 0 分享

阿里云主机一键安装lamp、lnmp环境的shell脚本分享

这篇文章主要介绍了阿里云主机一键安装lamp、lnmp环境的shell脚本分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本转发80端口数据包给Node.js服务器

开发基于Node.js的WEB应用很方便,但是服务端口问题很蛋疼,由于Linux内核规定普通用户只能使用大于1024的端口号,所以使用普通用户运行Node.js服务就不能监听80端口
收藏 0 赞 0 分享
查看更多