欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

mysql源码安装脚本分享

程序员文章站 2023-11-25 12:22:46
复制代码 代码如下:#!/bin/bashpath=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin...

复制代码 代码如下:

#!/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