一.主从的作用:
1.可以当做一种备份方式
2.用来实现读写分离,缓解一个数据库的压力
二.MySQL主从备份原理
master 上提供binlog ,
slave 通过 I/O线程从 master拿取 binlog,并复制到slave的中继日志中
slave 通过 SQL线程从 slave的中继日志中读取binlog ,然后解析到slave中
部署主从环境:主服务器:192.168.1.110(编译好的MySQL5.1版本的数据库)
从服务器:192.168.1.120(编译好的MySQL5.1版本的数据库)
(温馨提示:主和从数据库版本必须是一样。或者主库的数据库版本必须比从库高,不然会导致很多故障的发生。)
三:生产环境应用MySQL主从同步场景:
1. 一般用主库做为提供业务用户写操作(比如:在互联网上写一条微博,这时候就会写到mysql数据库的主库中)
2. 一般用从库做为提供业务用户读操作(比如:在互联网上,我想看一条微博,这时候里面提供数据就是MySQL数据库的从库中。)
(1)在主服务器(192.168.1.110)上操作。
[root@Andy ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:5E:6F:A7
inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5e:6fa7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:141354 errors:0 dropped:0 overruns:0 frame:0
TX packets:140807 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:142083379 (135.5 MiB) TX bytes:17815696 (16.9 MiB)
Interrupt:193 Base address:0x2000
[root@Andy ~]# vi /etc/my.cnf
[mysqld]在mysqld下添加以上两行。
server-id = 1
log-bin= Andy -bin
[root@Andy ~]# /etc/init.d/mysqld restart
Shutting down MySQL[ OK ]
Starting MySQL.[ OK ]
[root@Andy ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.44 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| Andy-bin.000001 | 106 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
请记住:File里的Andy-bin.000001 和 Position 106 。
Mysql> grant replication slave on *.* to 'python'@'192.168.1.%' identified by '123456';
mysql> quit
Bye
在从服务器(192.168.1.120)上操作:
[root@Andy ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:2B:8E:D2
inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2b:8ed2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:180290 errors:0 dropped:0 overruns:0 frame:0
TX packets:146169 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:223411069 (213.0 MiB) TX bytes:15504427 (14.7 MiB)
Interrupt:193 Base address:0x2000
[root@Andy ~]# vi /etc/my.cnf
把server-id = 1 改为:server-id = 2
然后重启Mysql服务:[root@Andy ~]# /etc/init.d/mysqld restart
Shutting down MySQL........[ OK ]
Starting MySQL.[ OK ]
[root@Andy ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.44 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.110', MASTER_PORT=3306, MASTER_USER='python', MASTER_PASSWORD='123456', MASTER_LOG_FILE=' Andy-bin .000001', MASTER_LOG_POS=106;
mysql> start slave;
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: 192.168.1.110
Master_User: python
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Andy-bin.000001
Read_Master_Log_Pos: 106
Relay_Log_File: Andy-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: Andy-bin.000001
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: 106
Relay_Log_Space: 410
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:
1 row in set (0.01 sec)
如果: Slave_IO_Running: Yes (主从I/O正确)
Slave_SQL_Running: Yes(主从进程正确)

最新文章

  1. python有超时的windows系统时间设置代码
  2. gulp基本介绍
  3. java中用spring实现数组类型输出
  4. C# Winform打包部署时添加注册表信息实现开机启动
  5. WPF学习03:Element Binding
  6. oracle数据库没有监听服务与实例服务(OracleServicesXX)的解决方法
  7. ASP.NET Boilerplate Castle容器无缝添加日志功能
  8. LF will be replaced by CRLF问题解决方法
  9. 5.1.1 读取Redis 数据
  10. java读取远程url图片,得到宽高
  11. PAT (Advanced Level) 1026. Table Tennis (30)
  12. hdu--4148--Length of S(n)
  13. 【搬运】 Page Object 官方文档 (新增了Widget特性)
  14. 10个经典的Java面试题集合
  15. mysql 开发进阶篇系列 10 锁问题 (相同索引键值或同一行或间隙锁的冲突)
  16. make[1]: *** [storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/all] 错误 2 解决方法
  17. End of script output before headers错误解决方法
  18. 使用ipns 解决ipfs 内容更新的问题
  19. static为什么一般与final一起用?
  20. eigen quick reference

热门文章

  1. nslookup、arp、netstat、traceroute
  2. A Tour of Go Structs
  3. hdoj 2546 饭卡
  4. Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
  5. java中服务器启动时,执行定时任务
  6. 用Ajax调用web api,解决URL太长的问题;
  7. linux中配置maven环境
  8. 在大型软件中用Word做报表: 书签的应用
  9. android 文件读取(assets、raw)
  10. js返回上一页方法区别