同步时间
ntpdate time.nist.gov
先把主库上面的数据先复制到从上 不然从binlog日志之前的数据就没有了
1. 同步的时候 丛库有两个线程 IO/sql 主库 由一个线程完成IO
2. 要在丛库上面要配置 主库的 配置文件 master change to
3. 在开启开关之前 要确定主从数据库的数据是一致的
4. 建立专门做同步的账号
5. 主库要开启binlog
6. 丛库打开开关的过程就是让 io start sql start sleve
主上
设置server id
开启binlog
[root@mangodb ~]# mysqldump -uroot -pddbddb -A -B --events --master-data=2 > rep.sql
--master-data=1
mysql> grant replication slave on *.* to 'rep'@'192.168.138.%' identified by 'ddbddb';
mysql> flush privileges;
就是不让用户写 窗口不能断
mysql> flush table with read lock;
'''
mysql> flush table with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mysql_scott.000001 | 335 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mysql_scott.000001 | 335 |
+--------------------+-----------+
1 row in set (0.00 sec)
mysql> unlocks;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unlocks' at line 1
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mysql_scott.000001 | 335 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
'''
在丛库上面操作
change master to
master_host='192.168.138.140',
master_port=3306,
master_user='rep',
master_password='ddbddb',
master_log_file='mysql_scott.000002',
master_log_pos=107;
mysql 主从同步配置步骤
1. 准备两台数据库环境 能否正常启动和登录
2. 配置my.cnf 文件 主库配置log-bin 和 server-id 参数,丛库配置server-id,不能
和主库及其他丛库一样,一般不开启丛库lob-bin功能, 配置参数后要重启生效
3. 登录主库增加用于丛库链接主库同步的账号 并授权 replication slave 同步的权限 执行完命令以后再刷新权限
mysql> grant replication slave on *.* to 'rep'@'192.168.138.%' identified by 'ddbddb';
mysql> flush privileges;
4. 登录主库 ,整库锁表 flush table with read lock(窗口关闭后即失效,超时参数到了
也会失效,然后 show master status 查看binlog的状态位置
5. 新开窗口,linux命令行备份或导出原有的数据库,并拷贝到从库所在的服务器
如果数据量很大,并且允许停机,可以停机打包,而不用mysqldump
[root@mangodb ~]# mysqldump -uroot -pddbddb -A -B --events --master-data=2 > rep.sql
6. 解锁主库,unlock tables
7.把主库导出的原有数据库恢复到从库
[root@mangodb ~]# mysql -uroot -pddbddb < repl.sql
8. 根据主库的show master status 查看binlog的位置状态,在丛库执行 change master
'''
change master to
MASTER_HOST='192.168.138.140',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='ddbddb',
MASTER_LOG_FILE='mysql_scott.000001',
MASTER_LOG_POS=336;
'''
[root@mangodb mysql]# ls
ibdata1 mangodb.err mangodb-relay-bin.index performance_schema
ib_logfile0 mangodb.pid master.info relay-log.info
ib_logfile1 mangodb-relay-bin.000001 mysql
[root@mangodb mysql]# less master.info
更新的时候会放在master.info 里面
9.丛库开启同步开关,start slave
10 丛库show slave status;G\
master.info 给io线程用的
relay.info 给sql线程用的
如果出现这样的错 首先关掉防火墙 查看时间是否一致
Last_IO_Error: error connecting to master 'repl@192.168.1.40:3306' - retry-time: 60 retries: 86400