mysql主从实验

来源:互联网 发布:apache日志 json 编辑:程序博客网 时间:2024/06/10 02:25

1环境
两台db服务器centos6.5
iptables 和selinux关闭

2开始部署
主mysql(140.4);从mysql(140.19)

查看mysql是否启动:
/etc/init.d/mysql status

主db服务器mysql.cnf配置(极简化配置):
my.cnf:

[mysqld]server-id=1log-bin=mysql-binbinlog-ignore-db=mysql,test  # 忽略这两个数据库binlog-do-db=apps  #指定同步库sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

重启mysql使它生效 /etc/init.d/mysql

赋予cuiyf权限仅能访问140.19db服务器的cui数据库:
grant ALL PRIVILEGES on . to weren@”192.168.140.22” Identified by “123shinyv!@#”;

查看master状态:

mysql> show  master  status;+------------------+----------+--------------+------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000002 |     1269 | apps         | mysql,test       |                   |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)

主服务器设置完成;

从服务器mysql.conf(极简化):

[mysqld]server-id = 2replicate-do-db = cui  #从库指定同步的数据库   sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

从库指定主库:

mysql> change master to master_host='192.168.140.21',master_user='weren',master_password='123shinyv!@#',master_log_file='mysql-bin.000002', master_log_pos=1269;Query OK, 0 rows affected, 2 warnings (0.08 sec)

同步启动:

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.140.21                  Master_User: weren                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000002          Read_Master_Log_Pos: 1269               Relay_Log_File: cyf-relay-bin.000002                Relay_Log_Pos: 283        Relay_Master_Log_File: mysql-bin.000002             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB: apps          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: 1269              Relay_Log_Space: 454              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: 0Master_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                  Master_UUID: f339bf21-f9e7-11e5-ab1e-000c29ea826b             Master_Info_File: /var/lib/mysql/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set:                 Auto_Position: 01 row in set (0.00 sec)

备注:
关于主从其他的问题(转自http://blog.51yip.com/mysql/1716.html):

1,在从服务器上使用show slave status\GSlave_IO_Running为No,则说明IO_THREAD没有启动,请执行mysql> start slave io_threadSlave_SQL_Running为No,则复制出错,查看Last_error字段排除错误后执行mysql> start slave sql_threadSlave_IO_State字段空, #复制没有启动Connecting to master #没有连接上masterWaiting for master to send event #已经连上2,主服务器上的相关命令show master statusshow slave hostsshow logsshow binlog eventspurge logs to 'log_name'purge logs before 'date'reset master(老版本flush master)set sql_log_bin=3,从服务器上的相关命令slave startslave stopSLAVE STOP IO_THREAD #此线程把master段的日志写到本地SLAVE start IO_THREADSLAVE STOP SQL_THREAD #此线程把写到本地的日志应用于数据库SLAVE start SQL_THREADreset slaveSET GLOBAL SQL_SLAVE_SKIP_COUNTERload data from mastershow slave status(SUPER,REPLICATION CLIENT)CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= #动态改变master信息PURGE MASTER [before 'date'] 删除master端已同步过的日志4,Master 同步线程状态以下列出了master的 Binlog Dump 线程 State 字段中最常见的几种状态。如果在master上没有 Binlog Dump 线程,那么同步就没有在运行。也就是说,没有slave连接上来。Sending binlog event to slave事件是由二进制日志构成,一个事件通常由更新语句加上其他信息。线程读取到一个事件并正发送到slave上。Finished reading one binlog; switching to next binlog读取完了一个二进制日志,正切换到下一个。Has sent all binlog to slave; waiting for binlog to be updated已经读取完全部未完成更新日志,并且全部都发送到slave了。它处于空闲状态,正等待在master上执行新的更新操作以在二进制日志中产生新的事件,然后读取它们。Waiting to finalize termination当前线程停止了,这个时间很短。5,Slave的I/O线程状态以下列出了slave的I/O线程 State 字段中最常见的几种状态。从MySQL 4.1.1开始,这个状态在执行 SHOW SLAVE STATUS 语句结果的Slave_IO_State 字段也会出现。这意味着可以只执行 SHOW SLAVE STATUS 语句就能了解到更多的信息。Connecting to master该线程证尝试连接到master上。Checking master version确定连接到master后出现的一个短暂的状态。Registering slave on master确定连接到master后出现的一个短暂的状态。Requesting binlog dump确定连接到master后出现的一个短暂的状态。该线程向master发送一个请求,告诉它要请求的二进制文件以及开始位置。Waiting to reconnect after a failed binlog dump request如果二进制日志转储(binary log dump)请求失败了(由于连接断开),该线程在休眠时进入这个状态,并定期重连。重连的时间间隔由 --master-connect-retry 选项来指定。Reconnecting after a failed binlog dump request该线程正尝试重连到master。Waiting for master to send event已经连接到master,正等待它发送二进制日志。如果master闲置时,这个状态可能会持续较长时间,如果它等待超过 slave_read_timeout 秒,就会发生超时。这时,它就会考虑断开连接,然后尝试重连。Queueing master event to the relay log已经读取到一个事件,正把它拷贝到中继日志中以备SQL线程处理。Waiting to reconnect after a failed master event read读日志时发生错误(由于连接断开)。该线程在重连之前休眠 master-connect-retry 秒。Reconnecting after a failed master event read正尝试重连到master。当连接确定后,状态就变成 Waiting for master to send event。Waiting for the slave SQL thread to free enough relay log spacerelay_log_space_limit 的值非零,中继日志的大小总和超过这个值了。I/O线程等待SQL线程先处理中继日志然后删除它们以释放足够的空间。Waiting for slave mutex on exit当前线程停止了,这个时间很短。6,Slave的SQL线程状态以下列出了slave的SQL线程 State 字段中最常见的几种状态:Reading event from the relay log从中继日志里读到一个事件以备执行。Has read all relay log; waiting for the slave I/O thread to update it已经处理完中继日志中的全部事件了,正等待I/O线程写入更新的日志。Waiting for slave mutex on exit当前线程停止了,这个时间很短。
0 0