mysql replication

<!-- /* Font Definitions */ @font-face {font-family:Courier; panose-1:2 7 4 9 2 2 5 2 4 4; mso-font-alt:"Courier New"; mso-font-charset:0; mso-generic-font-family:modern; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:Arial; mso-fareast-font-family:宋体; mso-fareast-language:EN-US;} h3 {mso-style-next:Normal; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; line-height:173%; mso-pagination:widow-orphan lines-together; page-break-after:avoid; mso-outline-level:3; font-size:16.0pt; font-family:Arial; mso-fareast-language:EN-US;} p.MsoHeading9, li.MsoHeading9, div.MsoHeading9 {mso-style-update:auto; mso-style-parent:"List Bullet"; mso-style-next:Normal; margin:0cm; margin-bottom:.0001pt; line-height:150%; mso-pagination:widow-orphan; page-break-after:avoid; mso-outline-level:9; font-size:12.0pt; font-family:Arial; mso-fareast-font-family:宋体; mso-fareast-language:EN-US; mso-bidi-font-weight:bold;} p.MsoFooter, li.MsoFooter, div.MsoFooter {margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 216.0pt right 432.0pt; font-size:8.0pt; font-family:Arial; mso-fareast-font-family:宋体; mso-fareast-language:EN-US;} p.MsoListBullet, li.MsoListBullet, div.MsoListBullet {margin-top:0cm; margin-right:0cm; margin-bottom:0cm; margin-left:18.0pt; margin-bottom:.0001pt; text-indent:-18.0pt; mso-char-indent-count:-2.0; mso-pagination:widow-orphan; tab-stops:list 18.0pt; font-size:10.0pt; font-family:Arial; mso-fareast-font-family:宋体; mso-fareast-language:EN-US;} p.MsoBodyText, li.MsoBodyText, div.MsoBodyText {mso-style-update:auto; margin-top:0cm; margin-right:0cm; margin-bottom:6.0pt; margin-left:15.0pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:6.0pt; mso-para-margin-left:1.5gd; text-indent:10.0pt; mso-char-indent-count:1.0; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:10.0pt; font-family:Arial; mso-fareast-font-family:宋体;} p.ITISSectionHeadline, li.ITISSectionHeadline, div.ITISSectionHeadline {mso-style-name:"ITIS Section Headline"; mso-style-parent:"Heading 3"; mso-style-next:Normal; margin-top:12.0pt; margin-right:0cm; margin-bottom:12.0pt; margin-left:0cm; page-break-before:always; mso-pagination:widow-orphan; page-break-after:avoid; mso-outline-level:1; border:none; mso-border-bottom-alt:solid windowtext 3.0pt; padding:0cm; mso-padding-alt:0cm 0cm 1.0pt 0cm; font-size:18.0pt; font-family:Arial; mso-fareast-font-family:宋体; mso-fareast-language:EN-US; font-weight:bold;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-page-numbers:1; mso-paper-source:0;} div.Section1 {page:Section1;} -->

MySql has its own user:group, which typically is mysql:mysql.

 

Setup Master

 

Add mysql account on the master server

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'slave_host_ip' IDENTIFIED BY 'slavepass';

   #Then slave can access master with master_user=’repl’ and master_password='slavepass'’’.

Flush all the tables and block write statements

mysql> FLUSH TABLES WITH READ LOCK;

#Start a new mysql client and keep the running client from which you issued the flush tables, #because it can be unlocked automatically at the close of the client.

#For InnoDB, additionally the #mysql should be shutdown to insure a consistent snapshot for InnoDB tables.

Remember the master_log_file and master_log_pos which are needed by slaves.

  mysql > SHOW MASTER STATUS;

Make a snapshot of all the databases(Or just the databases which will be synchronized with slaves)

Cd /usr/local/mytools/repository/mysql_version/data

#data is where the database data are stored, if no data directory, there should be var directory.

tar -cvf /tmp/mysql-snapshot.tar .

mysql>unlock tables;

Modify my.cnf

Cd /usr/local/mytools/repository/mysql_5.0.51a

Vim my.cnf

# Add the following lines right bellow [client]

socket          = /local/tools/repository/mysql-5.0.51a/tmp/mysql.sock

port            = 3306

# Add the following lines right below [mysqld]

socket          = /local/tools/repository/mysql-5.0.51a/tmp/mysql.sock

port            = 3306

sync_binlog = 1               #enable update statements log

log-bin=/usr/local/mytools/repository/mysql-5.0.51a/data/mysql-bin     #set log file name

binlog-do-db = replication_dbname1    #set all the replication databases

binlog-do-db = replication_dbname2

slave_skip_errors=all

service_id=n        # ‘n’ is a positive integer unique among master and slaves

user=mysql

#For InnoDB, additionally the following lines will be added.

innodb_data_home_dir = /usr/local/mytools/repository/mysql-5.0.51a/data/

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mytools/repository/mysql-5.0.51a/data/

innodb_log_arch_dir = /usr/local/mytools/repository/mysql-5.0.51a/data/

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

innodb_buffer_pool_size = 16M

innodb_additional_mem_pool_size = 2M

# Set .._log_file_size to 25 % of buffer pool size

innodb_log_file_size = 5M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

 

#After the preceding procedures, We can restart MySql for master.

Check master

mysql> show master status /G;

*************************** 1. row ***************************

File: mysql-bin.000022

Position: 774

