| 订阅 | 在线投稿
分享
 
 
 

实例讲解如何配置MySQL数据库主从复制

来源:互联网  宽屏版  评论
2008-06-01 02:59:52

本文为【实例讲解如何配置MySQL数据库主从复制】的汉字拼音对照版显示拼音

zhepianluntanwenzhang(saidiwangjishushequzhuyaojieshaoleMySQLshujukuzhucongfuzhidexiangguangainianjishezhifangfaxiangxineirongqingdajiashenkaoxiawen

MySQLzhichidanxiangyibufuzhifuzhiguochengzhongyigefuwuqichongdangzhufuwuqieryigehuoduogeqitafuwuqichongdangcongfuwuqizhufuwuqijianggengxinxieruerjinzhirizhiwenjianbingweihurizhiwenjiandeyigesuoyinyigenzongrizhixunhuandangyigecongfuwuqilianjiedaozhufuwuqishitatongzhizhufuwuqicongfuwuqizairizhizhongduqudezuihouyicichenggonggengxindeweizhicongfuwuqijieshoucongnashiqifashengderenhegengxinranhoufengsuobingdengdaizhufuwuqitongzhixiayicigengxin

weishenme使shiyongzhucongfuzhi

1zhufuwuqi/congfuwuqishezhizengjialejianzhuangxingzhufuwuqichuxianwentishinikeyiqiehuandaocongfuwuqizuoweibeifen

2tongguozaizhufuwuqihecongfuwuqizhijianqiefenchulikehuchaxundefuhekeyidedaogenghaodekehuxiangyingshijiandanshibuyaotongshizaizhucongfuwuqishangjinxinggengxinzheyangkenengyinqichongtu

3使shiyongfuzhidelingyigehaochushikeyi使shiyongyigecongfuwuqizhixingbeifenerbuhuiganraozhufuwuqizaibeifenguochengzhongzhufuwuqikeyijixuchuligengxin

MySQL使shiyong3ge线xianchenglaizhixingfuzhigongneng(qizhong1gezaizhufuwuqishanglinglianggezaicongfuwuqishangdangfachuSTART SLAVEshicongfuwuqichuangjianyigeI/O线xianchengyilianjiezhufuwuqibingrangzhufuwuqifasongerjinzhirizhizhufuwuqichuangjianyige线xianchengjiangerjinzhirizhizhongdeneirongfasongdaocongfuwuqicongfuwuqiI/O线xianchengduquzhufuwuqiBinlog Dump线xianchengfasongdeneirongbingjianggaishujukaobeidaocongfuwuqishujumuluzhongdebendiwenjianzhongjizhongjirizhidi3ge线xianchengshiSQL线xianchengcongfuwuqi使shiyongci线xianchengduquzhongjirizhibingzhixingrizhizhongbaohandegengxinSHOW PROCESSLISTyujukeyichaxunzaizhufuwuqishanghecongfuwuqishangfashengdeguanyufuzhidexinxi

morenzhongjirizhi使shiyonghost_name-relay-bin.nnnnnnxingshidewenjianmingqizhonghost_nameshicongfuwuqizhujimingnnnnnnshixuliehaoyonglianxuxuliehaolaichuangjianlianxuzhongjirizhiwenjiancong000001kaishicongfuwuqigenzongzhongjirizhisuoyinwenjianlaishibiemuqianzheng使shiyongdezhongjirizhimorenzhongjirizhisuoyinwenjianmingweihost_name-relay-bin.indexzaimorenqingkuangzhexiewenjianzaicongfuwuqideshujumuluzhongbeichuangjianzhongjirizhiyuerjinzhirizhidegeshixiangtongbingqiekeyiyongmysqlbinlogduqudangSQL线xianchengzhixingwanzhongjirizhizhongdesuoyoushijianhouzhongjirizhijianghuibeizidongshanchu

congfuwuqizaishujumuluzhonglingwaichuangjianlianggezhuangtaiwenjian--master.infoherelay-log.infozhuangtaiwenjianbaocunzaiyingpanshangcongfuwuqiguanbishibuhuidiushixiacicongfuwuqiqidongshiduquzhexiewenjianyiquedingtayijingcongzhufuwuqiduquleduoshaoerjinzhirizhiyijichulizijidezhongjirizhidechengdu

shezhizhucongfuzhi

1quebaozaizhufuwuqihecongfuwuqishanganzhuangdeMySQLbanbenxiangtongbingqiezuihaoshiMySQLdezuixinwendingbanben

2zaizhufuwuqishangweifuzhishezhiyigelianjiezhanghugaizhanghubixushouyuREPLICATION SLAVEquanxianruguozhanghujinyongyufuzhi(tuijianzheyangzuo)zebuxuyaozaishouyurenheqitaquanxian

mysql> GRANT REPLICATION SLAVE ON *.*

-> TO 'replication'@'%.yourdomain.com' IDENTIFIED BY 'slavepass';

3zhixingFLUSH TABLES WITH READ LOCKyujuqingkongsuoyoubiaohekuaixieruyuju

mysql> FLUSH TABLES WITH READ LOCK

baochimysqlkehuduanchengxubuyao退tuichukaiqilingyigezhongduanduizhufuwuqishujumuluzuokuaizhao

shell> cd /usr/local/mysql/

shell> tar -cvf /tmp/mysql-snapshot.tar ./data

ruguocongfuwuqideyonghuzhanghuyuzhufuwuqidebutongnikenengbuxiangfuzhimysqlshujukuzaizhezhongqingkuangxiayingcongguidangzhongpaichugaishujukuniyebuxuyaozaiguidangzhongbaokuorenherizhiwenjianhuozhemaster.infohuorelay-log.infowenjian

dangFLUSH TABLES WITH READ LOCKsuozhidusuodingyouxiaoshijimysqlkehuduanchengxubu退tuichu)duquzhufuwuqishangdangqiandeerjinzhirizhiminghepianyiliangzhi

mysql > SHOW MASTER STATUS;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.003 | 73 | test | manual,mysql |

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

FileliexianshirizhimingerPositionxianshipianyiliangzaigailizizhongerjinzhirizhizhiweimysql-bin.003pianyiliangwei73jilugaizhiyihoushezhicongfuwuqishixuyao使shiyongzhexiezhitamenbiaoshifuzhizuobiaocongfuwuqiyingconggaidiankaishicongzhufuwuqishangjinxingxindegengxin

ruguozhufuwuqiyunxingshimeiyouqiyong--logs-binSHOW MASTER STATUSxianshiderizhimingheweizhizhiweikongzaizhezhongqingkuangxiadangyihouzhidingcongfuwuqiderizhiwenjianheweizhishixuyao使shiyongdezhiweikongzifuchuan('')he4.

qudekuaizhaobingjilurizhiminghepianyilianghouhuidaoqianyizhongduanchongxinqiyongxiehuodong

mysql> UNLOCK TABLES

4quebaozhufuwuqizhujishangmy.cnfwenjiande[mysqld]bufenbaokuoyigelog-binxuanxianggaibufenhuanyingyouyigeserver-id=Master_idxuanxiangqizhongmaster_idbixuwei1dao2321zhijiandeyigezhengzhengshuzhiliru

[mysqld]

log-bin

server-id=1

ruguomeiyoutigongnaxiexuanxiangyingtianjiatamenbingchongqifuwuqi

5tingzhicongfuwuqishangdemysqldfuwubingzaiqimy.cnfwenjianzhongtianjiaxiamiandexing

[mysqld]

server-id=2

slave_idzhitongMaster_idzhiyiyangbixuwei1dao2321zhijiandeyigezhengzhengshuzhibingqiecongfuwuqideIDbixuyuzhufuwuqideIDbuxiangtong

6jiangshujubeijumuluzhongquebaoduizhexiewenjianhemuludequanxianzhengquefuwuqi MySQLyunxingdeyonghubixunenggouduxiewenjianrutongzaizhufuwuqishangyiyang

Shell> chown -R mysql:mysql /usr/local/mysql/data

7qidongcongfuwuqizaicongfuwuqishangzhixingxiamiandeyujuyongnidexitongdeshijizhitihuanxuanxiangzhi

mysql> CHANGE MASTER TO

-> MASTER_HOST='master_host_name',

-> MASTER_USER='replication_user_name',

-> MASTER_PASSWORD='replication_password',

-> MASTER_LOG_FILE='recorded_log_file_name',

-> MASTER_LOG_POS=recorded_log_position;

8qidongcongfuwuqi线xiancheng

mysql> START SLAVE

zhixingzhexiechengxuhoucongfuwuqiyinglianjiezhufuwuqibingbuchongzicongkuaizhaoyilaifashengderenhegengxin

9ruguochuxianfuzhicuowucongfuwuqidecuowurizhiHOSTNAME.errzhongyehuichuxiancuowuxiaoxi

10congfuwuqifuzhishihuizaiqishujumuluzhongfaxianwenjianmaster.infoheHOSTNAME-relay-log.infocongfuwuqi使shiyongzhelianggewenjiangenzongyijingchulileduoshaozhufuwuqideerjinzhirizhibuyaoyichuhuobianjizhexiewenjianchufeiniqueqiezhinizhengzaizuoshenmebingwanquanlijieqiyiyiji使shizheyangzuihaoshi使shiyongCHANGE MASTER TOyuju

原文
 
这篇论坛文章(赛迪网技术社区)主要介绍了MySQL数据库主从复制的相关概念及设置方法,详细内容请大家参考下文: MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环。当一个从服务器连接到主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知下一次更新。 为什么使用主从复制? 1、主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器作为备份。 2、通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。但是不要同时在主从服务器上进行更新,这样可能引起冲突。 3、使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新。 MySQL使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让主服务器发送二进制日志。主服务器创建一个线程将二进制日志中的内容发送到从服务器。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是SQL线程,从服务器使用此线程读取中继日志并执行日志中包含的更新。SHOW PROCESSLIST语句可以查询在主服务器上和从服务器上发生的关于复制的信息。 默认中继日志使用host_name-relay-bin.nnnnnn形式的文件名,其中host_name是从服务器主机名,nnnnnn是序列号。用连续序列号来创建连续中继日志文件,从000001开始。从服务器跟踪中继日志索引文件来识别目前正使用的中继日志。默认中继日志索引文件名为host_name-relay-bin.index。在默认情况,这些文件在从服务器的数据目录中被创建。中继日志与二进制日志的格式相同,并且可以用mysqlbinlog读取。当SQL线程执行完中继日志中的所有事件后,中继日志将会被自动删除。 从服务器在数据目录中另外创建两个状态文件--master.info和relay-log.info。状态文件保存在硬盘上,从服务器关闭时不会丢失。下次从服务器启动时,读取这些文件以确定它已经从主服务器读取了多少二进制日志,以及处理自己的中继日志的程度。 设置主从复制: 1、确保在主服务器和从服务器上安装的MySQL版本相同,并且最好是MySQL的最新稳定版本。 2、在主服务器上为复制设置一个连接账户。该账户必须授予REPLICATION SLAVE权限。如果账户仅用于复制(推荐这样做),则不需要再授予任何其它权限。 mysql> GRANT REPLICATION SLAVE ON *.* -> TO 'replication'@'%.yourdomain.com' IDENTIFIED BY 'slavepass'; 3、执行FLUSH TABLES WITH READ LOCK语句清空所有表和块写入语句: mysql> FLUSH TABLES WITH READ LOCK; 保持mysql客户端程序不要退出。开启另一个终端对主服务器数据目录做快照。 shell> cd /usr/local/mysql/ shell> tar -cvf /tmp/mysql-snapshot.tar ./data 如果从服务器的用户账户与主服务器的不同,你可能不想复制mysql数据库。在这种情况下,应从归档中排除该数据库。你也不需要在归档中包括任何日志文件或者master.info或relay-log.info文件。 当FLUSH TABLES WITH READ LOCK所置读锁定有效时(即mysql客户端程序不退出),读取主服务器上当前的二进制日志名和偏移量值: mysql > SHOW MASTER STATUS; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | mysql-bin.003 | 73 | test | manual,mysql | +---------------+----------+--------------+------------------+ File列显示日志名,而Position显示偏移量。在该例子中,二进制日志值为mysql-bin.003,偏移量为73。记录该值。以后设置从服务器时需要使用这些值。它们表示复制坐标,从服务器应从该点开始从主服务器上进行新的更新。 如果主服务器运行时没有启用--logs-bin,SHOW MASTER STATUS显示的日志名和位置值为空。在这种情况下,当以后指定从服务器的日志文件和位置时需要使用的值为空字符串('')和4. 取得快照并记录日志名和偏移量后,回到前一中端重新启用写活动: mysql> UNLOCK TABLES; 4、确保主服务器主机上my.cnf文件的[mysqld]部分包括一个log-bin选项。该部分还应有一个server-id=Master_id选项,其中master_id必须为1到232–1之间的一个正整数值。例如: [mysqld] log-bin server-id=1 如果没有提供那些选项,应添加它们并重启服务器。 5、停止从服务器上的mysqld服务并在其my.cnf文件中添加下面的行: [mysqld] server-id=2 slave_id值同Master_id值一样,必须为1到232–1之间的一个正整数值。并且,从服务器的ID必须与主服务器的ID不相同。 6、将数据备据目录中。确保对这些文件和目录的权限正确。服务器 MySQL运行的用户必须能够读写文件,如同在主服务器上一样。 Shell> chown -R mysql:mysql /usr/local/mysql/data 7、启动从服务器。在从服务器上执行下面的语句,用你的系统的实际值替换选项值: mysql> CHANGE MASTER TO -> MASTER_HOST='master_host_name', -> MASTER_USER='replication_user_name', -> MASTER_PASSWORD='replication_password', -> MASTER_LOG_FILE='recorded_log_file_name', -> MASTER_LOG_POS=recorded_log_position; 8、启动从服务器线程: mysql> START SLAVE; 执行这些程序后,从服务器应连接主服务器,并补充自从快照以来发生的任何更新。 9、如果出现复制错误,从服务器的错误日志(HOSTNAME.err)中也会出现错误消息。 10、从服务器复制时,会在其数据目录中发现文件master.info和HOSTNAME-relay-log.info。从服务器使用这两个文件跟踪已经处理了多少主服务器的二进制日志。不要移除或编辑这些文件,除非你确切知你正在做什么并完全理解其意义。即使这样,最好是使用CHANGE MASTER TO语句。
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有