码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从

时间:2014-10-31 19:16:30      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:mysql   mysql主从   

实验目的:

用于主库挂掉后快速切换到从库,实现备份作用;

实验环境:

两个linux主机,均己安装mysql,在同局域网内相互通;

A: 192.168.0.117

B: 192.168.0.118

步骤:

1、主库A上操作:

[root@mysql-master ~]# /etc/init.d/mysqld start

[root@mysql-master ~]# mysqladmin -u root password  "123456"    #没有root密码先设密码

mysql>GRANT REPLICATION SLAVE ON *.* TO ‘backup’@’192.168.1.101‘ IDENTIFIED BY ‘123456’;       #给从服务器授权

mysql>flush privileges;

[root@mysql-master ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

server-id = 1
log-bin=mysql-bin

binlog_ignore_db             #不需要备份的数据库,多个写多行

master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

2、从库B上操作:

[root@mysql-slave ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0

server-id = 2
master-host=192.168.0.117
master-user=root
master-password=root

master-port=3306
replicate-ignore-db=mysql   #忽略复制哪个库,可多行多个库;

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

3、主从上都重启mysqld服务

[root@mysql-slave ~]# /etc/init.d/mysqld restart

4、查看主从库的状态

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


mysql>slave start;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.117
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 3341752
               Relay_Log_File: mysqld-relay-bin.000006
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql
           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: 3341752
              Relay_Log_Space: 3342198
              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> create database test_back;
Query OK, 1 row affected (0.04 sec)

从库中同步了刚才的库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| backtest           |
| mysql              |
| test               |
| test_back          |
| zabbix             |
+--------------------+
6 rows in set (0.13 sec)

主从复制成功 ;


注意:

IO 复制连接master主机 把主数据库服务binlog日志里的sql语句
   存放本机的relaybinlog文件里
SQL  执行本机relaybinlog文件里的sql语句,把数据写进本机的数据库里

从数据库上没有主数据库服务器的库和表时 SQL进程会出错

解决方法:

mysql>slave stop;

mysql> change master to

-> master_host=‘master_host_name‘, //主服务器的IP地址

-> master_user=‘replication_user_name‘, //同步数据库的用户

-> master_password=‘replication_password‘, //同步数据库的密码

-> master_log_file=‘recorded_log_file_name‘, //主服务器二进制日志的文件名(前面要求记住的参数)

-> master_log_pos=recorded_log_position; //日志文件的开始位置(前面要求记住的参数)

其中,master_log_file与master_log_pos应该在备份主数据库时记录,通过show master status获取,在从服务器上通过change master to设置这两个参数后,从服务器就会从这个位置开始读取主服务器的二进制日志.使用select maseter_pos_wait() 可以确保从服务器跟上主服务器的日志进度.

本文出自 “Linux_蚂蚁” 博客,请务必保留此出处http://onlyoulinux.blog.51cto.com/7941460/1570472

mysql主从

标签:mysql   mysql主从   

原文地址:http://onlyoulinux.blog.51cto.com/7941460/1570472

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!