mysql主从配置

假设 主服务器地址 10.1.1.108 从服务器地址 10.1.1.161

1、主服务器

(1).建立数据库

mysql> CREATE DATABASE `test_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Query OK, 1 row affected (0.00 sec)

(2).修改my.cnf文件

在[mysqld]中增加

#for master and slave

server-id = 1

log-bin = mysql-bin

binlog-do-db = test_db

binlog-ignore-db = mysql

(3).对从库新建用户

mysql> grant replication slave on *.* to 'root'@'10.1.1.161' identified by '';

Query OK, 0 rows affected (0.00 sec)

(4).重启mysql

$ sudo service mysql restart

(5).查看master状态

mysql> show master status\G;

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

File: mysql-bin.000001

Position: 106

Binlog_Do_DB: test_db

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

ERROR:

No query specified

2、从服务器

(1).建立数据库

mysql> CREATE DATABASE `test_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Query OK, 1 row affected (0.00 sec)

(2).修改my.cnf文件

在[mysqld]中增加

#for master and slave

server-id = 2

binlog-do-db = test_db

(3).重启mysql

$ sudo service mysql restart

(4).将从服务器指向主服务器

mysql> slave stop;

Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_host='10.1.1.108', master_user='root', master_password='', master_log_file='mysql-bin.000001',master_log_pos=106;

Query OK, 0 rows affected (0.01 sec)

mysql>

mysql> slave start;

Query OK, 0 rows affected (0.00 sec)

(5).重启mysql,查看运行状态

$ sudo service mysql restart

mysql> show slave status \G;

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

Slave_IO_State: Waiting for master to send event

Master_Host: 10.1.1.108

Master_User: root

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 106

Relay_Log_File: mysqld-relay-bin.000004

Relay_Log_Pos: 251

Relay_Master_Log_File: mysql-bin.000001

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

Relay_Log_Space: 407

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)

ERROR:

No query specified

注意:Slave_IO_Running 和Slave_SQL_Running都为Yes才算设置正确

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇博客会详细介绍如何配置主从配置,但重点是想分享如何当配置未成功时,如何调试。 原文连接(推荐这个看 比较清晰)...
    海里的沙丁鱼阅读 797评论 0 2
  • 环境准备: 两个相同版本的mysql实例10.230.20.101/10.230.20.102(mysql 5.7...
    jsdjl阅读 467评论 0 0
  • 【MySQL】Linux下MySQL 5.5、5.6和5.7的RPM、二进制和源码安装 1.1BLOG文档结构图 ...
    小麦苗DB宝阅读 10,643评论 0 31
  • 夜凉如水 都道宫中繁华,可有一个地方,却是森森如阴,不知是因为故去了太多的如花容颜,还是消磨了无数殷红的期待?夜袭...
    月懒西楼阅读 703评论 6 7
  • 读一个人的传记或者看历史,记住了多少事儿不是最重要的(当然能记住的越多越好,但重点不是记忆),更重要的是读懂在当时...
    书愿阅读 812评论 0 1