mysql主从复制

2 篇文章 0 订阅

mysql主从复制,是mysql应对读写压力的一种有效的方式

主从复制对于大多数人来说都是比较简单的,操作可行性较高。说到底,只需要改变几个文件的配置就行了。

主要步骤如下

一:主从服务器配置

1,版本一致
2,mysql已经启动

二:修改主服务器的master

#vim /etc/my.cnf
[mysqld]
log-bin = mysql-bin //启用二进制日志
server_id = 1       //此服务器id与从服务器的要不同

从服务器也进行类似的操作,但是server_id 要不同

三:重启两台服务器的mysql

/etc/init.d/mysql restart

四:在主服务器开启赋予从服务器的slave权限

mysql>GRANT REPLICATION SLAVE ON *.* to 'test'@'%' identified by '123123'; //需要注意的是,此账号一点要具有远程登录权限

(注:创建新账号: grant all privileges on *.* to test@"%" identified by "123123" ;   //  设置用户test,可以在远程访问mysql)
 mysql>flush privileges ;  //  mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效.
 
 mysql> grant all privileges on *.* to test@“192.168.1.100” identified by "123123" ;  //设置用户testuser,只能在客户端IP为192.168.1.100上才能远程访问mysql ;

五:登录主服务器,查看master的状态

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

六:配置从服务器的slave

mysql> CHANGE MASTER TO
    -> MASTER_HOST='114.67.228.235',
    -> MASTER_USER='test',
    -> MASTER_PASSWORD='123123',
    -> MASTER_LOG_FILE='mysql-bin.000009',
    -> MASTER_LOG_POS= 107;

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

七:查看从服务器的状态

mysql > show slave status\G;
************************** 1. row ***************************
           Slave_IO_State: Waiting for master to send event
              Master_Host: 114.67.228.235
              Master_User: test
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: mysql-bin.000009
      Read_Master_Log_Pos: 107
           Relay_Log_File: ubuntu-relay-bin.000002
            Relay_Log_Pos: 253
    Relay_Master_Log_File: mysql-bin.000009
         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: 107
          Relay_Log_Space: 410
          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: 
 Replicate_Ignore_Server_Ids: 
         Master_Server_Id: 1
1 row in set (0.00 sec)

注:Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误,如果这两个显示的都是no的话,那么可以查看错误日志寻找为什么错误:错误日志存放位置:show variables like ‘log_error’)。

以上操作过程,主从服务器配置完成。


八:测试

在主服务器插入一条数据看看

mysql > create database test; // 新建数据库
mysql > create tables test(id int(3), name char(20)); //  新建表

mysql > insert into test value (001,'haoren');

切换到从服务器,查看从数据库 有没有插入数据

mysql > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hi_db              |
| mysql              |
| performance_schema |
| test        |
+--------------------+
5 rows in set (0.01 sec)

然后我们能发现数据已经插入表中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值