Binlog_Do_DB: gisus,syn_test

Binlog_Ignore_DB:

1 row in set (0.00 sec)

 

ERROR:

No query specified

Setup Slave

Stop server

Cd /usr/local/mytools/repository/mysql_5.0.51a/bin

./mysqladmin –u username –ppassword shutdown

Modify my.cnf

Cd /usr/local/mytools/repository/mysql_5.0.51a

Vim my.cnf

#Add the following lines right below [client]

socket          = /local/tools/repository/mysql-5.0.51a/tmp/mysql.sock

port            = 3306

#Add the following lines right below [mysqld]

socket          = /local/tools/repository/mysql-5.0.51a/tmp/mysql.sock

port            = 3306

log-bin=/usr/local/mytools/repository/mysql-5.0.51a/data/mysql-bin

replicate-do-db = replication_dbname1    #set all the replication databases by this

replicate-do-db = replication_dbname2

slave_skip_errors=all

sync_binlog = 1

service_id=n        # ‘n’ is positive integer unique among master and slaves

user=mysql

# The replication master for this slave - required

master-host     =   master_host_ip

#

# The account the slave will use for authentication when connecting

# to the master,which is created previously be “Grant…” in master.

master-user     =  repl

master-password =   slavepass

#

# The port the master is listening on.

# optional - defaults to 3306

master-port     =  3306

#

# binary logging - not required for slaves, but recommended

log-bin=/local/tools/repository/mysql-5.0.51a/data/mysql-bin

 

replicate-do-db = replication_dbname1  #indicate databases you want to synchronize.

replicate-do-db = replication_dbname2

replicate-ignore-table = replication_dbname1.tablename_ignore. #Indicate tables not been synchronized inside the specified databases

replicate-ignore-table = replication_dbname2.tablename_ignore

log-slave-updates

slave-skip-errors=all

read-only            #Insure the db is only updated by slave thread.

 

#For InnoDB, additionally the following lines will be added.

innodb_data_home_dir = /local/tools/repository/mysql-5.0.51a/data/    

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /local/tools/repository/mysql-5.0.51a/data/

innodb_log_arch_dir = /local/tools/repository/mysql-5.0.51a/data/

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

innodb_buffer_pool_size = 16M

innodb_additional_mem_pool_size = 2M

# Set .._log_file_size to 25 % of buffer pool size

innodb_log_file_size = 5M

#innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

 

Copy database snapshot mysql-snapshot.tar to /tmp/

Scp root@master_host_ip: /tmp/mysql-snapshot.tar    /tmp/

Unzip snapshot into local database data directory.

Cd /local/tools/repository/mysql-5.0.51a/data/

Tar –xvf /tmp/mysql-snapshot.tar

#Make sure mysql has the access to data/* and my.cnf

Cd /local/tools/repository/mysql-5.0.51a/

Chown mysql:mysql *

Cd /local/tools/repository/mysql-5.0.51a/data/

Chown mysql:mysql *

       

Start mysql without slave

Service mysql start --skip-slave-start

 

Change slave status

Mysql>change master to

        ->master_log_file=’master_log_file’,

      ->master_log_pos=master_log_pos;

#Here we can change more, such as

#->master_host=’master_host’,

#->master_user=’repl’,

#->master_password=’slavepass’,

#->master_port=’3306’,

#but since all the others are correctly configured in my.cnf, no need to do it again.

 

Start slave and check the slave status.

Mysql>slave start;

Mysql>show slave status /G;

#if it appears as follow, then it’s ok.

#            Slave_IO_State: Waiting for master to send event        #attention

#                Master_Host: master_host_ip       e.g 172,24,61,58 #attention

#                   Master_User: repl                              #attention

#                   Master_Port: 3306                                  #attention

#               Connect_Retry: 10

#           Master_Log_File: master_log_file e.g mysql-bin.000022         #attention

#Read_Master_Log_Pos: master_log_pos, e.g 774                #attention

#               Relay_Log_File: e.g aclgcl-134-relay-bin.000087

#             Relay_Log_Pos: 235

#Relay_Master_Log_File: master_log_file e.g mysql-bin.000022          #attention

#        Slave_IO_Running: Yes                             #attention

#      Slave_SQL_Running: Yes                               #attention

#             Replicate_Do_DB: replication_dbnames                     #attention

#      Replicate_Ignore_DB:

#      Replicate_Do_Table:

#Replicate_Ignore_Table: replication_ignore_tables           #attention

#Replicate_Wild_Do_Table:

#Replicate_Wild_Ignore_Table:

#                     Last_Errno: 0

#                      Last_Error:

#                 Skip_Counter: 0

#Exec_Master_Log_Pos: 678

#         Relay_Log_Space: 235

#               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

 

Make a salve server also a master

Modify my.cnf

Add the following lines to my.cnf right bellow [mysqld], then restart the mysql.

binlog-do-db = replication_dbname1    #indicate databases you want to synchronize.

binlog-do-db = replication_dbname2

sync_binlog = 1

Restart the mysql

/etc/rc.d/init.d/mysql restart

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

Create ssh tunneling, as follow, which means routing all requests to local port 33008 to remote server root@sumaster.tctmobile.com on port 127.0.0.1:33007, we can take sumaster.tctmobile.com as a proxy here.

       ssh -p 18082 -N -f -L 33008:127.0.0.1:33007 root@sumaster.tctmobile.com

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值