MySQL主从复制

修改mysql配置文件

1.        找到my.cnf(windows下为my.ini)配置文件的位置

           find / -name "*my.cnf*"

2.        修改配置文件,添加如下行

server-id=1   //给数据库服务的唯一标识,一般为大家设置服务器Ip的末尾号

log-bin=master-bin

log-bin-index=master-bin.index

启动mysql服务

service mysqld start

通过命令行登录管理MySQL服务器

mysql -uroot -p 'new-password'

创建用户并授权给从服务器

createuser repl; //创建新用户

GRANTREPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.16%' IDENTIFIED BY 'mysql';

我的从服务器的ip都是16开头

查看主数据库日志状态

配置从数据库会用到

mysql>SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

|File             | Position | Binlog_Do_DB | Binlog_Ignore_DB|

+------------------+----------+--------------+------------------+

|mysql-bin.000002 |      383 |              |                  |

+------------------+----------+--------------+------------------+

配置Slave从服务器

1.        修改my.cnf文件,添加如下行

server-id=2   //给数据库服务的唯一标识,一般为大家设置服务器Ip的末尾号

relay-log-index=slave-relay-bin.index

relay-log=slave-relay-bin

2.        连接Master

change master tomaster_host='192.168.0.104', //Master 服务器Ip

master_port=3306,

master_user='repl',

master_password='mysql', 

master_log_file=' mysql-bin.000002',//Master服务器产生的日志

master_log_pos=383;

3.        启动Slave

           start slave;

主从同步检查

mysql>show slave status\G;

***************************1. row ***************************

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.1.166

                  Master_User: master

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 383

               Relay_Log_File:mysqld-relay-bin.000002

                Relay_Log_Pos: 439

        Relay_Master_Log_File: mysql-bin.000002

             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: 383

              Relay_Log_Space: 595

              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 inset (0.00 sec)

 

其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。

将同步之前master里的数据copy到slave中

(1)主数据库进行锁表操作,不让数据再进行写入动作
mysql> FLUSH TABLES WITH READ LOCK;

(2)查看主数据库状态
mysql> show master status;

(3)记录下 FILE 及 Position 的值。
将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器。

(4)修改该文件夹的所属组所属用户

chown -Rmysql:mysql test

(5)取消主数据库锁定
mysql> UNLOCK TABLES;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值