您现在的位置是:首页 >MySQL网站首页MySQL

MySQL主从配置完整步骤资料

简介在当今更为复杂的网络世界,Mysql的优化越来越重要,越来越突出了Mysql的核心价值和技术深度。做了这行,就肯定会带着一腔热血上阵。相信掌握了各种优化技术,离大玩家也不远了。

试验环境: 主服务器IP:192.168.17.167 从服务器IP:192.168.17.66 配置: 一、主库 1.1、创建一个复制用户,具有replication slave 权限。 1.mysql>grant all on . to ‘jack@’192.168.203.55′ identified by ‘123;  1.2、编辑my.cnf文件 vi /etc/my.cnf 添加 server-id=88   # 标记不同数据库,不能与其他数据库一样 并开启log-bin二进制日志文件 log-bin=mysql-bin 注:需要把默认的server-id=1去掉 关闭mysql [root@cent]  /usr/local/mysql/bin/mysqladmin -uroot -p shutdown
1.3、启动mysql数据库 1./usr/local/mysql/bin/mysqldsafe –user=mysql &  1.4、得到binlog日志文件名和偏移量 1.mysql>show master status;   2.+——————+———-+————–+——————+   3.| File | Position | BinlogDoDB | BinlogIgnore_DB |   4.+——————+———-+————–+——————+   5.| mysql-bin.0000010 | 106| | |   6.+——————+———-+————–+——————+ 

二、从库 2.1、编辑my.cnf文件 vi /etc/my.cnf 添加 server-id=168 log-bin=mysql-bin   #建议开启 注:需要把默认的server-id=1去掉 关闭mysql /usr/local/mysql/bin/mysqladmin -uroot -p shutdown 2.2、启动从数据库 1./usr/local/mysql/bin/mysqld_safe –user=mysql & 

2.3清空所有bin-log mysql>reset master;

停止 mysql>stop slave;//停止从库 2.4、对从数据库进行相应设置 1.mysql> change master to   2.-> masterhost=’192.168.203.88′, 3.-> masteruser=’jack’,   4.-> masterpassword=’123’,   5.-> masterlogfile=’mysql-bin.0000010′,   # 从哪个bin-log开始同步   在主库中show master status 6.-> masterlogpos=106;  2.5、启动从服务器slave线程 1.mysql>start slave;  2.6、查看slave线程状态 1.mysql>show slave status;   2.* 1. row *   3.SlaveIOState: Waiting for master to send event   4.MasterHost: 192.168.203.149   5.MasterUser: repl   6.MasterPort: 3306   7.ConnectRetry: 60   8.MasterLogFile: mysql-bin.0000010   9.ReadMasterLogPos: 106   10.RelayLogFile: centos-relay-bin.000002   11.RelayLogPos: 529   12.RelayMasterLogFile: mysql-bin.0000010   13.SlaveIORunning: Yes   14.SlaveSQLRunning: Yes   15.ReplicateDoDB:   16.ReplicateIgnoreDB:   17.ReplicateDoTable:   18.ReplicateIgnoreTable:   19.ReplicateWildDoTable:   20.ReplicateWildIgnoreTable:   21.LastErrno: 0   22.LastError:   23.SkipCounter: 0   24.ExecMasterLogPos: 106   25.RelayLogSpace: 830   26.UntilCondition: None   27.UntilLogFile:   28.UntilLogPos: 0   29.MasterSSLAllowed: No   30.MasterSSLCAFile:   31.MasterSSLCAPath:   32.MasterSSLCert:   33.MasterSSLCipher:   34.MasterSSLKey:   35.SecondsBehindMaster: 0   36.MasterSSLVerifyServerCert: No   37.LastIOErrno: 0   38.LastIOError:   39.LastSQLErrno: 0   40.LastSQLError:   41.1 row in set (0.00 sec)  验证是否配置正确 在从服务器上执行 1.show slave status\G;   2.Waiting for master to send event   3.SlaveIORunning: Yes   4.SlaveSQLRunning: Yes  如以上二行同时为Yes 说明配置成功 测试: 1、在主服务器test数据库中创建user表 1.mysql>use test;   2.mysql>create table user(id int);  2、在从服务器中查看user表 1.mysql>use test;   2.mysql> show tables like ‘user’;   3.+———————-+   4.| Tablesintest(user) |   5.+———————-+   6.| user |   7.+———————-+   8.1 row in set (0.00 sec)  说明主从数据同步成功。 常见问题归纳: 1.在从数据库中查看slave状态时出现 The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the –replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it) 说明方从服务器里my.cnf中的server-id有相同的。 解决办法: 修改my.cnf里的server-id,并重启数据库服务。my.cnf文件默认有server-id=1 其它说明 主服务器my.cnf #binlog-do-db=需要备份的数据库名,可写多行 #binlog-ignore-db=不需要备份的数据库名,可写多行 从服务器my.cnf # replicate-do-db=test 需要备份的数据库名 # replicate-ignore-db=mysql 忽略的数据库 # master-connect-retry=60 如果从服务器发现主服务器断掉,重新连接的时间差(秒) 以下设置也可直接修改my.cnf配置文件 1.log-bin=mysql-bin   2.master-host=192.168.203.149   3.master-user=repl  4.master-password=repl  5.master-port=3306  主从服务器同步维护 由于各种原因,导致主从数据不一致,在负载低的时候,进行手动同步. 在主服务器上执行 1.mysql>flush tables with read lock;   2.Query OK,rows affected (0.01 sec)   3.mysql>show master status;   4.+——————+———-+————–+——————+   5.| File | Position | BinlogDoDB | BinlogIgnoreDB |   6.+——————+———-+————–+——————+   7.| mysql-bin.0000011 | 260| | |   8.+——————+———-+————–+——————+  在从服务器上执行 先得到当前主服务器的二进制文件名和偏移量,执行命令使从服务器与主服务器同步 1.mysql>select masterposwait(‘mysql-bin.0000011′,’260′);   2.+————————————————–+   3.| masterposwait(‘mysql-bin.0000011′,’260′) |   4.+————————————————–+   5.| 0 |   6.+————————————————–+   7.1 row in set (0.01 sec)  同步完成后,在主服务器上执行解锁 1.mysql>unlock tables;  切换主从服务器 当主服务器出现故障时,可将从服务器当主服务器来使用.步骤如下: 1、保证所有从数据库都已经执行了relay log中的全部更新,在从服务器中执行 stop slave iothread,用show processlist检查,查看状态是否是Has read all relay log,表示更新完成. 1.mysql>stop slave iothread;   2.Query OK,0 affected (0.00 sec)   3.mysql>show processlist\G;   4.* 2. row *   5.Id: 2   6.User: system user   7.Host:   8.db: NULL   9.Command: Connect   10.Time: 4757   11.State: Has read all relay log; waiting for the slave I/O thread to update it   12.Info: NULL  2、在从服务器上执行stop slave,reset master命令,重置成主数据库 1.mysql>stop slave;   2.Query OK,0 affected (0.00 sec)   3.mysql>reset master;   4.Query OK,0 affected (0.00 sec)  3、删除新的主服务器数据库目录中的master.info和relay-log.info文件,否则下次重启时还会按照从服务器来启动.

上一篇:数据库优化

下一篇:JS获取本地时间

文章评论

Top