博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql主从复制
阅读量:4106 次
发布时间:2019-05-25

本文共 5172 字,大约阅读时间需要 17 分钟。

怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下:

tail -f  /var/log/mysqld.log

1、主从服务器分别作以下操作
  1.1、版本一致
  1.2、初始化表,并在后台启动mysql
  1.3、修改root的密码

2、修改主服务器master:
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[必须]启用二进制日志
       server-id=222      //[必须]服务器唯一ID,默认是1,一般取IP最后一段

3、修改从服务器slave:
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[不是必须]启用二进制日志
       server-id=226      //[必须]服务器唯一ID,默认是1,一般取IP最后一段

4、重启两台服务器的mysql
   /etc/init.d/mysql restart

5、在主服务器上建立帐户并授权slave:
   #/usr/local/mysql/bin/mysql -uroot -pmttang   
   mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; //一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。

6、登录主服务器的mysql,查询master的状态
   mysql>show master status;
   +------------------+----------+--------------+------------------+
   | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   +------------------+----------+--------------+------------------+
   | mysql-bin.000004 |      308 |              |                  |
   +------------------+----------+--------------+------------------+
   1 row in set (0.00 sec)
   注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化

7、配置从服务器Slave:
   mysql>change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456',
         master_log_file='mysql-bin.000004',master_log_pos=308;   //注意不要断开,308数字前后无单引号。

   Mysql>start slave;    //启动从服务器复制功能

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

   mysql> show slave status\G

   *************************** 1. row ***************************

              Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.2.222  //主服务器地址
              Master_User: mysync   //授权帐户名,尽量避免使用root
              Master_Port: 3306    //数据库端口,部分版本没有此行
              Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
              Read_Master_Log_Pos: 600     //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
              Relay_Log_File: ddte-relay-bin.000003
              Relay_Log_Pos: 251
              Relay_Master_Log_File: mysql-bin.000004
              Slave_IO_Running: Yes    //此状态必须YES
              Slave_SQL_Running: Yes     //此状态必须YES
                    ......

注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。

以上操作过程,主从服务器配置完成。
  
9、主从服务器测试:

主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:

  mysql> create database hi_db;
  Query OK, 1 row affected (0.00 sec)

  mysql> use hi_db;
  Database changed

  mysql>  create table hi_tb(id int(3),name char(10));
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> insert into hi_tb values(001,'bobu');
  Query OK, 1 row affected (0.00 sec)

  mysql> show databases;
   +--------------------+
   | Database           |
   +--------------------+
   | information_schema |
   | hi_db                |
   | mysql                |
   | test                 |
   +--------------------+
   4 rows in set (0.00 sec)

从服务器Mysql查询:

   mysql> show databases;

   +--------------------+
   | Database               |
   +--------------------+
   | information_schema |
   | hi_db                 |       //I'M here,大家看到了吧
   | mysql                 |
   | test          |

   +--------------------+
   4 rows in set (0.00 sec)

   mysql> use hi_db
   Database changed
   mysql> select * from hi_tb;           //查看主服务器上新增的具体数据
   +------+------+
   | id   | name |
   +------+------+
   |    1 | bobu |
   +------+------+
   1 row in set (0.00 sec)
 

10、完成:

    编写一shell脚本,用nagios监控slave的两个yes(Slave_IO及Slave_SQL进程),如发现只有一个或零个yes,就表明主从有问题了,发短信警报吧。

从/etc/my.cnf:

[mysqld]

datadir=/home/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
log-bin=mysql-bin
log-slave-updates
server-id=2
master-host=10.0.30.122
master-user=root
master-password=1
master-port=3306
auto_increment_increment = 2
auto_increment_offset = 1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#bind-address=0.0.0.0
query_cache_size = 64M
#max_connections = 1024
max_connections = 10000
#wait_timeout = 10
max_connect_errors = 100
table_open_cache = 5120
thread_cache_size = 120
key_buffer_size = 384M
tmp_table_size = 64M
max_heap_table_size = 64M
innodb_buffer_pool_size=5M
innodb_file_per_table=1
character_set_server = utf8
lower_case_table_names=1
#skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

主/etc/my.cnf:

[mysqld]

datadir=/home/mysql
#socket=/home/mysql/mysql.sock
socket=/var/lib/mysql/mysql.sock
user=mysql
log-bin=mysql-bin
binlog-do-db=db5
binlog-ignore-db=information_schema
binlog-ignore-db=acdb
binlog-ignore-db=audb
binlog-ignore-db=db1
binlog-ignore-db=db3
binlog-ignore-db=fpdb
binlog-ignore-db=infosec_ca
binlog-ignore-db=infosec_ra
binlog-ignore-db=jrgazx30
binlog-ignore-db=mbdb
binlog-ignore-db=mldb
binlog-ignore-db=mogilefs
binlog-ignore-db=mysql
binlog-ignore-db=pldb
binlog-ignore-db=ptdb
binlog-ignore-db=test
binlog-ignore-db=trdb
binlog-ignore-db=vcdb
binlog-ignore-db=wndb
log-slave-updates
server-id=1
auto_increment_increment = 2
auto_increment_offset = 2
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#bind-address=0.0.0.0
query_cache_size = 0
query_cache_type = 0
#max_connections = 1024
max_connections = 1024
#wait_timeout = 10
max_connect_errors = 100
table_open_cache = 5120
thread_cache_size = 120
key_buffer_size = 384M
tmp_table_size = 64M
max_heap_table_size = 64M
innodb_file_per_table=1
character_set_server = utf8
lower_case_table_names=1
#log=/var/lib/mysql/sql_row.log
#innodb_buffer_pool_size=1500M
innodb_buffer_pool_size=1500M
#innodb_log_buffer_size=256K
#innodb_additional_mem_pool_size=2M
#skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
#log-error=/var/log/chenyi.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
#socket=/home/mysql/mysql.sock
socket=/var/lib/mysql/mysql.sock

转载地址:http://tfnsi.baihongyu.com/

你可能感兴趣的文章
Sublime Text 3 绝对神器
查看>>
Git push与pull的默认行为
查看>>
自动化发布-GitLab WEB Hooks 配置
查看>>
Android性能优化实战前篇
查看>>
微信网页第三方登录原理
查看>>
使用Sublime Text3+Ctags+Cscope替代Source Insight
查看>>
git reset revert 回退回滚取消提交返回上一版本
查看>>
Android Studio插件整理
查看>>
微信开放平台开发——网页微信扫码登录(OAuth2.0)
查看>>
PHP微信第三方扫码登录技术问题
查看>>
C++11带来的优雅语法
查看>>
CentOS7 安装 KVM
查看>>
CentOS 7 下配置KVM
查看>>
CentOS 7 清除旧内核
查看>>
CentOS 7 使用阿里云的yum源
查看>>
php-fpm的配置和优化
查看>>
php-fpm 与 Nginx优化总结
查看>>
实时查看及监控PHP-FPM的运行状态
查看>>
阿里云CentOS7挂载SSD云盘的方法
查看>>
CentOS 7 NFS服务器和客户端设置
查看>>