设为首页 收藏本站
查看: 447|回复: 0

[经验分享] Ubuntu下Nginx做负载实现高性能WEB服务器5

[复制链接]

尚未签到

发表于 2018-5-7 08:07:05 | 显示全部楼层 |阅读模式
  一、MySQL复制概述
  MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新、删除等等)。因此,要进行复制,必须在主服务器上启用二进制日志。每个从服务器从主服务器接收主服务器上已经记录到其二进制日志的保存的更新。当一个从服务器连接主服务器时,它通知主服务器定位到从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,并在本机上执行相同的更新。然后封锁并等待主服务器通知新的更新。从服务器执行备份不会干扰主服务器,在备份过程中主服务器可以继续处理更新。
  二、复制实现细节
  MySQL使用3个线程来执行复制功能,其中两个线程(Sql线程和IO线程)在从服务器,另外一个线程(IO线程)在主服务器。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让它发送记录在其二进制日志中的语句。主服务器创建一个线程将二进制日志中的内容发送到从服务器。该线程可以即为主服务器上SHOW PROCESSLIST的输出中的Binlog Dump线程。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是SQL线程,由从服务器创建,用于读取中继日志并执行日志中包含的更新。在从服务器上,读取和执行更新语句被分成两个独立的任务。当从服务器启动时,其I/O线程可以很快地从主服务器索取所有二进制日志内容,即使SQL线程执行更新的远远滞后。
  三、MySQL建立主主服务器配置方法
  A、环境描述
  服务器A(主) 192.168.1.9
  服务器B(从) 192.168.1.10
  Mysql版本:5.1.61
  System OS:ubuntu 10.10 X86
  主主需同步的数据库内容保持一致。
  B、主主配置过程
  创建同步用户
  在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。
  这里服务器A和服务器B互为主从,所以都要分别建立一个同步用户。
  服务器A和B:


  • grant replication slave on *.* to 'replication'@'192.168.1.%' identified by 'mysqlsync';  
  • flush privileges;  

  修改mysql配置文件:vim /etc/mysql/my.cnf
  服务器A:


  • #mysql replication  
  • server-id = 1
  • log_bin = /var/log/mysql/mysql-bin.log  
  • master-host = 192.168.1.10  
  • master-user = replication
  • master-password = mysqlsync
  • master-port = 3306
  • master-connect-retry = 60
  • binlog-do-db = movie
  • binlog-ignore-db = mysql
  • binlog-ignore-db = information_schema
  • binlog-ignore-db = phpmyadmin
  • replicate-do-db = movie
  • replicate-ignore-db = mysql,information_schema

  服务器B:


  • #mysql replication  
  • server-id = 2
  • log_bin = /var/log/mysql/mysql-bin.log  
  • master-host = 192.168.1.9  
  • master-user = replication
  • master-password = mysqlsync
  • master-port = 3306
  • master-connect-retry = 60
  • binlog-do-db = movie
  • binlog-ignore-db = mysql
  • binlog-ignore-db = information_schema
  • binlog-ignore-db = phpmyadmin
  • replicate-do-db = movie
  • replicate-ignore-db = mysql,information_schema

  分别在A和B上重启mysql


  • /etc/init.d/mysql restart

  分别在服务器A、B上查看做为主服务器状态
  A服务器:


  • mysql>flush tables with read lock;  
  • mysql> show master status\G  
  • *************************** 1. row ***************************  
  •             File: mysql-bin.000003  
  •         Position: 106  
  •     Binlog_Do_DB: movie  
  • Binlog_Ignore_DB: mysql,information_schema,phpmyadmin  
  • 1 row in set (0.00 sec)

  B服务器;


  • mysql>flush tables with read lock;  
  • mysql> show master status\G  
  • *************************** 1. row ***************************  
  •             File: mysql-bin.000002  
  •         Position: 106  
  •     Binlog_Do_DB: movie  
  • Binlog_Ignore_DB: mysql,information_schema,phpmyadmin  
  • 1 row in set (0.00 sec)

  注:这里锁表的目的是为了生产环境中不让进新的数据,好让从服务器定位同步位置。初次同步完成后,记得解锁。
  分别在服务器A、B上用change master语句指定同步位置
  A服务器


  • mysql>change master to master_host='192.168.1.10', master_user='replication', master_password='mysqlsync', master_log_file='binlog.000002', master_log_pos=106;  

  B服务器


  • mysql>change master to master_host='192.168.1.9', master_user='replication', master_password='mysqlsync', master_log_file='binlog.000003', master_log_pos=106;  

  注:master_log_file,master_log_pos由上面主服务器查出的状态值中确定。master_log_file对应File,master_log_pos对应Position。
  mysql 5.x以上版本已经不支持在配置文件中指定主服务器相关选项。
  分别在服务器A、B上启动从服务器线程


  • mysql>start slave;  

  分别在服务器A、B上查看作为从服务器的状态


  • mysql> show slave status\G  
  • *************************** 1. row ***************************  
  •                Slave_IO_State: Waiting for master to send event  
  •                   Master_Host: 192.168.1.10  
  •                   Master_User: replication  
  •                   Master_Port: 3306  
  •                 Connect_Retry: 60  
  •               Master_Log_File: mysql-bin.000004  
  •           Read_Master_Log_Pos: 106  
  •                Relay_Log_File: ubuntu2-relay-bin.000005  
  •                 Relay_Log_Pos: 251  
  •         Relay_Master_Log_File: mysql-bin.000004  
  •              Slave_IO_Running: Yes  
  •             Slave_SQL_Running: Yes  
  •               Replicate_Do_DB: movie  
  •           Replicate_Ignore_DB: mysql,information_schema  
  •            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: 553  
  •               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.00 sec)  

  • mysql>  

  只要包含如下两项就表明正常运行了。
  Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  四:测试主主同步情况
  在A服务器上插入一条数据。


  • root@ubuntu3:~# mysql -u root -p  
  • Enter password:   
  • mysql> use movie  
  • Reading table information for completion of table and column names  
  • You can turn off this feature to get a quicker startup with -A  

  • Database changed  
  • mysql> show tables;  
  • +----------------------+  
  • | Tables_in_movie      |  
  • +----------------------+  
  • | wind_account_log     |  
  • | wind_ad              |  
  • | wind_ad_position     |  
  • | wind_admin           |  
  • | wind_admin_action    |  
  • | wind_admin_log       |  
  • | wind_admin_message   |  
  • | wind_adminutil       |  
  • | wind_adsense         |  
  • | wind_article         |  
  • | wind_article_cat     |  
  • | wind_card            |  
  • | wind_card_log        |  
  • | wind_category        |  
  • | wind_co_html         |  
  • | wind_co_listen       |  
  • | wind_co_media        |  
  • | wind_co_note         |  
  • | wind_comment         |  
  • | wind_cpsession       |  
  • | wind_datastore       |  
  • | wind_feedback        |  
  • | wind_friend_link     |  
  • | wind_humanverify     |  
  • | wind_keywords        |  
  • | wind_mailqueue       |  
  • | wind_nav             |  
  • | wind_netbar          |  
  • | wind_order_info      |  
  • | wind_pay_log         |  
  • | wind_payment         |  
  • | wind_play_log        |  
  • | wind_player          |  
  • | wind_searchcore_text |  
  • | wind_searchengine    |  
  • | wind_server          |  
  • | wind_session         |  
  • | wind_setting         |  
  • | wind_show            |  
  • | wind_show_article    |  
  • | wind_show_cat        |  
  • | wind_stats           |  
  • | wind_subject         |  
  • | wind_tag             |  
  • | wind_template        |  
  • | wind_template_mail   |  
  • | wind_user_account    |  
  • | wind_user_rank       |  
  • | wind_users           |  
  • | wind_vote            |  
  • | wind_vote_log        |  
  • | wind_vote_option     |  
  • +----------------------+  
  • 52 rows in set (0.01 sec)  
  • mysql> desc wind_admin;  
  • +-------------+---------------------+------+-----+---------+----------------+  
  • | Field       | Type                | Null | Key | Default | Extra          |  
  • +-------------+---------------------+------+-----+---------+----------------+  
  • | user_id     | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |  
  • | user_name   | varchar(60)         | NO   |     | NULL    |                |  
  • | email       | varchar(60)         | NO   |     | NULL    |                |  
  • | password    | varchar(32)         | NO   |     | NULL    |                |  
  • | join_time   | int(10) unsigned    | NO   |     | 0       |                |  
  • | last_time   | int(10) unsigned    | NO   |     | 0       |                |  
  • | last_ip     | varchar(15)         | NO   |     | NULL    |                |  
  • | action_list | text                | NO   |     | NULL    |                |  
  • | nav_list    | text                | NO   |     | NULL    |                |  
  • | lang_type   | varchar(50)         | NO   |     | NULL    |                |  
  • | todolist    | longtext            | NO   |     | NULL    |                |  
  • +-------------+---------------------+------+-----+---------+----------------+  
  • 11 rows in set (0.00 sec)  
  • mysql>insert into wind_admin values(NULL,'coldwind','123@456.com','coldwind','0','0','0.0.0.0','0','0','0','0');

  在服务器B中查询,看到之前服务器A新增的数据就成功了。
  B服务器上


  • root@ubuntu3:~# mysql -u root -p  Enter password:   
  • mysql> use movie  
  • Reading table information for completion of table and column names  You can turn off this feature to get a quicker startup with -A   
  • Database changed   
  • mysql> select * from wind_admin where user_name='coldwind';  
  • +---------+-----------+-------------+----------+-----------+-----------+---------+-------------+----------+-----------+----------+  
  • | user_id | user_name | email       | password | join_time | last_time | last_ip | action_list | nav_list | lang_type | todolist |  
  • +---------+-----------+-------------+----------+-----------+-----------+---------+-------------+----------+-----------+----------+  
  • |       2 | coldwind  | 123@456.com | coldwind |         0 |         0 | 0.0.0.0 | 0           | 0        | 0         | 0        |  
  • +---------+-----------+-------------+----------+-----------+-----------+---------+-------------+----------+-----------+----------+  
  • 1 row in set (0.00 sec)  

  • mysql>  

  相同,在B服务器上插入一条数据,在A服务器上能够查询到也就成功了。
  B服务器上:


  • mysql>insert into wind_admin values(NULL,'coldwind1','111@456.com','coldwind1','0','0','0.0.0.0','0','0','0','0');

  A服务器上:


  • mysql> select * from wind_admin where user_name='coldwind1';  
  • +---------+-----------+-------------+----------+-----------+-----------+---------+-------------+----------+-----------+----------+  
  • | user_id | user_name | email       | password | join_time | last_time | last_ip | action_list | nav_list | lang_type | todolist |  
  • +---------+-----------+-------------+----------+-----------+-----------+---------+-------------+----------+-----------+----------+  
  • |       3 | coldwind1  | 111@456.com | coldwind1 |         0 |         0 | 0.0.0.0 | 0           | 0        | 0         | 0        |  
  • +---------+-----------+-------------+----------+-----------+-----------+---------+-------------+----------+-----------+----------+  
  • 1 row in set (0.00 sec)  

  • mysql>  

  至此就配置成功了。
  五、配置参数说明
  server-id
  ID值唯一的标识了复制群集中的主从服务器,因此它们必须各不相同。master_id必须为1到232–1之间的一个正整数值,slave_id值必须为2到232–1之间的一个正整数值。
  log-bin
  
  表示打开binlog,打开该选项才可以通过I/O写到Slave的relay-log,也是可以进行replication的前提;
  binlog-do-db
  
  表示需要记录进制日志的数据库。如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项
  binlog-ignore-db
  表示不需要记录二进制日志的数据库。如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项
  replicate-do-db
  表示需要同步的数据库,如果有多个数据库可用逗号分隔,或者使用多个replicate-do-db选项
  replicate-ignore-db=mysql
  表示不需要同步的数据库,如果有多个数据库可用逗号分隔,或者使用多个replicate-ignore-db=mysql选项
  log-slave-updates
  配置从库上的更新操作是否写入二进制文件,如果这台从库,还要做其他从库的主库,那么就需要打这个参数,以便从库的从库能够进行日志同步
  slave-skip-errors
  在复制过程,由于各种原因导致binlog中的sql出错,默认情况下,从库会停止复制,要用户介入。可以设置Slave-skip-errors来定义错误号,如果复制过程中遇到的错误号是定义的错误号,便可以跳过。如果从库是用来做备份,设置这个参数会存在数据不一致,不要使用。如果是分担主库的查询压力,可以考虑。
  sync_binlog=1 or N
  
  sync_binlog的默认值是0,这种模式下,MySQL不会同步到磁盘中去。这样的话,MySQL依赖操作系统来刷新二进制日志binary log,就像操作系统刷其他文件的机制一样。因此如果操作系统或机器(不仅仅是MySQL服务器)崩溃,有可能binlog中最后的语句丢失了。要想防止这种情况,你可以使用sync_binlog全局变量,使binlog在每N次binlog写入后与硬盘同步。当sync_binlog变量设置为1是最安全的,因为在crash崩溃的情况下,你的二进制日志binary log只有可能丢失最多一个语句或者一个事务。但是,这也是最慢的一种方式(除非磁盘有使用带蓄电池后备电源的缓存cache,使得同步到磁盘的操作非常快)。
  即使sync_binlog设置为1,出现崩溃时,也有可能表内容和binlog内容之间存在不一致性。如果使用InnoDB表,MySQL服务器处理COMMIT语句,它将整个事务写入binlog并将事务提交到InnoDB中。如果在两次操作之间出现崩溃,重启时,事务被InnoDB回滚,但仍然存在binlog中。可以用–innodb-safe-binlog选项来增加InnoDB表内容和binlog之间的一致性。(注释:在MySQL 5.1中不需要–innodb-safe-binlog;由于引入了XA事务支持,该选项作废了),该选项可以提供更大程度的安全,使每个事务的 binlog(sync_binlog =1)和(默认情况为真)InnoDB日志与硬盘同步,该选项的效果是崩溃后重启时,在滚回事务后,MySQL服务器从binlog剪切回滚的 InnoDB事务。这样可以确保binlog反馈InnoDB表的确切数据等,并使从服务器保持与主服务器保持同步(不接收回滚的语句)。
  auto_increment_offset和auto_increment_increment
  auto_increment_increment和auto_increment_offset用于主-主服务器(master-to-master)复制,并可以用来控制AUTO_INCREMENT列的操作。两个变量均可以设置为全局或局部变量,并且假定每个值都可以为1到65,535之间的整数值。将其中一个变量设置为0会使该变量为1。
  这两个变量影响AUTO_INCREMENT列的方式:auto_increment_increment控制列中的值的增量值,auto_increment_offset确定AUTO_INCREMENT列值的起点。
  如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值被忽略。例如:表内已有一些数据,就会用现在已有的最大的自增值做为初始值。
  六、最后在举一个常会出现的错误:
  MySQL同步故障:" Slave_SQL_Running:No" 解决办法
