基础知识不补了,直接搞起。


公司内网,尽管不在一个网段,保证路由畅通就OK了。

主IP:10.241.92.201

从IP:10.241.89.69


第一步:Master -- Slave my.cnf配置,确定主从关系,之后启动mysql.


----master

log-bin=mysql-bin #开启二进制日志,slave会基于此log-bin来做replication,slave不必开启这个

server-id=1          #master的标示

sync_binlog=1               #任何一个事务提交之后就立即写入到磁盘中的二进制文件

innodb_flush_logs_at_trx_commit=1       #任何一个事物提交之后就立即写入到磁盘中的日志文件



----slave

server-id=2#slave的标示


第二步:


在Master上创建用于复制的帐号repl_user,授权从服务器10.241.89.69使用密码为'123456'的repl_user帐号对所有数据库进行复制操作。


------on master

mysql> grant replication slave on *.* to [email protected] identified by '123456';

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001 |      238 |              |                  |

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

1 row in set (0.00 sec)


------on slave

mysql> slave stop;

Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> change master to master_host='10.241.92.201',

-> master_user='repl_user',

-> master_password='123456',

-> master_log_file='mysql-bin.000001',

-> master_log_pos=238;

Query OK, 0 rows affected (0.08 sec)


mysql> start slave;

Query OK, 0 rows affected (0.00 sec)


第三步:测试复制


--------on master

mysql> use test;

mysql> create table a (id int(100));

Query OK, 0 rows affected (0.35 sec)


mysql> show tables;

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

| Tables_in_test |

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

| a              |

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

1 row in set (0.00 sec)


mysql> desc a;

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

| Field | Type     | Null | Key | Default | Extra |

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

| id    | int(100) | YES  |     | NULL    |       |

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

1 row in set (0.00 sec)


mysql> insert into a values(10);

Query OK, 1 row affected (0.00 sec)


mysql> select * from a;

+------+

| id   |

+------+

|   10 |

+------+

1 row in set (0.00 sec)


mysql> insert into a values(11);

Query OK, 1 row affected (0.00 sec)


mysql> select * from a;

+------+

| id   |

+------+

|   10 |

|   11 |

+------+

2 rows in set (0.00 sec)


------on slave

mysql> use test;

mysql> select * from a;

+------+

| id   |

+------+

|   10 |

|   11 |

+------+

2 rows in set (0.00 sec)


mysql> show slave status\G

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

Slave_IO_State: Waiting for master to send event

Master_Host: 10.241.92.201

Master_User: repl_user

Master_Port: 3306

 Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 1293

Relay_Log_File: AY12042509502824de51d-relay-bin.000005

 Relay_Log_Pos: 499

 Relay_Master_Log_File: mysql-bin.000001

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

Relay_Log_Space: 499

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)