http://blog.csdn.net/testcs_dn/article/details/51423861

为何使用主从架构?java

一、实现服务器负载均衡;mysql

二、经过复制实现数据的异地备份;sql

三、提升数据库系统的可用性;数据库

 

四、能够分库【垂直拆分】,分表【水平拆分】;服务器

主从配置的前提条件

一、MySQL版本一致;架构

你尚未安装MySQL?负载均衡

请参考:CentOS 6.5 下安装MySQL 5.7.12,使用官网下载的rpm安装包性能

二、MySQL中的数据一致;测试

不一致就把它们搞一致!spa

三、操做前中止一切更新操做(写入、更新、删除等);

配置master(主服务器)

 

[plain]  view plain  copy
 
  1. vi /etc/my.cnf  
  2. #[必须]启用二进制日志  
  3. log-bin=mysql-bin  
  4. #[必须]服务器惟一ID,默认是1,通常取IP最后一段  
  5. server-id=151  

配置slave(从服务器)

 

 

[plain]  view plain  copy
 
  1. vi /etc/my.cnf  
  2. #[可选]启用二进制日志  
  3. log-bin=mysql-bin  
  4. #[必须]服务器惟一ID,默认是1,通常取IP最后一段  
  5. server-id=152  

若是你的从服务器下面再挂从服务器,启用二进制日志就是必选的!

 

 

重启mysql服务

/etc/init.d/mysqld restart

 

重启的目的是使用刚才的配置生效,主从都须要重启;

注意:服务名是“mysqld”,不是某同窗文章中的“mysql”!

在主服务器上建立备份专用账户

 

[plain]  view plain  copy
 
  1. mysql -uroot -ppassword -e "GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'192.168.0.154' IDENTIFIED BY '123456';"  

注意:

 

一、这里是用GRANT建立用户并受权远程登陆权限,而不是使用“Create User”来建立;

二、命令是在Shell下执行,不是在“mysql”客户端中执行;

我是为了方便,直接把命令都写到一行里了,固然你也能够先用“mysql -uroot -ppassword”登陆后,

再执行“GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'192.168.0.154' IDENTIFIED BY '123456';”;

命令中的“password”是什么鬼?我怎么登陆不了!泥玛,我哪知道你的 root 用户密码是什么鬼!

查询master(主服务器)的状态

 

[plain]  view plain  copy
 
  1. mysql -uroot -ppassword -e "show master status;"  

 

这是在主服务器上执行的,看准了。

不解释了,看输出:

 

[plain]  view plain  copy
 
  1. mysql: [Warning] Using a password on the command line interface can be insecure.  
  2. +------------------+----------+--------------+------------------+-------------------+  
  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |  
  4. +------------------+----------+--------------+------------------+-------------------+  
  5. | mysql-bin.000001 |      154 |              |                  |                   |  
  6. +------------------+----------+--------------+------------------+-------------------+  

File列和Position列的值一下子咱们要用到。

 

 

配置Slave启动主从复制

[plain]  view plain  copy
 
  1. mysql -uroot -ppassword -e "change master to master_host='192.168.0.151',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154; start slave;"  
  2. //注意不要断开,154数字先后无单引号。  
  3. //启动从服务器复制功能  

master_host=主服务器IP
master_user=在主服务器上建立的备份用户名
master_password=备份用户密码
master_log_file=查询master(主服务器)的状态获得的File列的值
master_log_pos=Position列的值
start slave:启动从服务器复制功能

 

 

检查从服务器复制功能状态

[plain]  view plain  copy
 
  1. mysql -uroot -ppassword -e "show slave status\G;"  

输出以下:

 

 

