技术开发 频道

Replication安装配置

  IT168技术资讯1.确保master和slave安装了相同的mysql版本

  2.在master上创建复制使用的用户,并授予 replication slave权限

  mysql> grant replication slave on *.* to 'repl'@'192.168.60.4' identified by 'hunter0';

  Query OK, 0 rows affected (0.01 sec)

  mysql> flush privileges;

  Query OK, 0 rows affected (0.00 sec)

  3.修改master上的my.cnf,开启binlog,并设置server-id

  # The MySQL server

  [mysqld]

  server-id=1

  port = 3306

  socket = /var/lib/mysql/mysql.sock

  datadir = /usr/local/mysql/data

  log-error = /var/lib/mysql/test2_mysqld.err

  #log-bin

  log-bin = /var/lib/mysql/test2_log-bin

  #query-log

  #log = /var/lib/mysql/query_log.log

  #slow-query-log

  log-slow-queries = /var/lib/mysql/slow_query_log.log

  skip-innodb

  ...................

  4.在master上设置读锁定有效

  mysql> flush tables with read lock;

  Query OK, 0 rows affected (0.00 sec)

  5.查看binlog和偏移量值,目的是为了使slave启动后,从这点开始数据恢复操作

  mysql> show master status;

  +----------------------+----------+--------------+------------------+

  | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

  +----------------------+----------+--------------+------------------+

  | test2_log-bin.000002 | 311 | | |

  +----------------------+----------+--------------+------------------+

  1 row in set (0.00 sec)

  6.停止master数据库,并将备份传到slave上(也可使用mysqldump等)

  [root@test2 mysql]# mysqladmin -uroot -p shutdown

  Enter password:

  STOPPING server from pid file /usr/local/mysql/data/test2.pid

  080313 16:08:55 mysqld ended

  [1]+ Done mysqld_safe --user=mysql

  [root@test2 mysql]# cd /usr/local/mysql

  [root@test2 mysql]# tar -cvf data.tar data

  7.备份完后,恢复写操作

  mysql> unlock tables;

  Query OK, 0 rows affected (0.01 sec)

  8.在slvae上将数据备份解压到相应的目录

  [root@test1 mysql]# mysqladmin -uroot -p shutdown

  Enter password:

  STOPPING server from pid file /usr/local/mysql/data/test1.pid

  080229 02:26:53 mysqld ended

  [1]+ Done ./mysqld_safe --user=mysql (wd: /usr/local/mysql/bin)

  (wd now: /usr/local/mysql)

  [root@test1 mysql]# tar -xvf data.tar

  9.修改slave上的my.cnf,设置server-id(server-id是唯一的)

  [mysqld]

  datadir = /usr/local/mysql/data

  socket=/var/lib/mysql/mysql.sock

  log-error = /var/lib/mysql/test1_mysqld.err

  log-slow-queries = /var/lib/mysql/slow_query_log.log

  log-bin = /var/lib/mysql/test1_log-bin

  server-id=2

  10.在slave上,使用--skip-slave-start启动mysql,这个参数据不用马上启动复制进程.这样方便我们下一步配置

  [root@test1 mysql]# bin/mysqld_safe --skip-slave-start --user=mysql &

  [1] 8185

  [root@test1 mysql]# Starting mysqld daemon with databases from /usr/local/mysql/data

  11.对slave做相应的配置

  mysql> show slave status;

  Empty set (0.16 sec)

  mysql> change master to

  -> master_host='192.168.60.3',

  -> master_port=3306,

  -> master_user='repl',

  -> master_password='hunter0',

  -> master_log_file='test2_log-bin.000002',

  -> master_log_pos=311 ;

  Query OK, 0 rows affected (0.08 sec)

  mysql> show slave status \G;

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

  Slave_IO_State:

  Master_Host: 192.168.60.3

  Master_User: repl

  Master_Port: 3306

  Connect_Retry: 60

  Master_Log_File: test2_log-bin.000002

  Read_Master_Log_Pos: 311

  Relay_Log_File: test1-relay-bin.000001

  Relay_Log_Pos: 4

  Relay_Master_Log_File: test2_log-bin.000002

  Slave_IO_Running: No ------注意还有启动slave

  Slave_SQL_Running: No ------注意还有启动slave

  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: 311

  Relay_Log_Space: 98

  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: NULL

  1 row in set (0.01 sec)

  12.在slave上启动slave进程

  mysql> start slave;

  Query OK, 0 rows affected (0.00 sec)

  mysql> show warnings;

  Empty set (0.02 sec)

  mysql> show slave status \G;

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

  Slave_IO_State: Waiting for master to send event

  Master_Host: 192.168.60.3

  Master_User: repl

  Master_Port: 3306

  Connect_Retry: 60

  Master_Log_File: test2_log-bin.000003

  Read_Master_Log_Pos: 98

  Relay_Log_File: test1-relay-bin.000003

  Relay_Log_Pos: 239

  Relay_Master_Log_File: test2_log-bin.000003

  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: 98

  Relay_Log_Space: 239

  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

  1 row in set (0.00 sec)

  ERROR:

  No query specified

  mysql> show processlist \G ;

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

  Id: 1

  User: root

  Host: localhost

  db: NULL

  Command: Query

  Time: 0

  State: NULL

  Info: show processlist

  *************************** 2. row ***************************

  Id: 2

  User: system user

  Host:

  db: NULL

  Command: Connect

  Time: 29

  State: Waiting for master to send event

  Info: NULL

  *************************** 3. row ***************************

  Id: 3

  User: system user

  Host:

  db: NULL

  Command: Connect

  Time: 18

  State: Has read all relay log; waiting for the slave I/O thread to update it

  Info: NULL

  3 rows in set (0.00 sec)

  ERROR:

  No query specified

  13.检验复制

  (1).在master上

  mysql> use test;

  Database changed

  mysql> show tables;

  +----------------+

  | Tables_in_test |

  +----------------+

  | pet |

  +----------------+

  1 row in set (0.00 sec)

  mysql> create table t1(id int,name char(10));

  Query OK, 0 rows affected (0.02 sec)

  mysql> insert into t1 values(1,'yxyup');

  Query OK, 1 row affected (0.00 sec)

  (2)在slave上查看

  mysql> show tables;

  +----------------+

  | Tables_in_test |

  +----------------+

  | pet |

  | t1 |

  +----------------+

  2 rows in set (0.00 sec)

  mysql> select * from t1;

  +------+-------+

  | id | name |

  +------+-------+

  | 1 | yxyup |

  +------+-------+

  1 row in set (0.00 sec)

  复制指定的数据库

  1.在master上操作

  mysql> show databases;

  +--------------------+

  | Database |

  +--------------------+

  | information_schema |

  | mysql |

  | test |

  +--------------------+

  3 rows in set (0.00 sec)

  mysql> use test;

  Database changed

  mysql> create table t1(id int,name char(10));

  Query OK, 0 rows affected (0.02 sec)

  mysql> create table t2(id int,name char(8));

  Query OK, 0 rows affected (0.07 sec)

  在slave上查看

  mysql> use test;

  Database changed

  mysql> show tables;

  +----------------+

  | Tables_in_test |

  +----------------+

  | pet |

  | t1 |

  | t2 |

  +----------------+

  3 rows in set (0.00 sec)

  2.设置复制指定数据库

  在slave上的my.cnf中设置如下(加入replicate-do-db)

  ......

  replicate-do-db=test

  ......

  3.启动slave

  [root@test1 mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql &

  [1] 8357

  [root@test1 mysql]# Starting mysqld daemon with databases from /usr/local/mysql/data

  4.检验

  (1).在master上

  mysql> use test;

  Database changed

  mysql> create table t3(id int,name char(8));

  Query OK, 0 rows affected (0.01 sec)

  mysql> create table t3(id int,name char(8));

  Query OK, 0 rows affected (0.01 sec)

  mysql> show tables;

  +----------------+

  | Tables_in_test |

  +----------------+

  | pet |

  | t1 |

  | t2 |

  | t3 |

  +----------------+

  4 rows in set (0.00 sec)

  mysql> use dbatest;

  Database changed

  mysql> show tables;

  Empty set (0.00 sec)

  mysql> create table t11(id int,name char(8));

  Query OK, 0 rows affected (0.01 sec)

  mysql> show tables;

  +-------------------+

  | Tables_in_dbatest |

  +-------------------+

  | t11 |

  +-------------------+

  1 row in set (0.00 sec)

  (2)在slave上查看同一个数据库test

  mysql> use test;

  Database changed

  mysql> show tables;

  +----------------+

  | Tables_in_test |

  +----------------+

  | pet |

  | t1 |

  | t2 |

  | t3 |

  +----------------+

  4 rows in set (0.01 sec)

  (3)在slvae查看不同数据库dbatest(可以看到没有复制过来了)

  mysql> use dbatest;

  Database changed

  mysql> show tables;

  Empty set (0.00 sec)

  mysql> show tables;

  Empty set (0.01 sec)

  参数说明

  relay-log= /var/lib/mysql/test1-relay-bin -----设置中断日志存位置和中断日志名字

  relay-log-purge=1 ----- 在不需要中继日志时禁用或启用自动清除。默认值是1(启用)。

0
相关文章