mysql 配置互為主從

mysql主從配置

首先可以參考我原來寫的mysql在Linux(CentOS)下主從配置詳解,在此基礎之上加一些簡單的操作就可以配置成功啦。

mysql

配置過程

以下稱呼的主庫和從庫都是原始配置主從的主庫和從庫

  1. 將從庫像主庫一樣建立binlog文件夾并在/etc/my.cnf下配置主庫信息,mysqld下面大概是這些信息,僅供參考。
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
datadir = /data/mysql
port = 3306
character-set-server=utf8
# server_id = .....
socket = /data/mysql/mysql.sock
server-id=102
relay-log=/data/mysql_slave/relaylog/mysql-relay-bin.log
relay-log-index=/data/mysql_slave/relaylog/mysql-relay-bin.index
relay_log_purge=on
log-bin=/data/mysql_master/binlog/mysql-bin.log
log-bin-index=/data/mysql_master/binlog/mysql-bin.index
read-only=on

同樣主庫也去像從庫一樣配置這些信息,這里就不贅述了,接下來都重啟一下。

# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
  1. 查看從庫的master運行狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  1. 在從庫中添加slave用戶
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'your-slave-ip' IDENTIFIED BY 'your-password';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;                                                                         +-------------------------------------+
| query                               |
+-------------------------------------+
| User: 'root'@'%';                   |
| User: 'slave'@'your-slave-ip';     |
| User: 'root'@'127.0.0.1';           |
| User: 'root'@'::1';                 |
| User: 'root'@'vm\_61\_211\_centos'; |
+-------------------------------------+
5 rows in set (0.00 sec)
  1. 主庫中導入
mysql> change master to master_host='119.29.105.144',master_user='slave',master_password='dsg-password',master_port=3306,MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120;
mysql> slave start;
mysql> show slave status\G;

像原來配置主從一樣成功便是成功。
如果連接不上,別忘了將從庫的防火墻打開。

# service firewalld stop
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容