当前位置:Gxlcms > 数据库问题 > mysql主从复制环境搭建

mysql主从复制环境搭建

时间:2021-07-01 10:21:17 帮助过:46人阅读


  1. 主my.cnf文件 (192.168.1.64)

mysqld模块加入

log-bin=mysql-bin 启动二进制文件

server-id=1 服务器ID

binlog-do-db = 需要复制的库名可以用,分割

如果主数据库存有数据

首先进行锁表操作,不让数据进行写入动作,这么做事为了防止从数据库的原始数据和主数据库的原始数据不一致。

主进入mysql

mysql> flush tables with read lock;

mysqldump –uroot –p123456 testDB > /home/testDB.sql

scp  -r /home/testDB.sql root@192.168.1.12:/home

从进入mysql

create database testDB;

mysql -u root  "你的数据库名"< “你的sql文件”

做完之后进行unlock tables; 解锁表操作

进入mysql

新建用户并且给从使用

#grant replication slave on *.* to ‘test‘@‘192.168.1.12‘ identified by ‘1234‘;

也可以Grant all on testDB.* to ‘test‘@‘192.168.1.12‘ identified by ‘1234‘ with grant option;

show master status;
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      106 | testDB       |                  |
+------------------+----------+--------------+------------------+


2.从my.cnf文件(192.168.1.12)


mysqld模块加入

log-bin=mysql-bin 启动二进制文件

server-id=2 服务器ID

进入mysql

change master to master_host=‘192.168.1.64‘,master_user=‘test‘,master_password=‘1234‘,master_log_file=‘mysql-bin.000002‘,master_log_pos=2852;

start slave;

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

     

要求Slave_IO_Running 和SQL等于yes才行

如果俩个有一个没有yes

检查数据库用户权限和防火墙,

从服务器登陆主测试mysql -h192.168.1.64 -utest -p 

在检查serverip

mysql> show variables like ‘server_id‘;

或者slave stop;   slave start;

如果还不行就装俩个一样版本的mysql测试一下




mysql主从复制环境搭建

标签:mysql   主从搭建   

人气教程排行