MySQL安装脚本0104-亲试ok

时间:2024-06-29 15:04:20
#!/bin/bash
export host_ip=192.168.7.79
export password=123456
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/my3306/data
mkdir -p /data/my3306/log/iblog
mkdir -p /data/my3306/log/binlog
mkdir -p /data/my3306/log/relaylog
mkdir -p /data/my3306/run
mkdir -p /data/my3306/tmp
chown -R mysql:mysql /data/my3306
chmod -R 755 /data/my3306
echo '5.上传解包软件包'
cd /softwares
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/my3306
cd /data/my3306/data
rm -rf *
cd /data/my3306/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/my3306/data   #必须到脚本的父目录执行哦
#./scripts/mysql_install_db  --defaults-file=/data/mysql/my.cnf  --user=mysql --datadir=/data/my3306/data
echo '#8.取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 '9.修改系统环境变量'
echo "PATH=/data/mysql/bin:\$PATH:/sbin" >> /etc/profile
echo '10.自启动脚本'                                                                                                                                                                                   
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/my3306/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 '11.删除默认匿名账户'
#mysql -h127.0.0.1 -uroot -P3306 -e "flush privileges;"
mysql  -uroot -P3306 <<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'@'%' 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 '123456' with grant option;
update mysql.user set authentication_string=password("123456") where user='root';
flush privileges;
EOF
service mysql restart
exit