MySQL多主從集群是一種常見的數據庫架構,能夠提高數據庫的可用性和擴展性。以下是MySQL多主從集群的搭建流程:
步驟一:安裝MySQL
$ apt-get update $ apt-get install mysql-server
步驟二:創建并配置主節點
$ sudo systemctl stop mysql $ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
在mysqld.cnf中添加以下配置:
server-id = 1 log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = my_database
然后重啟MySQL:
$ sudo systemctl start mysql
步驟三:創建并配置從節點
$ sudo systemctl stop mysql $ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
在mysqld.cnf中添加以下配置:
server-id = 2 relay-log = /var/log/mysql/mysql-relay-bin.log log_slave_updates = 1 read_only = 1
然后重啟MySQL:
$ sudo systemctl start mysql
步驟四:創建主從關系
mysql>CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; mysql>FLUSH PRIVILEGES; mysql>SHOW MASTER STATUS;
記下輸出的File和Position。
mysql>STOP SLAVE; mysql>CHANGE MASTER TO ->MASTER_HOST='master_ip_address', ->MASTER_USER='repl', ->MASTER_PASSWORD='password', ->MASTER_LOG_FILE='xxxx.log', ->MASTER_LOG_POS=xxxx; mysql>START SLAVE;
步驟五:測試多主從集群
mysql>INSERT INTO my_database.my_table (id, data) VALUES (1, 'test data');
然后在從節點上查詢,應該可以看到插入的數據。
mysql>SELECT * FROM my_database.my_table;
以上就是MySQL多主從集群的搭建過程。
上一篇mysql多主多從架構
下一篇mysql 檢查驅動