mysql 主从复制实验------包含授权、bin-log

来源:互联网 发布:如何成为社交高手 知乎 编辑:程序博客网 时间:2024/05/21 06:36
环境:两台 Linux虚拟机 centos  5.5

200.168.10.10 master
200.168.10.5   slave

-------------------------

mysql 用户授权:

bin-log日志:

200.168.10.10_master:

200.168.10.5_slave:
从如果能复制主服务器的数据,就要求有访问主的权限:
所以授权是必要的。
----
[root@localhost test]# /usr/local/mysql/bin/mysql -uroot -p -h200.168.10.10
Enter password: 
ERROR 1130 (HY000): Host '200.168.10.5' is not allowed to connect to this MySQL server
[root@localhost test]# 

在主服务器上:
mysql> grant all on *.* to user@200.168.10.5 identified by '123';

//或者:grant replication slave on *.* to user@200.168.10.5 identified by '123';

在从上登录主的数据库
[root@localhost test]# /usr/local/mysql/bin/mysql -uuser -p -h200.168.10.10
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3106
Server version: 5.1.59-community-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

--------------------------------------------------------------------

查看bin-log是否开启:
show variables like "%log_bin%";

 reset master;之后:
[root@localhost var]# ls
ibdata1      lehand                     mysql-bin.000001
ib_logfile0  localhost.localdomain.err  mysql-bin.index
ib_logfile1  localhost.localdomain.pid  test
left_hand    mysql

*
查看最后一个bin-log文件信息:
mysql> show master status;
*

**
查看bin-log的动作记录
[root@localhost var]# /usr/local/mysql/bin/mysqlbinlog --no-defaults mysql-bin.000003

**
****
通过bin-log日志恢复数据:
[root@localhost var]# /usr/local/mysql/bin/mysqlbinlog --no-defaults mysql-bin.000001 |/usr/local/mysql/bin/mysql -uroot -p test
Enter password: 
[root@localhost var]# 

****

reset master :清理mysql-bin文件;
flush logs;  生成一个新的bin-logs;

***
//mysql备份:
[root@localhost var]# /usr/local/mysql/bin/mysqldump -uroot -p test -l -F > test.sql
Enter password: 
[root@localhost var]# 
***
*
恢复方法:
用先前备份的sql文件恢复;
用bin-log日志恢复;
可以根据位置点来恢复文件:
--stop-position  348
--start-position 467
恢复操作点之间的部分数据
[root@localhost var]# /usr/local/mysql/bin/mysqlbinlog --no-defaults mysql-bin.000005 --start-position="348" --stop-position="467" |/usr/local/mysql/bin/mysql -uroot -p test
Enter password: 
[root@localhost var]# 
*
*****
主从复制: --另一个备份方法: 最高境界,这几个备份方法的混合使用:最后的是dump -l -F 不用考虑枷锁解锁
----在从服务器上进行查询操作。负载均衡、读写分离:(实时性要求高的话,查询不要在从服务器)
----------
考虑数据库的锁定
主服务器配置不变  cat /etc/my.cnf
server-id
bin-log

查看授权:
mysql> show grants for user@200.168.10.5;


隧道传输:把 200.168.10.10 当前目录下的 test.sql文件 传到200.168.10.5 的test目录下
[root@localhost var]# scp test.sql  200.168.10.5:/test

从服务器:
server-id       = 2

# The replication master for this slave - required
master-host     =   200.168.10.10
#
# The username the slave will use for authentication when connecting
# to the master - required
master-user     =   user
#
# The password the slave will authenticate with when connecting to
# the master - required
master-password =   123
#
# The port the master is listening on.
# optional - defaults to 3306
master-port     =  3306


重启mysql:
[root@localhost test]# /usr/local/mysql/bin/mysqld_safe  --user=mysql &
关闭:
pkill mysqld


进入从自己的数据库,执行 
show slave status;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 200.168.10.10
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 106
               Relay_Log_File: localhost-relay-bin.000006
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000003
             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: 106
              Relay_Log_Space: 453
              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)


说明已经同步的情况:
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes

---
主服务器端:
mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000003
        Position: 106
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

*****


***
slave无法同步的问题:
先停掉slave

如果不同步 再看视频:

第12讲:
 


mysql 主从复制、授权、bin-log - 无幻 - 一个PHPer
 
***
mysql 主从复制、授权、bin-log - 无幻 - 一个PHPer
 
mysql 主从复制、授权、bin-log - 无幻 - 一个PHPer
 
 
mysql 主从复制、授权、bin-log - 无幻 - 一个PHPer