centos7单机安装mysql多实例

post by rocdk890 / 2017-3-16 15:52 Thursday linux技术
在上一篇centos7安装mysql 5.7中说了要给大家个一键安装mysql多实例的脚本,在这篇文章里给大家补上,这里说下脚本有几个问题:
1.没有检测是否有安装mysql,如果你自己已经安装了mysql,请自行修改脚本.
2.本脚本有可能修改mysql的root密码会失败,如果失败了的话,请直接进mysql里修改root密码.(默认mysql的root是空)

目前这个脚本不是最满意的状态,先发上来,以后再改了.
脚本内容:
cat /root/soft_shell/auto_install_multi_mysql5.7.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

# Check the network status
NET_NUM=`ping -c 4 www.baidu.com |awk '/packet loss/{print $6}' |sed -e 's/%//'`
if [ -z "$NET_NUM" ] || [ $NET_NUM -ne 0 ];then
        echo "Please check your internet"
        exit 1
fi

if [ -s mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz ];then
	echo -e "\033[40;31m mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz [found]\033[40;37m"
else
	wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
fi

if [ -s /etc/init.d/mysqld_multi ];then
	rm -f /etc/init.d/mysqld_multi
fi

user_mysql=`cat /etc/passwd|grep mysql|awk -F : '{print $1}'`
if [ -s /etc/my.cnf ]; then
	rm -f /etc/my.cnf
fi

if [ -z "$user_mysql" ];then
	groupadd mysql
	useradd -s /sbin/nologin -M -g mysql mysql
else
	echo "user mysql already exists!"
fi

rpm=`rpm -qa|grep libaio`
if [ -z $rpm ];then
	yum -y install libaio
else
 	echo -e "\033[40;31m libaio [found]\033[40;37m"
fi

mysql_port="3305"
read -p "Enter mysql password": newpasswd
wget http://download.slogra.com/mysql/mysqld_multi -P /etc/init.d/
sed -i "s/rocdk890/$newpasswd/g" /etc/init.d/mysqld_multi
chmod +x /etc/init.d/mysqld_multi
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
echo "Install mysql...."

tar zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
mkdir -p /var/log/mysql
touch /var/log/mysql/mysql_multi.log
chown -R mysql:mysql /var/log/mysql

cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
log=/var/log/mysql/mysql_multi.log

#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF

read -p "install some MySQL case": number
for i in $( seq 1 $number );do
mkdir -p /data/mysql${i}
chown -R mysql.mysql /data/mysql${i}
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql${i}

cat >> /etc/my.cnf <<EOF
[mysqld${i}]
basedir=/usr/local/mysql
datadir=/data/mysql${i}
port=$(($mysql_port+$i))
server_id=${i}
socket=/data/mysql${i}/mysql.sock
symbolic-links=0
character_set_server=utf8
pid-file=/data/mysql${i}/mysql.pid

EOF

#/etc/init.d/mysqld_multi start
/usr/local/mysql/bin/mysqld_multi --defaults-extra-file=/etc/my.cnf start ${i}

/usr/local/mysql/bin/mysql -u root -S /data/mysql${i}/mysql.sock -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$newpasswd';"

done

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile 
#echo 'export PATH=$PATH:/usr/local/bin' >> /etc/profile
source /etc/profile
#/etc/init.d/mysqld_multi start
#/etc/init.d/mysqld_multi report

ps:
当脚本完成后,请执行/usr/local/mysql/bin/mysql -u root -S /data/mysql1/mysql.sock来验证是否有root密码,如果直接进去了,请执行:
/usr/local/mysql/bin/mysql -u root -S /data/mysql(个数)/mysql.sock -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${newpasswd}'";
这样来创建root密码,好了,就这样吧.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-690.html

标签: centos mysql 安装 shell install 一键 单机 多实例 multi

评论: