脚本:在CentOS上实现MariaDB双机系统(1 Master+1 Slave)
棒棒堂智能运维
2020年06月09日 14:57
收录于文集
共22篇

MariaDB-Master:

#!/bin/bash ######################################## File Description #​####################################### # Creation time:2020-05-31 # Project:05 # Task: 04-1 # Execute example:bash reader-shell-x-x.sh # Detailed description: # About:http://linux.book.51xueweb.cn ################################################################################################## # reback start yum remove -y  MariaDB-server yum -y clean all rm -rf /var/lib/mysql/ rm -f /etc/yum.repos.d/MariaDB.repo rm -f /etc/my.cnf # reback end #***************reader shell start*************** # echo &#​34;---------------Configuration MariaDB---------------" # Create YUM touch /etc/yum.repos.d/MariaDB.repo # Write MariaDB YUM source configuration information to a file echo [mariadb] > /etc/yum.repos.d/MariaDB.repo echo name = MariaDB >> /etc/yum.repos.d/MariaDB.repo echo baseurl = https://mirrors.aliyun.com/mariadb/yum/10.4/centos8-amd64 >> /etc/yum.repos.d/MariaDB.repo echo module_hotfixes=1 >> /etc/yum.repos.d/MariaDB.repo echo gpgkey= https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB >> /etc/yum.repos.d/MariaDB.repo echo gpgcheck=1 >> /etc/yum.repos.d/MariaDB.repo # Install MariaDB yum install -y MariaDB-server # Start MariaDB # echo &#​34;---------------Start MariaDB---------------" systemctl start mariadb # View MariaDB INFO # echo &#​34;---------------View MariaDB INFO---------------" # systemctl status mariadb systemctl status mariadb | head  -10 # Configure the mariadb service as A boot-on systemctl enable mariadb # Verify that the mariadb service is boot-on systemctl is-enabled mariadb systemctl stop firewalld setenforce 0 # Configure the first database server as the primary node echo [mariadb] >> /etc/my.cnf echo log-bin >> /etc/my.cnf echo server_id=1 >> /etc/my.cnf echo log-basename= db-cluster-mariadb >> /etc/my.cnf # Restart the mariadb service for the profile to take effect systemctl restart mariadb # Create and authorize accounts for synchronization mysql -e "CREATE USER &#​39;replication_user'@'%&#​39; IDENTIFIED BY 'centos@mariadb#123&#​39;;" mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'replication_user&#​39;@'%&#​39;" mysql -e "show master status;&#​34; echo "Please record file value and Position value,Press 【Enter】 to continue" read -n 1 echo "Please execute Script 2 on server-2 and press 【Enter】 to continue after execution" read -n 1 # Add data to the database server primary node mysql -e "create database fourthdb;&#​34; mysql -e "use fourthdb;create table test_table(id int(11),name varchar(20),sex enum('0&#​39;,'1&#​39;,'2&#​39;),primary key (id));" mysql -e "use fourthdb;insert into test_table ( id, name,sex ) VALUES ( 1, 'name1&#​39;,'0&#​39; );" mysql -e "show databases;&#​34; mysql -e "use fourthdb;show tables;&#​34; mysql -e "use fourthdb;select * from test_table;" #***************reader shell end***************

MariaDB-Slave:

#!/bin/bash ######################################## File Description #​####################################### # Creation time:2020-05-31 # Project:05 # Task: 04-2 # Execute example:bash reader-shell-x-x.sh # Detailed description: # About:http://linux.book.51xueweb.cn ################################################################################################## # reback start yum remove -y MariaDB-server   yum -y clean all rm -rf /var/lib/mysql/ rm -f /etc/yum.repos.d/MariaDB.repo rm -f /etc/my.cnf # reback end #***************reader shell start*************** # Create a YUM source file touch /etc/yum.repos.d/MariaDB.repo # Write MariaDB YUM source configuration information to a file echo [mariadb] > /etc/yum.repos.d/MariaDB.repo echo name = MariaDB >> /etc/yum.repos.d/MariaDB.repo echo baseurl = https://mirrors.aliyun.com/mariadb/yum/10.4/centos8-amd64 >> /etc/yum.repos.d/MariaDB.repo echo module_hotfixes=1 >> /etc/yum.repos.d/MariaDB.repo echo gpgkey= https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB >> /etc/yum.repos.d/MariaDB.repo echo gpgcheck=1 >> /etc/yum.repos.d/MariaDB.repo # Install MariaDB yum install MariaDB-server -y # Start mariadb systemctl start mariadb # Look MariaDB systemctl status mariadb | head  -10 # Configure the mariadb service as A boot-on systemctl enable mariadb # Verify that the mariadb service is boot-on systemctl is-enabled mariadb systemctl stop firewalld setenforce 0 # Configure the first database server as the primary node echo [mariadb] >> /etc/my.cnf echo log-bin >> /etc/my.cnf echo server_id=2 >> /etc/my.cnf # Restart mariadb systemctl restart mariadb read -p "Please enter the server-1 IP address:" server1_ip read -p "Please enter the File value:&#​34; log_file read -p "Please enter the Position value:" log_position # Set the option to connect the primary server from the server mysql -e "CHANGE MASTER TO MASTER_HOST='$server1_ip&#​39;, MASTER_USER='replication_user&#​39;, MASTER_PASSWORD='centos@mariadb#123&#​39;, MASTER_PORT=3306, MASTER_LOG_FILE='$log_file&#​39;, MASTER_LOG_POS=$log_position, MASTER_CONNECT_RETRY=10;" # Start replication mysql -e "start slave;&#​34; # View synchronization status by looking at the node mysql -e "show slave status \G&#​34; echo "Please execute Script 1 on server-1 and press Enter to continue when you are finished" read -n 1 # Verify that the server is synchronized from the database server mysql -e "show databases;&#​34; mysql -e "use fourthdb;show tables;&#​34; mysql -e "use fourthdb;select * from test_table;" #***************reader shell end***************