新環境搭建Mysql主從
環境資訊:
1、Mysql安裝
參考http://blog.itpub.net/28536251/viewspace-2138854分別在兩節點安裝Mysql。
2、主節點配置
(1)修改配置檔案
[root@dbrac16 ~]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
symbolic-links=0
character-set-server=utf8
server-id = 8416
log-bin=/usr/local/mysql/data/mysql-bin
binlog_format=mixed
slow-query-log
long_query_time=3
log-output=TABLE
expire_logs_days = 7
event_scheduler=1
innodb_file_per_table=1
innodb_log_file_size=536870912
innodb_buffer_pool_size=1073741824
max_binlog_size=1073741824
log_bin_trust_function_creators=1
innodb_stats_on_metadata=0
max_connect_errors = 1000000
max_connections = 5000
skip_name_resolve = 1
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/my.pid
[client]
socket=/usr/local/mysql/mysql.sock
[mysql]
prompt=(\u@\h)[\d]\_
(2)重啟Mysql
[root@dbrac16 ~]# /etc/init.d/mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL. SUCCESS!
(3)建立複製使用者
(root@localhost)[(none)] GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.163.84.%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected, 1 warning (0.06 sec)
(root@localhost)[(none)] flush privileges;
Query OK, 0 rows affected (0.08 sec)
(4)獲取日誌資訊
(root@localhost)[(none)] show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
3、從節點配置
(1)修改配置檔案
[root@dbrac17 ~]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
symbolic-links=0
character-set-server=utf8
server-id=8417
slow-query-log
long_query_time=3
log-output=TABLE
expire_logs_days = 7
event_scheduler=1
innodb_file_per_table=1
innodb_log_file_size=536870912
innodb_buffer_pool_size=1073741824
max_binlog_size=1073741824
log_bin_trust_function_creators=1
innodb_stats_on_metadata=0
max_connect_errors = 1000000
max_connections = 5000
skip_name_resolve = 1
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/my.pid
[client]
socket=/usr/local/mysql/mysql.sock
[mysql]
prompt=(\u@\h)[\d]\_
(2)刪除auto.cnf檔案
[root@dbrac17 ~]# rm /usr/local/mysql/data/auto.cnf
rm: remove regular file `/usr/local/mysql/data/auto.cnf'? y
(3)重啟Mysql
[root@dbrac17 ~]# /etc/init.d/mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL. SUCCESS!
(4)配置到主節點的連線
(root@localhost)[(none)] CHANGE MASTER TO MASTER_HOST='10.163.84.16',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
(5)啟動複製並檢視狀態
(root@localhost)[(none)] start slave;
Query OK, 0 rows affected (0.03 sec)
(root@localhost)[(none)] show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.163.84.16
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: dbrac17-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 529
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 8416
Master_UUID: 3de828ce-354c-11e7-9f0b-0050568a4cf6
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.02 sec)
Slave_IO_Running: Yes表示獲取日誌正常。
Slave_SQL_Running: Yes表示日誌應用正常。
4、測試
(1)主節點建立庫,表並插入資料
(root@localhost)[(none)] create database test;
Query OK, 1 row affected (0.01 sec)
(root@localhost)[(none)] use test;
Database changed
(root@localhost)[test] create table tb1(id int);
Query OK, 0 rows affected (0.07 sec)
(root@localhost)[test] insert into tb1 values(1);
Query OK, 1 row affected (0.04 sec)
(2)從節點檢視
(root@localhost)[(none)] use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
(root@localhost)[test] select * from tb1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
用途 | IP | OS | Mysql |
主 | 10.163.84.16 | RHEL 6.5_X64 | mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz |
從 | 10.163.84.17 | RHEL 6.5_X64 | mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz |
1、Mysql安裝
參考http://blog.itpub.net/28536251/viewspace-2138854分別在兩節點安裝Mysql。
2、主節點配置
(1)修改配置檔案
[root@dbrac16 ~]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
symbolic-links=0
character-set-server=utf8
server-id = 8416
log-bin=/usr/local/mysql/data/mysql-bin
binlog_format=mixed
slow-query-log
long_query_time=3
log-output=TABLE
expire_logs_days = 7
event_scheduler=1
innodb_file_per_table=1
innodb_log_file_size=536870912
innodb_buffer_pool_size=1073741824
max_binlog_size=1073741824
log_bin_trust_function_creators=1
innodb_stats_on_metadata=0
max_connect_errors = 1000000
max_connections = 5000
skip_name_resolve = 1
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/my.pid
[client]
socket=/usr/local/mysql/mysql.sock
[mysql]
prompt=(\u@\h)[\d]\_
(2)重啟Mysql
[root@dbrac16 ~]# /etc/init.d/mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL. SUCCESS!
(3)建立複製使用者
(root@localhost)[(none)] GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.163.84.%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected, 1 warning (0.06 sec)
(root@localhost)[(none)] flush privileges;
Query OK, 0 rows affected (0.08 sec)
(4)獲取日誌資訊
(root@localhost)[(none)] show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
3、從節點配置
(1)修改配置檔案
[root@dbrac17 ~]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
symbolic-links=0
character-set-server=utf8
server-id=8417
slow-query-log
long_query_time=3
log-output=TABLE
expire_logs_days = 7
event_scheduler=1
innodb_file_per_table=1
innodb_log_file_size=536870912
innodb_buffer_pool_size=1073741824
max_binlog_size=1073741824
log_bin_trust_function_creators=1
innodb_stats_on_metadata=0
max_connect_errors = 1000000
max_connections = 5000
skip_name_resolve = 1
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/my.pid
[client]
socket=/usr/local/mysql/mysql.sock
[mysql]
prompt=(\u@\h)[\d]\_
(2)刪除auto.cnf檔案
[root@dbrac17 ~]# rm /usr/local/mysql/data/auto.cnf
rm: remove regular file `/usr/local/mysql/data/auto.cnf'? y
(3)重啟Mysql
[root@dbrac17 ~]# /etc/init.d/mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL. SUCCESS!
(4)配置到主節點的連線
(root@localhost)[(none)] CHANGE MASTER TO MASTER_HOST='10.163.84.16',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
(5)啟動複製並檢視狀態
(root@localhost)[(none)] start slave;
Query OK, 0 rows affected (0.03 sec)
(root@localhost)[(none)] show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.163.84.16
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: dbrac17-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 529
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 8416
Master_UUID: 3de828ce-354c-11e7-9f0b-0050568a4cf6
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.02 sec)
Slave_IO_Running: Yes表示獲取日誌正常。
Slave_SQL_Running: Yes表示日誌應用正常。
4、測試
(1)主節點建立庫,表並插入資料
(root@localhost)[(none)] create database test;
Query OK, 1 row affected (0.01 sec)
(root@localhost)[(none)] use test;
Database changed
(root@localhost)[test] create table tb1(id int);
Query OK, 0 rows affected (0.07 sec)
(root@localhost)[test] insert into tb1 values(1);
Query OK, 1 row affected (0.04 sec)
(2)從節點檢視
(root@localhost)[(none)] use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
(root@localhost)[test] select * from tb1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2138928/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux環境中MySQL主從同步–新增新的從庫LinuxMySql主從同步
- 用 Docker 構建 MySQL 主從環境DockerMySql
- Docker環境搭建redis叢集(主從模式)DockerRedis模式
- 如何基於生產環境mysql 5.6.25主從部署新的mysql從庫操作指南MySql
- MySQL Xtrabackup真實生產環境搭建主從複製全過程MySql
- Redis多例項及主從複製環境搭建Redis
- mysql主從搭建MySql
- Mac 新環境搭建Mac
- Windows 環境下,MySQL 的主從複製和主主複製WindowsMySql
- windows環境下,Mysql的主從複製和主主複製WindowsMySql
- 基於docker環境下搭建redis主從叢集DockerRedis
- MYSQL主從搭建5.6.38MySql
- 手把手教你,如何在windows系統搭建mysql主從複製的環境WindowsMySql
- 分分鐘搭建MySQL一主多從環境(r12筆記第31天)MySql筆記
- ORACLE 12C DATAGUARD環境搭建和主從切換Oracle
- mysql8.0以後的版本,進行多主一從的叢集環境搭建MySql
- Docker 方式 MySQL 主從搭建DockerMySql
- mysql主從複製搭建MySql
- mysql主從搭建切換MySql
- MySQL(14)---Docker搭建MySQL主從複製(一主一從)MySqlDocker
- CentOS7.8 環境搭建 Redis 主從複製和哨兵模式CentOSRedis模式
- MYSQL5.6.40原始碼安裝 主從搭建 主主搭建MySql原始碼
- MySQL環境搭建利器---SandboxMySql
- LAMP環境搭建-MySQL5.6LAMPMySql
- MySQL 5.7主從新增新從庫MySql
- 主從環境中,從IO程式被停掉
- mysql雙主雙從 搭建配置MySql
- MYSQL主從複製的搭建MySql
- Mysql主從搭建(docker compose)MySqlDocker
- 揭祕MySQL 主從環境中大事務的傳奇事蹟MySql
- DM8 MPP主備環境搭建
- 從環境搭建到打包使用TypeScriptTypeScript
- VUE從零開始環境搭建Vue
- 在容器環境搭建mysql備庫MySql
- CentOS 7.9 環境下搭建k8s叢集(一主兩從)CentOSK8S
- 記一次 MySQL 主從搭建MySql
- 基於GTID搭建主從MySQLMySql
- Mysql主從複製原理及搭建MySql