1. 程式人生 > 實用技巧 >linux配置Mariadb雙主互備

linux配置Mariadb雙主互備

查詢安裝包:rpm -qa | grep mysql

有mysql包顯示:

停止服務:systemctl stop mysqld

解除安裝:

rpm -e mysql-server
rpm -e --nodeps mysql-libs

檢視防火牆狀態:systemctl status firewalld

停止防火牆:systemctl stop firewalld

設定開機不啟用防火牆:systemctldisable firewalld

主從節點安裝

yum -y install mariadb mariadb-server
cp /usr/share/mysql/my-huge.cnf  /etc/my.cnf
systemctl start mariadb
systemctl enable mariadb

Mariadb資料庫, 執行指令碼:/usr/bin/mysql_secure_installation

NOTE:RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTIONUSE!  PLEASE READ EACH STEP CAREFULLY!
In order to log intoMariaDB to secure it, we'll need the current
password for the rootuser.  If you've just installed MariaDB,and
you haven't set the rootpassword yet, the password will be blank, so you should just pressenter here. Enter current passwordfor root (enter for none): 安裝後預設沒有root密碼,直接回車 OK, successfully usedpassword, moving on... Setting the rootpassword ensures that nobody can log into the MariaDB root user without theproper authorisation. Set root password
? [Y/n]Y New password: 輸入root的新密碼 Re-enter new password: 新密碼確認 Password updatedsuccessfully! Reloading privilegetables.. ... Success! By default, a MariaDBinstallation has an anonymous user, allowing anyone to log into MariaDBwithout having to have a user account created for them. This is intended only for testing, and tomake the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users?[Y/n] 刪除匿名使用者 Y ... Success! Normally, root shouldonly be allowed to connect from 'localhost'. This ensures that someonecannot guess at the root password from the network. Disallow root loginremotely? [Y/n] 關閉root遠端登入 Y ... Success! By default, MariaDBcomes with a database named 'test' that anyone can access. This is also intended only for testing, andshould be removed before moving into aproduction environment. Remove test database andaccess to it? [Y/n] 刪除test資料庫 Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilegetables will ensure that all changes made so far will take effectimmediately. Reload privilege tablesnow? [Y/n] 確定以上所有操作 Y ... Success! Cleaning up... All done! If you've completed all of the above steps,your MariaDB installation should nowbe secure. Thanks for usingMariaDB!

從節點修改vim /etc/my.cnf配置檔案:

server-id= 2

進行重啟從節點(slave):systemctl restart mariadb

在master節點上執行:

登入MariaDB資料庫:mysql -uroot –proot

GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'root'@'192.168.45.%' identified by 'root';
flush privileges;

查詢SQL(Master的狀態)命令:SHOW MASTER STATUS;

配置從節點SLAVE:(注意在從節點上執行)

登入從伺服器:mysql -uroot –proot進行配置:

CHANGE MASTER TO MASTER_HOST='192.168.45.211', MASTER_USER='root', MASTER_PASSWORD='root',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=600;

start slave;

reset master;

show slave status\G;

確保 Slave_IO_Running和 Slave_SQL_Running 的值為Yes

在slave節點上執行:

登入MariaDB資料庫:mysql -uroot –proot

GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'root'@'192.168.45.%' identified by 'root';
flush privileges;

查詢SQL(Master的狀態)命令:SHOW MASTER STATUS;

配置主節點Master:(注意在主節點上執行)

登入從伺服器:mysql -uroot –proot進行配置:

CHANGE MASTER TO MASTER_HOST='192.168.45.211', MASTER_USER='root', MASTER_PASSWORD='root',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=1655;

start slave;

show slave status\G;

確保 Slave_IO_Running和 Slave_SQL_Running 的值為Yes

主從節點的show slave status\G;結果Slave_IO_Running和 Slave_SQL_Running 的值為Yes及成功。