MySQL安装脚本最佳实践
2018年9月19日
17:01
#!/bin/bash
export host_ip=192.168.7.206
echo '#1.取master主机ip的后三位作为master的server_id'
export host_server_id=`echo $host_ip |awk -F . '{print $4}'`
echo '1.屏蔽当前的MySQL环境'
service mysql stop
rm -f /etc/my.cnf
echo '2.检查并卸载系统中低版本的mysql'
#rpm -e mariadb-libs --nodeps
echo '3.添加用户和组'
rm -rf /home/mysql/
userdel mysql
#groupdel mysql
groupadd mysql
useradd -g mysql mysql
echo '4.创建目录及授权'
rm -rf /data
mkdir -p /data/mysql
mkdir -p /data/my3309/data
mkdir -p /data/my3309/log/iblog
mkdir -p /data/my3309/log/binlog
mkdir -p /data/my3309/log/relaylog
mkdir -p /data/my3309/run
mkdir -p /data/my3309/tmp
chown -R mysql:mysql /data/my3309
chmod -R 755 /data/my3309
echo '5.上传解压软件包'
cd /software
tar zxf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
cd mysql-5.7.23-linux-glibc2.12-x86_64
cp -R * /data/mysql/
chown -R mysql:mysql /data/
echo '6.修改配置文件my.cnf参数'
#手动上传写好的my.cnf配置文件
cp /scripts/my.cnf /etc/
##cp /data/mysql/support-files/my-default.cnf /data/mysql/my.cnf
#chown -R mysql:mysql /data/my3309
#cd /data/my3309/data
#rm -rf *
#cd /data/my3309/log
#rm -f binlog/*
#rm -f iblog/*
#rm -rf relaylog/
#rm -rf slow.log
echo '7.执行MySQL安装脚本'
cd /data/mysql/
bin/mysqld --initialize-insecure --user=mysql --datadir=/data/my3309/data/ --basedir=/data/my3309 #必须到脚本的父目录执行哦
#./scripts/mysql_install_db --defaults-file=/data/mysql/my.cnf --user=mysql --datadir=/data/my3309/data
echo '#1.取master主机ip的后三位作为master的server_id'
export host_server_id=`echo $host_ip |awk -F . '{print $4}'`
#echo $host_server_id
eval sed -i 's/server_id=128/server_id=${host_server_id}/g' /etc/my.cnf
cat /etc/my.cnf |grep server_id
echo '8.修改系统环境变量'
echo "PATH=/data/mysql/bin:$PATH:/sbin" >> /etc/profile
source /etc/profile
echo '9.自启动脚本'
#cp /data/mysql/my.cnf /etc/my.cnf
cp /data/mysql/support-files/mysql.server /etc/init.d/mysql
sed -i 's#^basedir=#basedir=/data/mysql#g' /etc/init.d/mysql
sed -i 's#^datadir=#datadir=/data/mysql/data#g' /etc/init.d/mysql
sed -i 's#^mysqld_pid_file_path=#mysqld_pid_file_path=/data/my3309/run/mysqld.pid#g' /etc/init.d/mysql
chkconfig --add mysql
/etc/init.d/mysql start
/etc/init.d/mysql stop
service mysql start
echo '10.删除默认匿名账户'
#mysql -h127.0.0.1 -uroot -P3309 -e "flush privileges;"
mysql -uroot -P3309 <<EOF
flush privileges;
update mysql.user set authentication_string='password(123456)' where user='root';
delete from mysql.user where user='';
set old_passwords=0;
create user 'replusr'@'%' identified by '123456';
create user 'root'@'192.168.7.206' identified by '123456';
create user 'root'@'192.168.7.207' identified by '123456';
grant replication slave on *.* to 'replusr'@'%';
grant super on *.* to 'replusr'@'%';
grant all privileges on *.* to 'root'@'%' identified by '&password' with grant option;
update mysql.user set authentication_string='password(123456)' where user='root';
flush privileges;
EOF
service mysql restart
exit