[plain]  view plain  copy
 
  1. *************************** 1. row ***************************  
  2.                Slave_IO_State: Waiting for master to send event  
  3.                   Master_Host: 192.168.0.151  
  4.                   Master_User: backup  
  5.                   Master_Port: 3306  
  6.                 Connect_Retry: 60  
  7.               Master_Log_File: mysql-bin.000001  
  8.           Read_Master_Log_Pos: 154  
  9.                Relay_Log_File: vir2-relay-bin.000002  
  10.                 Relay_Log_Pos: 320  
  11.         Relay_Master_Log_File: mysql-bin.000001  
  12.              Slave_IO_Running: Yes  
  13.             Slave_SQL_Running: Yes  
  14.               Replicate_Do_DB:   
  15.           Replicate_Ignore_DB:   
  16.            Replicate_Do_Table:   
  17.        Replicate_Ignore_Table:   
  18.       Replicate_Wild_Do_Table:   
  19.   Replicate_Wild_Ignore_Table:   
  20.                    Last_Errno: 0  
  21.                    Last_Error:   
  22.                  Skip_Counter: 0  
  23.           Exec_Master_Log_Pos: 154  
  24.               Relay_Log_Space: 526  
  25.               Until_Condition: None  
  26.                Until_Log_File:   
  27.                 Until_Log_Pos: 0  
  28.            Master_SSL_Allowed: No  
  29.            Master_SSL_CA_File:   
  30.            Master_SSL_CA_Path:   
  31.               Master_SSL_Cert:   
  32.             Master_SSL_Cipher:   
  33.                Master_SSL_Key:   
  34.         Seconds_Behind_Master: 0  
  35. Master_SSL_Verify_Server_Cert: No  
  36.                 Last_IO_Errno: 0  
  37.                 Last_IO_Error:   
  38.                Last_SQL_Errno: 0  
  39.                Last_SQL_Error:   
  40.   Replicate_Ignore_Server_Ids:   
  41.              Master_Server_Id: 151  
  42.                   Master_UUID: 959de288-197f-11e6-9615-525400190a25  
  43.              Master_Info_File: /var/lib/mysql/master.info  
  44.                     SQL_Delay: 0  
  45.           SQL_Remaining_Delay: NULL  
  46.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates  
  47.            Master_Retry_Count: 86400  
  48.                   Master_Bind:   
  49.       Last_IO_Error_Timestamp:   
  50.      Last_SQL_Error_Timestamp:   
  51.                Master_SSL_Crl:   
  52.            Master_SSL_Crlpath:   
  53.            Retrieved_Gtid_Set:   
  54.             Executed_Gtid_Set:   
  55.                 Auto_Position: 0  
  56.          Replicate_Rewrite_DB:   
  57.                  Channel_Name:   
  58.            Master_TLS_Version:   

主要查看如下两项:

 

 

[plain]  view plain  copy
 
  1. Slave_IO_Running: Yes  
  2. lave_SQL_Running: Yes  

Slave_IO及Slave_SQL进程必须正常运行,即YES状态,不然都是错误的状态(如:其中一个NO均属错误)。
以上操做过程,主从服务器配置完成。

 

我在一次配置过程当中忘记在主服务器上建立备份用户,Slave_IO_Running 就一直处于“connect”状态。

主从服务器测试

在主服务器上执行如下操做:

你仍是登陆Mysql再操做吧,此次命令比较多,不组合了!

 

[plain]  view plain  copy
 
  1. mysql -uroot -ppassword  
  2.   
  3. create database db_test_slave;  
  4. use db_test_slave;  
  5. create table tb_test(id int(3), name varchar(50));  
  6. insert into tb_test values(1,'hello slave');  
  7. show databases;  

在从服务器上执行如下操做:

 

 

[plain]  view plain  copy
 
  1. mysql -uroot -ppassword -e "show databases; use db_test_slave; select * from tb_test;"  

查看输出:

 

 

[plain]  view plain  copy
 
  1. +--------------------+  
  2. | Database           |  
  3. +--------------------+  
  4. | information_schema |  
  5. | db_test_slave      |  
  6. | mysql              |  
  7. | performance_schema |  
  8. | sys                |  
  9. +--------------------+  
  10. +------+-------------+  
  11. | id   | name        |  
  12. +------+-------------+  
  13. |    1 | hello slave |  
  14. +------+-------------+  

看到在主服务器上建立的数据库、数据表、插入的数据记录了吗?

 

没有?本身找找缘由吧,我一次就成功了。

结束语

上面提到了“若是你的从服务器下面再挂从服务器,启用二进制日志就是必选的!”,主从只是一个相对概念,一台MySQL服务便可以是主,也能够是从。

一开始讲了主从复制的一些好处,可是单一的主从复制也有其不足:当更新操做增长到必定程度后,主服务器的任务会过度繁重,成为瓶颈,从而使系统性能大幅度降低。另外当主机出现故障时,整个系统都涉及更新的功能都不能正常使用,所以系统的可靠性依然不高。

 

参考

http://blog.csdn.net/xmz_java/article/details/54896955

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息