mysql> show slave status\G
.......
Relay_Log_File: localhost-relay-bin.000535
Relay_Log_Pos: 21795072
Relay_Master_Log_File: localhost-bin.000094
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
解决办法一、
Slave_SQL_Running: No
1.程序可能在slave 上进行了写操作
2.也可能是slave 机器重起后,事务回滚造成的.
一般是事务回滚造成的:
解决办法:
mysql> slave stop;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;
解决办法二、
首先停掉Slave 服务:slave stop
到主服务器上查看主机状态:
记录File 和Position 对应的值
进入master
mysql> show master status;
+----------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| localhost-bin.000094 | 33622483 | | |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
然后到slave 服务器上执行手动同步:
mysql> change master to
  > master_host='master_ip',
> master_user='user',
> master_password='pwd',
> master_port=3306,
> master_log_file=localhost-bin.000094',
> master_log_pos=33622483;
1 row in set (0.00 sec)
mysql> slave start;
1 row in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
........
Master_Log_File: localhost-bin.000094
Read_Master_Log_Pos: 33768775
Relay_Log_File: localhost-relay-bin.000537
Relay_Log_Pos: 1094034
Relay_Master_Log_File: localhost-bin.000094
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
手动同步需要停止master 的写操作!
  参考文档:
  http://www.mike.org.cn/articles/mysql-master-slave-sync-conf-detail/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-456779-1-1.html 上篇帖子: Ubuntu用户磁盘配额设置 quota 下篇帖子: ubuntu11.10的root密码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表