1+X云计算运维与开发初级 部署主从数据库 (脚本) 思维导图

1+X云计算运维与开发初级 部署主从数据库 (脚本) 思维导图

// An highlighted block
#!/bin/bash
##脚本运行环境,能上网,做服务端
##基础环境配置,修改主机名,关闭防火墙和SELinuX
hostnamectl set-hostname mysql1
systemctl disable --now firewalld && setenforce 0
yum repolist


##两台主机的IP地址
mysql1=192.168.100.30
mysql2=192.168.100.40
##数据库密码
DB_PASS=000000


#ssh免密登录shell脚本
#配置免密登录的所有机子都要运行该脚本
 
#修改/etc/ssh/sshd_config配置文件
#sed -i 's/被替换的内容/替换成的内容/'  /配置文件地址
#sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
#cat >> /etc/ssh/sshd_config <<EOF
#RSAAuthentication yes
#EOF
 
yum install expect  -y #安装expect
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

SERVERS=$mysql2   #需要配置的主机名
PASSWORD=$DB_PASS   #需要配置的主机登录密码
 
#将本机生成的公钥复制到其他机子上
#如果(yes/no)则自动选择yes继续下一步
#如果password:怎自动将PASSWORD写在后面继续下一步
auto_ssh_copy_id(){
        expect -c "set timeout -1;
        spawn ssh-copy-id $1;                                
        expect {
                *(yes/no)* {send -- yes\r;exp_continue;}
                *password:* {send -- $2\r;exp_continue;}  
                eof        {exit 0;}
        }";
}
 
ssh_copy_id_to_all(){
        for SERVER in $SERVERS #遍历要发送到各个主机的ip
        do
                auto_ssh_copy_id $SERVER $PASSWORD
        done
}
ssh_copy_id_to_all


cat >> /etc/hosts <<EOF
$mysql1	mysql1
$mysql2	mysql2
EOF

##安装配置数据库相关服务,初始化数据库
yum install -y mariadb mariadb-server
systemctl enable --now  mariadb.service


expect -c "
spawn /usr/bin/mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"\r\"
expect \"Set root password?\"
send \"y\r\"
expect \"New password:\"
send \"$DB_PASS\r\"
expect \"Re-enter new password:\"
send \"$DB_PASS\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"n\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
"

: > /etc/my.cnf 
cat >> /etc/my.cnf <<EOF
[mysqld]
log_bin = mysql-bin	#记录操作日志
binlog_ignore_db = mysql	#不同步 mysql 系统数据库
server_id = 30
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log 
pid-file=/var/run/mariadb/mariadb.pid
EOF

systemctl restart mariadb
mysql -uroot -p$DB_PASS -e "grant all privileges on *.* to 'root'@'%' identified by '$DB_PASS' ;"
mysql -uroot -p$DB_PASS -e "grant  replication slave   on *.* to 'user'@'mysql2' identified by '$DB_PASS' ;"

ssh $mysql2  << eeooff
hostnamectl set-hostname mysql2
systemctl disable --now firewalld && setenforce 0
yum repolist

##两台主机的IP地址
mysql1=192.168.100.30
mysql2=192.168.100.40
##数据库密码
DB_PASS=000000

cat >> /etc/hosts <<EOF
$mysql1 mysql1
$mysql2 mysql2
EOF

##安装配置数据库相关服务,初始化数据库
yum install -y mariadb mariadb-server
systemctl enable --now  mariadb.service

yum install expect  -y 

expect -c "
spawn /usr/bin/mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"\r\"
expect \"Set root password?\"
send \"y\r\"
expect \"New password:\"
send \"$DB_PASS\r\"
expect \"Re-enter new password:\"
send \"$DB_PASS\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"n\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
"

: > /etc/my.cnf
cat >> /etc/my.cnf <<EOF
[mysqld]
log_bin = mysql-bin     #记录操作日志
binlog_ignore_db = mysql        #不同步 mysql 系统数据库
server_id = 40
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
EOF

systemctl restart mariadb
mysql -uroot -p$DB_PASS -e "change master to master_host='mysql1',master_user='user',master_password='$DB_PASS';"

mysql -uroot -p$DB_PASS -e "start slave;"
mysql -uroot -p$DB_PASS -e "show slave status\G"

exit
eeooff
echo done!
发布了18 篇原创文章 · 获赞 9 · 访问量 2488

猜你喜欢

转载自blog.csdn.net/weixin_43663238/article/details/105461064