跳至内容
wiki
用户工具
登录
站点工具
工具
显示页面
修订记录
反向链接
最近更改
媒体管理器
网站地图
登录
最近更改
媒体管理器
网站地图
您的足迹:
分享:技术:mysql:mysql主从同步
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== mysql主从同步 ====== ===== 环境 ===== 主库:121.43.104.34 mysql版本 [5.5.43-0ubuntu0.14.04.1-log] 从库:121.40.171.96 mysql版本 [5.7.18-0ubuntu0.16.04.1-log] ===== 主库配置 ===== 修改/etc/mysql/my.cnf在[mysqld]下配置 <code> #[必须]启用二进制日志 log-bin = mysql-bin #[必须]服务器唯一ID,默认是1,一般取IP最后一段 server-id = 34 </code> 保存后重启mysql: <code>sudo /etc/init.d/mysql restart</code> 登录mysql <code>mysql -uroot -p</code> 创建用户syn并授权给从服务器: <code>grant replication slave on *.* to 'syn'@'121.40.171.96' identified by 'syn';</code> 刷新权限: <code>flush privileges;</code> 主库状态 <code>show master status;</code> <code> +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 17384 | | | +------------------+----------+--------------+------------------+ </code> ===== 配置从库 ===== 修改/etc/mysql/mysql.conf.d/mysqld.cnf在[mysqld]下配置 <code> #[不是必须]启用二进制日志 log-bin = mysql-bin #[必须]服务器唯一ID,默认是1,一般取IP最后一段 server-id = 96 </code> 保存后重启mysql: <code>sudo /etc/init.d/mysql restart</code> 登录mysql <code>mysql -uroot -p</code> 执行 <code> change master to master_host = '121.43.104.34', master_user = 'syn', master_port=3306, master_password='syn', master_log_file = 'mysql-bin.000003', master_log_pos=17384; </code> 开启复制 <code>start slave;</code> 查看从库状态 <code>show slave status\G;</code> <code> *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 121.43.104.34 Master_User: syn Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 20073 Relay_Log_File: iZbp12rdwfbgblafgipsh5Z-relay-bin.000002 Relay_Log_Pos: 2985 Relay_Master_Log_File: mysql-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: 20073 Relay_Log_Space: 3202 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: 34 Master_UUID: Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) ERROR: No query specified </code> 到这里,主从就算配置完了,在主库create database test;,从库就马上看到新建的数据库。 ===== 数据库迁移 ===== 有个问题,如果主库已经存在的一个数据库,在里面做修改,同步到从库会报错,类似:不存在该库表XXXXX 这时候就得按以下方法,先把主库整个导出再导入到从库,然后开始同步就不会报错。 备份数据库,输入密码 <code>mysqldump -u root -p record_app>/mnt/lost+found/backup/mysql/record_app_`date +%Y%m%d`.sql</code> 拷贝备份的sql文件到从库机器 <code>scp /mnt/lost+found/backup/mysql/record_app_20170526.sql gxx@121.40.171.96:/home/gxx/</code> 主库状态 <code>show master status;</code> <code> +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 17385 | | | +------------------+----------+--------------+------------------+ </code> 登陆从库,停止复制 <code>stop slave;</code> 创建数据库 <code>create database record_app;</code> 使用数据库 <code>use record_app;</code> 导入数据库 <code>source /home/gxx/record_app_20170526.sql</code> 执行 <code> change master to master_host = '121.43.104.34', master_user = 'syn', master_port=3306, master_password='syn', master_log_file = 'mysql-bin.000003', master_log_pos=17385; </code> 开启同步 <code>start slave;</code>
分享/技术/mysql/mysql主从同步.txt
· 最后更改: 2017/05/26 18:35 由
gxx
页面工具
显示页面
修订记录
反向链接
回到顶部