准备两台机器,分别安装mysql,版本最好是相同的
安装过程能够参考https://blog.51cto.com/13736286/2135537 mysql的安装
这里已经安装好了,一台hostname为master,ip为192.168.66.128 一台hostname为slave,ip为192.168.66.129
配置以前,保持两台mysql的数据是一致的,能够经过mysqldump把主上数据备份后,上传到从上,而后恢复html
一、在master上修改mysql配置文件/etc/my.cnf,设置server-id和打开log-bin二进制日志mysql
[root@master ~]# vi /etc/my.cnf #增长下面两行 server-id=1 #本机标识号,惟一,数字能够随意设置,和从上要不同 log_bin=master #打开log-bin,master为log-bin日志的前缀
二、修改完配置文件后,启动或者重启mysqld服务linux
[root@master ~]# /etc/init.d/mysqld restart
三、重启完成后在mysql数据目录下会生成相应的mater开头的二进制日志文件, 这些文件是实现主从的根本,
个人数据目录是在/var/lib/mysql下sql
[root@master ~]# ll /var/lib/mysql/ 总用量 28772 -rw-rw----. 1 mysql mysql 18874368 7月 16 06:42 ibdata1 -rw-rw----. 1 mysql mysql 5242880 7月 16 06:42 ib_logfile0 -rw-rw----. 1 mysql mysql 5242880 7月 16 06:42 ib_logfile1 -rw-rw---- 1 mysql mysql 106 7月 16 06:39 master.000001 -rw-rw---- 1 mysql mysql 16 7月 16 06:39 master.index drwx------. 2 mysql mysql 4096 5月 5 02:28 mysql srwxrwxrwx 1 mysql mysql 0 7月 16 06:39 mysql.sock drwx------. 2 mysql mysql 4096 5月 5 04:23 performance_schema drwx------ 2 mysql mysql 4096 5月 20 10:12 test
注:myster.000001就是二进制日志文件
四、登录mysql,建立用做同步数据的用户数据库
mysql> grant replication slave on *.* to 'repl'@'192.168.66.129' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
五、为了保持数据一致, 把表锁一下, 不继续写. 架构
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)
六、查看主的状态,并记录下log-bin文件的名字和偏移量ide
mysql> show master status; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | master.000001 | 336 | | | +---------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
注:master.000001为log-bin文件的名字,336为偏移量,指定从这个点开始同步测试
一、修改从的mysql配置文件/etc/my.cnf,只用设置server-id便可,不用打开log-bin.net
[root@slave ~]# vi /etc/my.cnf #增长一行 server-id=2 #这个数字能够随意设置,但要区别于主设置的id
二、修改完配置文件,重启mysql服务线程
[root@slave ~]# /etc/init.d/mysqld restart
三、登录从的mysql,中止slave,并更改主的信息,这一步很是关键
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host='192.168.66.128', master_user='repl', master_password='123456', master_log_file='master.000001', master_log_pos=336; Query OK, 0 rows affected (0.06 sec)
注意:
这一步很关键,这里的change master的信息就是主的ip 还有建立受权的用户和密码,还有master的 log-bin文件和偏移量
四、启动slave,并查看状态
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.66.128 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master.000001 Read_Master_Log_Pos: 336 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 249 Relay_Master_Log_File: master.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: 336 Relay_Log_Space: 405 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: 1 1 row in set (0.00 sec)
能够看到刚才配置的主的一些信息,关键的两个指标 Slave_IO_Running: Yes Slave_SQL_Running: Yes,看这两个状态是否为Yes,Yes表示正常,若是其中有一个为No表示不正常,若是有错误能够在 Last_Errno: 0 Last_Error: 两个指标中体现出
来
至此,从配置完成
下面这一步在主上执行
不要忘记,刚才主上为了防止写入数据锁表了,要解锁,回到主上,登录mysql,执行
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)
一、几个配置参数在主从my.cnf设置
(主和从设置一个便可)
可在mysql的配置文件my.cnf中增长如下参数,表示,要同步或忽略的库
主:可支持逗号分隔设置多个
binlog-do-db= //仅同步指定的库,多个建议写多行 binlog-ignore-db= //忽略指定库
从:
replicate_do_db= //仅同步指定的库 replicate_ignore_db= //忽略指定库 replicate_do_table= //仅同步指定的表(慎用) --假设表示test.xx 带结尾的也会忽略致使库不完整,so,建议用wild下面2个支持通配。 replicate_ignore_table= //忽略指定的表(慎用) replicate_wild_do_table= //同步指定的表支持通配(如test.%) replicate_wild_ignore_table= //忽略指定的表支持通配(如test.%)
二、测试主从是否同步
在主上建立一个数据库luo
mysql> create database luo; Query OK, 1 row affected (0.02 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | luo | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec)
在从上直接查看是否自动建立了数据库luo
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | luo | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec)
能够看到从上已经自动建立了数据库luo,说明数据是同步的
扩展的一些连接:
mysql主从
http://www.javashuo.com/article/p-ssmsbwqm-k.html
相关扩展
不停库不锁表在线主从配置
http://seanlook.com/2015/12/14/mysql-replicas/
主从不一样步
http://www.rfyy.net/archives/2309.html
http://www.javashuo.com/article/p-cthpurvb-bb.html
主主
关于 auto_increment http://www.javashuo.com/article/p-tamhibns-by.html
http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 实现读写分离
http://www.javashuo.com/article/p-gjdwpcia-k.html
mysql-proxy相似的产品有:
mycat 基于阿里的开源软件cobar,官网 www.mycat.io
https://my.oschina.net/ruoli/blog/1789370
mycat实现分库分表
http://www.javashuo.com/article/p-xbjkpuop-bb.html
atlas 出自于360,不维护不更新了 http://www.javashuo.com/article/p-sofkphwt-m.html
mysql环形主从
http://ask.apelearn.com/question/11437
mysql架构演变 http://www.aminglinux.com/bbs/thread-8025-1-1.html
MHA架构
http://www.javashuo.com/article/p-rrhwavha-k.html
比较复杂的mysql集群架构 http://ask.apelearn.com/question/17026