首页 > 代码库 > 自动安装MySQL5.6

自动安装MySQL5.6

MySQL5.6脚本自动安装 

---------

#! /bin/bash

source /etc/profile

# install MySQL5.6 

#MySQL安装包

Mysql="mysql-5.6.35-linux-glibc2.5-x86_64"

#MySQL下载地址

Mysql_Url="http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz"

#MySQL安装目录

Mysql_Basedir="/usr/local"

#MySQL数据库目录

Mysql_data="http://www.mamicode.com/data/mysql"

Mysql_Datedir="$Mysql_data/data"

#MySQL用户

User=mysql

mkdir $Mysql_data/{tmp,binlog,log} -p

#网卡路径

Eth0=‘/etc/sysconfig/network-scripts/ifcfg-eth0‘ 

Em1=‘/etc/sysconfig/network-scripts/ifcfg-em1‘ 

if [ -f $Eth0 ];then 

IP_ADD="$Eth0"

else

if [ -f $Em1 ];then

IP_ADD=$Em1

else

echo "error The script cannot recognize the configuration file of the network card"

fi

fi

ip="`grep IPADDR $IP_ADD | awk -F ‘[ =]‘ ‘{print $2}‘`"

#获取网卡的IP的最后一位

ip2="`grep IPADDR $IP_ADD | awk -F ‘[ =.]‘ ‘{print $5}‘`"


#如果检测到已经安装了MySQL,提示输入y,卸载旧版,再开始安装MySQL5.6

rpm -qa mysql-server | grep mysql-server > /tmp/rpm.log

if [ $? -eq 0 ];then

echo "You have installed `cat /tmp/rpm.log`"

echo "Enter y/yes to continue, other any key exits (y/yes)"

read input1

if [[ $input1 = y ]] ;then

yum -y remov `cat /tmp/rpm.log`

echo "start install MySQL5.6"

else

if [[ $input1 = yes ]] ;then

                yum -y remov `cat /tmp/rpm.log`

echo "start install MySQL5.6"

else

echo "The script has exit 1"

                exit 2

fi

echo "The script has exit 2"

exit 3

fi

else

echo "start install MySQL5.6"

fi



#检查mysql用户是否存在,不存在则创建

egrep "^$User" /etc/passwd >& /dev/null  

if [ $? -ne 0 ];then  

    useradd -s /sbin/nologin $User  &> /dev/null

fi 

 

#安装libaio依赖包

echo "See Dependency installation package"

yum -y install  libaio* &> /dev/null

rpm -qa libaio*


# 下载MySQL5.6二进制包解压。

# wget $Mysql_Url 


if [ -f $Mysql.tar.gz ];then

echo  "Unpack... $Mysql.tar.gz"

tar -xf $Mysql.tar.gz

else 

echo "$Mysql.tar.gz Package does not exist" 

fi


#移动mysql-5.6.35-linux-glibc2.5-x86_64 到$Mysql_Basedir/目录下改名为mysql

if [ -d $Mysql ];then

echo "move $Mysql mysql"

mv  $Mysql $Mysql_Basedir/mysql 

else 

echo "Unpack $Mysql.tar.gz ON" 

fi

#移动datadir 到$Mysql_Datedir

if [ -d $Mysql_Basedir/mysql/data ];then

echo "move $Mysql_Basedir/mysql/data  $Mysql_Datedir"

mv $Mysql_Basedir/mysql/data  $Mysql_Datedir

else 

echo "$Mysql_Basedir/mysql/data Non-existent" 

fi

#检查$Mysql_Datedir存在,给MySQL授权。

if [ -d $Mysql_Datedir/data ];then

chown -R mysql.mysql $Mysql_Datedir/data 

chown -R mysql.mysql $Mysql_Basedir/mysql

else 

echo "$Mysql_Datedir/data Non-existent"

fi



#初始化MySQL

$Mysql_Basedir/mysql/scripts/mysql_install_db --datadir=$Mysql_Datedir --basedir=$Mysql_Basedir/mysql --user=$User --explicit_defaults_for_timestamp

if [ $? -eq 0 ];then  

echo "Initialization MySQL OK" 

else

echo "Initialization MySQL NO"

exit 4

fi


#拷贝启动脚本....

if [ ! -f /etc/init.d/mysqld ];then

cp $Mysql_Basedir/mysql/support-files/mysql.server /etc/init.d/mysqld

else 

rm -rf /etc/init.d/mysqld && cp $Mysql_Basedir/mysql/support-files/mysql.server /etc/init.d/mysqld

fi



echo "$Mysql_Basedir/mysql/lib" > /etc/ld.so.conf.d/mysql.conf  

if [ $? -eq 0 ];then 

echo "Mysql lib OK" 

fi


ln -s $Mysql_Basedir/mysql/include /usr/include/mysql

ln -s $Mysql_Basedir/mysql/bin/* /usr/bin/ 


#Config my.cnf

mv /etc/my.cnf  /etc/my.cnf.ori

echo " " >/etc/my.cnf


echo "[mysqld]

user = $User

port = 3306

server-id = $ip2

basedir = $Mysql_Basedir/mysql

datadir = $Mysql_Datedir

" >/etc/my.cnf



$Mysql_Basedir/mysql/bin/mysql -e "drop database test;drop user root@‘::1‘;drop user ‘‘@‘localhost‘;drop user ‘‘@‘localhost.localdomain‘;drop user ‘root‘@‘localhost.localdomain‘; select user,host from mysql.user;flush privileges;"

sleep 1

/usr/local/mysql/bin/mysqladmin -uroot password ‘Zykj@passwd2017‘


chkconfig mysqld on


service mysqld start


netstat -anput | grep mysqld 


Pid=`netstat  -anput | grep mysqld |  awk -F ‘[ ;]+‘ ‘{print $7}‘ |awk -F ‘[ /]+‘ ‘{print $1}‘`

Port=`netstat  -anput | grep mysqld |  awk -F ‘[ ;]+‘ ‘{print $4}‘ | awk -F ‘[:;]+‘ ‘{print $2}‘`


echo "PID  $Pid"  

echo "PROT $Port"

---------

可以直接复制使用 

# wget $Mysql_Url  如果本地没有MySQL的安装文件,可以取消前面的注释,会自动下载MySQL二进制安装包。


自动安装MySQL5.6