Linux下MySQL主从同步复制详细配置

注:主服务器:192.168.100.245      从服务器:192.168.100.246

一、检查系统是否安装MySQL服务

1.1 rpm -qa |grep -i mysql

二、安装MySQL服务器

2.1 rpm -ivh MySQL-server-5.5.16-1.rhel4.i386.rpm MySQL-client-5.5.16-1.rhel4.i386.rpm MySQL-devel-5.5.16-1.rhel4.i386.rpm

三、启动MySQL服务

3.1 service mysqld start

四、登录root账户

4.1 mysql -h localhost -u root -p 回车输入登录密码(刚安装的时候是没有root账号的,直接输入mysql回车即可登录)

4.2 查找mysql数据库下的user用户及主机

select user,host,password from mysql.user;

4.3 删除匿名用户

Delete from mysql.user where user=’’;

4.4 设置mysql的root账户密码

Set password for [email protected]=password(‘123456’);

4.5 查看数据库

show databases;

4.6 退出mysql

Exit;

五、编辑主服务器的mysql主配置文件/etc/my.cnf

要先在从服务器的主机上新建这个用户:

 mysql>grant replication slave on *.* to ‘mydata‘@192.168.100.245 identified by ‘123456‘;

在[mysqld]下添加如下

server-id=1       #主机标示,整数

log-bin=/var/lib/mysql/mysql-bin.log   #确保此文件可写

master-host=192.168.100.246   #主机地址

master-user=mydata    #远程登录数据库用户名称

master-pass=123456  #远程登录数据库用户密码

master-port=3306   #远程数据库服务器端口号

master-connect-retry=60  #响应时间

binlog-do-db=test  #需要备份数据,多个写多行

replicate-do-db=test  #指定复制到哪个库

保存退出

六、重启服务器

Service mysqld restart

七、进入mysql锁表

Mysql>flush tables with read lock;

退出

八、备份

mysqldump -hlocalhost -uroot -p3306 -p test >/home/test.sql

九、远程复制

scp test.sql [email protected]:/home/(在当前目录下复制)

十、进入mysql解锁

Mysql> unlock tables;

十一、验证:

Show master status\G;  (显示如下内容)

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

File: mysql-bin.000005

Position: 537

Binlog_Do_DB: test

Binlog_Ignore_DB:

1 row in set (0.00 sec)

ERROR:

No query specified

从服务器配置(192.168.100.246)

要先在主服务器新建这个用户:

 mysql>grant replication slave on *.* to ‘mydata‘@192.168.100.246 identified by ‘123456‘;

十二、编辑主配置文件 /etc/my.cnf

在[mysqld]下添加如下命令

server-id=2

log-bin=/var/lib/mysql/mysql-bin.log

master-host=192.168.100.245

master-user=mydata

master-pass=123456

master-port=3306

master-connect-retry=60

binlog-do-db=test

replicate-do-db=test

保存退出

十三、重启mysql服务

Service mysql restart

十四、验证

Show Slav status \G;(显示如下内容则配置成功)

mysql> show slave status \G;

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

Slave_IO_State: Reconnecting after a failed master event read

Master_Host: 192.168.100.245

Master_User: mydata

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000005

Read_Master_Log_Pos: 537

Relay_Log_File: mysqld-relay-bin.000007

Relay_Log_Pos: 674

Relay_Master_Log_File: mysql-bin.000005

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: test

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

Relay_Log_Space: 674

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

1 row in set (0.00 sec)

ERROR:

No query specified

十五、验证数据是否可以同步

主:

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;

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

| Tables_in_test |

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

| aaa            |

| ccc            |

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

2 rows in set (0.00 sec)

mysql> create table ooo(id int, date varchar(20));

Query OK, 0 rows affected (0.01 sec)

mysql>

从:

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;

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

| Tables_in_test |

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

| aaa            |

| ccc            |

| ooo            |

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

3 rows in set (0.00 sec)

在从服务器进行删除:

mysql> drop table ooo;

Query OK, 0 rows affected (0.00 sec)

在主服务器:

mysql> show tables;

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

| Tables_in_test |

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

| aaa            |

| ccc            |

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

2 rows in set (0.00 sec)

十六、测试完毕!

时间: 2024-07-29 01:42:48

Linux下MySQL主从同步复制详细配置的相关文章

Linux下MySQL主从同步配置

