[原创]linux(centos 6.x 64位)环境下安装mysql 5.6以及mysql 5.7.x的简易shell脚本 – 21运维
通知: .-...

[原创]linux(centos 6.x 64位)环境下安装mysql 5.6以及mysql 5.7.x的简易shell脚本

mysql 21运维 3027浏览 0评论

同一件事情做多了,就想写个脚本,不要问我为什么,明显是因为我懒! 以后再去安装这个mysql,只需要执行以下脚本,其他版本安装至需要替换下载地址和解压路径就可以了。注意该脚本采用cmake安装,适用于mysql 5.5以及以上,低版本请采用make安装。
如下脚本通过centos 6.X_64测试安装没问题,有问题的朋友请留言反馈以便调整。
手工安装请参考[原创]linux(centos)环境下如何编译安装mysql 5.6

脚本执行效果:
1
具体简单脚本:

#!/bin/bash
#Install  mysql 5.x  for centos  6.x
#author:21yunwei
downloaddir="/usr/local/src"
cpuinfo=`cat  /proc/cpuinfo | grep -c  processor`
function  checkroot(){
if [ $UID -ne  0 ]
  then
	echo "Please login as  root"
	exit;
fi
}

function create_user_mysql(){
gflag=`cat  /etc/group  |awk -F':'  '{print  $1}'  | grep  mysql`
[[ $gflag != "" ]]  && echo "group 'mysql' already exists"  || groupadd mysql
uflag=`cat  /etc/passwd  |awk -F':'  '{print  $1}'  | grep  mysql`
[[ $uflag != ""  ]] && echo "user 'mysql' already exists" ||useradd -r mysql -g mysql -s /sbin/nologin
}
function  Msgbox(){
if [  $? -ne 0 ] ;then
	echo "Error,please check"
	exit;
fi
}
function  install_required_libs(){
echo "Install  required  libs,please  wait:"
yum install gcc-c++  ncurses-devel  tcp_wrappers-devel bison-devel bison build-essential libncurses5-dev cmake openssl  openssl-devel  -y  >/dev/null
}
function  check_datadir(){
[ ! -d /data/mysql ] && mkdir  -p /data/mysql
datadir="/data/mysql"
chown  -R mysql.mysql   $datadir
}
function menu(){
	echo "###############################################################"
	cat < <EOF
	Please choose  mysql  version  which  you want to install:
	1,Mysql 5.6.x
	2,Mysql 5.7.x
EOF
	echo "###############################################################"
}
function  mysql_version(){
read  -p  "please choose php  version that you  want to install:" flag
read -p   "please  mysql root password that you want to set:" mysql_password
}

function  mysql_download(){

case $flag  in

1)
	version="mysql-5.6.36"
	 echo "${version} will be installed"  &&  cp ./package/mysql-5.6.36.tar.gz  ${downloaddir}/${version}.tar.gz 
;;
2)
	version="mysql-5.7.17"
	echo "${version} will be installed"  &&  cp  ./package/mysql-boost-5.7.17.tar.gz ${downloaddir}/${version}.tar.gz
;;
*)
echo "Please  input number 1 or  2,other novalid"
mysql_version
mysql_download
esac
}
function mysql_install(){
case  $version  in
"mysql-5.6.36")
tar zxf  ${downloaddir}/${version}.tar.gz -C ${downloaddir} && cd ${downloaddir}/${version} && echo "Preconfig mysql,please wait:"
/usr/bin/cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql    \
-DMYSQL_DATADIR=${datadir}    \
-DSYSCONFDIR=/etc   \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci  > /dev/null	
echo  "Compiling mysql now ,please wait(10~20 minutes):"
make -j $cpuinfo  >/dev/null 2>&1
echo  "Install mysql now ,please wait:"
make install  >/dev/null 2>&1
[ $? -eq 0 ] && echo  "Mysql install ok ,next step Init mysql." || echo  "Mysql install error ,please check"
cat > /etc/my.cnf  < <EOF
[client]
#password   = your_password
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
datadir = ${datadir}
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500

explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = ${datadir}
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${datadir}
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF
echo "Init mysql now,please wait:"
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=${datadir} --user=mysql >/dev/null 2>&1
\cp support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld && chkconfig mysqld  on
cat > /etc/ld.so.conf.d/mysql.conf< <EOF
    /usr/local/mysql/lib
    /usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck
;;

"mysql-5.7.17")
 tar zxf  ${downloaddir}/${version}.tar.gz -C ${downloaddir}  && cd ${downloaddir}/${version} && echo "Preconfig mysql,please wait:"
 
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql  \
-DSYSCONFDIR=/etc  \
-DWITH_MYISAM_STORAGE_ENGINE=1  \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1  \
-DWITH_FEDERATED_STORAGE_ENGINE=1  \
-DEXTRA_CHARSETS=all  \
-DDEFAULT_CHARSET=utf8mb4  \
-DDEFAULT_COLLATION=utf8mb4_general_ci  \
-DENABLE_DOWNLOADS=1 \
-DWITH_EMBEDDED_SERVER=1  \
-DENABLED_LOCAL_INFILE=1  \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=./boost    >/dev/null  2>&1
echo  "Compiling mysql now ,please wait:"
make -j $cpuinfo >/dev/null  2>&1
Msgbox
echo  "Install mysql now ,please wait:"
make install >/dev/null  2>&1
Msgbox
cat > /etc/my.cnf  < <EOF
[client]
#password   = your_password
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
datadir = ${datadir}
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500

explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10
early-plugin-load = ""


default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = ${datadir}
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${datadir}
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

EOF
/usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=${datadir} --user=mysql  >/dev/null  2>&1
\cp support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld && chkconfig mysqld  on
cat > /etc/ld.so.conf.d/mysql.conf< <EOF
    /usr/local/mysql/lib
    /usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck
;;

*) 
	echo "Mysql version error  ,please  check" && exit
;;
esac
}

function  mysql_check_status(){
#start mysql
/etc/init.d/mysqld  start
#set mysql  password for user root
/usr/local/mysql/bin/mysqladmin -uroot password ${mysql_password} >/dev/null  2>&1

netstat   -tunlp | grep  mysqld
if [ $? -eq 0 ];then
	mysql_version=`mysql  -V`
	echo  -e  "Mysql start  sucess,and  mysql version  is :\n" ${mysql_version}
	echo "Mysql root password is: $mysql_password"
else
	echo   "Mysql start  failed ,please check"
	exit;
fi
}

function  main(){
checkroot
create_user_mysql
install_required_libs
check_datadir
menu
mysql_version
mysql_download
mysql_install
mysql_check_status
}
main

转载请注明:21运维 » [原创]linux(centos 6.x 64位)环境下安装mysql 5.6以及mysql 5.7.x的简易shell脚本

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 这个世界就是你们这些懒人改变的! :roll:
    蛋蛋的忧伤2016-01-31 14:07 回复