master 192.168.0.9
slave 192.168.0.10
1主備都安裝mysql軟件
2編輯master /etc/my.conf
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql log-bin=mysql-bin server-id=1 innodb_flush_log_at_trx_commit=1 sync_binlog=1 # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
3授權slave
[root@localhost mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.0.45-log Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> grant replication slave,reload,super on *.* to 'slave'@'192.168.0.10' identified by '10086'; mysql> flush privilegges;
從庫登陸測試
mysql -uslave -p10086 -h 192.168.0.9
出現OK成功
4編輯slave /etc/my.cnf文件
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql server-id=2 master-host=192.168.0.9 master-user=slave master-password=10086 # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
5、在master上查看主庫狀態
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 7156 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
6、查看從庫狀態
mysql> slave stop; mysql> change master to master_host='192.168.0.9',master_user='slave',master_password='10086',master_log_file='mysql-bin.000001',master_log_pos=7156; mysql> slave start mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.9 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 7156 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 7068 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: 7156 Relay_Log_Space: 7068 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)
7、測試master建立test10測試庫,創建一個abc表,插入數據,查看是否同步到slave上
mysql> create database test10; Query OK, 1 row affected (0.00 sec) mysql> use test10; Database changed mysql> create table abc (name char,age int); Query OK, 0 rows affected (0.01 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.02 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.02 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> insert into abc (name,age) values ('zhang',22); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> insert into abc (name,age) values ('zhang',22);
查看slave是否有相同的信息
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test1 | | test10 | +--------------------+ 5 rows in set (0.00 sec) mysql> use test10 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 mysql> select * from abc; +------+------+ | name | age | +------+------+ | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | | z | 22 | +------+------+
成功