Centos6.5 MySQL主从同步 MySQL版本5.6.25 主服务器:centos6.5 IP:192.168.1.101 从服务器:centos6.5 IP:192.168.1.102 一.主服务器相关配置 1.创建同步账户并指定服务器地址 [[email protected] ~]mysql -uroot -p mysql>use mysql mysql>grant replication slave on *.* to 'testuser'@'192.168.1.102' ide

Linux下mysql主从同步备份master-slave详细配置

1.概要首先要准备两台服务器,一台主服务器(Master),另一台从服务器(Slave),然后要保证Master与Slave的版本要相同且Master不能高于Slave的版本,一般稳健的做法都是使其版本相同,因为MySQL不同版本之间的binlog(二进制日志)格式可能会不一样,最后会导致同步出现异常.介绍先到这里,以下我们先确认两台服务器的信息: 主服务器(Master):IP:192.168.1.100系统:Linux(Ubuntu 64位 v10.10)MySQL版本:5.1.49需要同步

MySQL主从同步(复制)的配置

1.主从复制的原理: *Master,记录数据更改操作 - 启用binlog记录模式 - 允许Slave读取binlog日志 *Slave运行2个同步线程 - Slave_IO:负责连接Master,复制其binlog日志文件到本机的relay-log文件 - Slave_SQL:执行本机relay-log文件里的SQL语句,重现Master的数据操作 2.基本构建思路: 1)初始化现有库:将主库导入从库,确保数据一致性 2)配置Master,主服务器:调整运行参数,授权一个同步用户 3)配置S

Linux下mysql主从配置

mysql服务器的主从配置,这样可以实现读写分离,也可以在主库挂掉后从备用库中恢复需要两台机器,安装mysql,两台机器要在相通的局域网内主机A: 192.168.1.100从机B:192.168.1.101可以有多台从机1.先登录主机 Amysql>GRANT REPLICATION SLAVE ON *.* TO ‘backup’@’192.168.1.101‘ IDENTIFIED BY ‘123456’;赋予从机权限,有多台丛机,就执行多次2. 打开主机A的my.cnf,输入server

centos下MySQL主从同步配置

centos下MySQL主从同步配置一.环境 主机: master操作系统:centos 5.3 IP:192.168.1.222 MySQL版本:5.0.77 从机: slave操作系统:centos 5.3 IP:192.168.1.220 MySQL版本:5.0.77 二.创建数据库 分别登录master机和slave机的mysql:mysql –u root –p 创建数据库:create database repl; 三.master机和slave机的相关配置 1.修改master机器

linux下 mysql主从备份

在使用Ubuntu作为开发环境时经常需要在全局安装一些依赖框架等,这个时候就常常需要用到root权限,但是在Ubuntu下第一次使用su命令时会提示认证失败:查找资料后发现Ubuntu下root权限默认是锁定的,可能是处于安全考虑,但是作为开发人员肯定是需要root权限的. 在命令行中可以输入下面命令设置root密码,这样就能随时使用root权限了: [email protected]:~$ su 密码: su:认证失败 [email protected]:~$ sudo passwd [sud

Windows下MySQL主从同步

Windows下MySQL主从同步修改master的my.ini配置文件在master中添加一个mysql主从复制需要的账号查看master的status修改slave的my.ini配置文件slave连接master库测试主从同步 Windows下MySQL主从同步 修改master的my.ini配置文件 从mysql官网下载的压缩包中默认是没有my.ini文件的,需要自己在根目录手动建立一个my.ini文件 [mysqld] #设置3310端口 port = 3310 #server-id和l

MySQL半同步复制原理配置与介绍详解

环境介绍: Ubuntu Server 16.04.2+MySQL 5.7.17 Community Server (GPL) MySQL安装 通过APT的方式安装,官方指导文档地址: https://dev.mysql.com/downloads/repo/apt/ 1.下载mysql-apt-config_0.8.3-1_all.deb 2.安装deb A Quick Guide to Using the MySQL APT Repository: https://dev.mysql.com

mac下mysql主从同步配置

一.环境说明 两台mac,mysql环境master是5.7.20,slave是5.7.21 master IP: 172.21.127.10 slave IP:172.21.127.12 二.master机器配置 1.更改配置文件 # Default Homebrew MySQL server config [mysqld] # Only allow connections from localhost bind-address = 172.21.127.10 log-error=/var/l