mysql A 已有数据
mysql B 空的
想要主从,先保持两边的有一样的数据 ;
备份A :mysqldump -uroot -p'XXXXXXX' -f --all-databases > all.sql
其中 -f 为跳过sql报错继续备份(很有必要),--all-databases 就是直接备份所有了
把all.sql 传到B
mysql> source /home/ubuntu/all.sql (进入数据库执行)
自己检查一下数据是否大差不差。
主从:
主:
配置文件:
server-id = 1
log-bin = /var/log/mysql/mysql-bin.log
改完配置需重启
给从机授权:
mysql> grant all privileges on *.* to 'root'@'从机ip' identified by 'xxxxx';
mysql> flush privileges;
mysql> show master status ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 5795045 | | |
+------------------+----------+--------------+------------------+
从机:
配置文件:
server-id = 2
改完重启
认主:
mysql>slave stop;
mysql> change master to master_host='主机ip', master_user='root', master_password='xxxxx', master_log_file='mysql-bin.000002', master_log_pos=5795045 ;
Query OK, 0 rows affected (0.00 sec)
mysql>slave start;
Query OK, 0 rows affected (0.00 sec)
到这里就配置完成了
可查看状态:
mysql> show slave status\G ;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: xxxxxxx
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 5834567
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 39775
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: 5834567
Relay_Log_Space: 39932
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: 1
1 row in set (0.00 sec)
ERROR:
No query specified
两个yes证明是好的,,如果操作没错,,基本都会是好的。
网友评论