当前位置:Gxlcms > 数据库问题 > mysql-5.5配置主从 及 主主关系

mysql-5.5配置主从 及 主主关系

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

|      107 | test         | mysql            |

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

1 row in set (0.00 sec) //上面两个值将在从Mysql中用到。。

下面配置从mysql

[root@localhost ~]# vim /etc/my.cnf

大约49行log-bin=mysql-bin处

添加:replicate-do-db=test    //复制的库名test

    replicate-ignore-db=mysql //不复制的库名mysql

修改:server-id = 2    值唯一

[root@localhost ~]# /etc/init.d/mysqld restart

[root@localhost ~]# mysql -u root -predhat

[root@localhost ~]# create database test; //创建和主上面一样的同步的库名

[root@localhost ~]# slave stop;  

[root@localhost ~]# change master to master_host=‘192.168.1.1‘, master_user=‘baidu‘, master_password=‘123.com‘, master_log_file=‘mysql-bin.000005‘, master_log_pos=107;

//指定主的IP,可复制用户及密码。上面两个值在每次重启主Mysql都会变。如果重启,则需要重新change master to 了

[root@localhost ~]# slave start;  //开启复制

[root@localhost ~]# 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.1

                  Master_User: baidu

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 107

               Relay_Log_File: localhost-relay-bin.000003

                Relay_Log_Pos: 253

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: test

          Replicate_Ignore_DB: mysql

两个Yes表示主从配置正确……

接下来我们到主mysql的test库上面创建一个表,再到从mysql上面看看是否同步过来了。


主mysql

mysql> use test;

Database changed

mysql> create table student(id int(11) not null, primary key(id));

Query OK, 0 rows affected (0.06 sec)


mysql> show tables;

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

| Tables_in_user |

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

| student        |

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

1 row in set (0.00 sec)


从mysql

mysql> use test;

Database changed

mysql> show tables;

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

| Tables_in_user |

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

| student        |

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

1 row in set (0.00 sec)

可以看到student表已经成功同步过来了,至此Mysql主从实验已完成。 

PS:常见错误,防火墙阻塞了端口,授权密码填写错误   mysql-bin错误    每次重启服务会变更        change mater to   需要重新敲。

Mysql编译安装的都有一个小小的BUG,不管什么错误导致mysql重启失败都会报PID丢失,一般都是你的配置文件写错了,或者查找日志文件进行拍错。。

本文出自 “11000174” 博客,请务必保留此出处http://11010174.blog.51cto.com/11000174/1901854

mysql-5.5配置主从 及 主主关系

标签:搭建mysql主从

人气教程排行