标签:mysql
############mysql主从复制##########
实验环境:
Redhat6.5 ##主从服务器版本一致
IP:172.25.8.1 master ##主服务器或称主库
IP:172.25.8.2 slave ##从服务器或称从库(可以多个)
实验内容:
1.修改master主服务器
[mysqld] ##在[mysqld]模块内添加
log-bin=mysql-bin ##启用二进制日志
server-id=1 ##服务器唯一ID,一般使用IP最后一段,master此处为1
2.修改从服务器slave
[mysqld] ##在[mysqld]模块内添加
#log-bin=mysql-bin ##从库一般不设置,若有A-->B-->C级联同步,中间的B数据库服务要开启log-bin
server-id=2 ##服务器唯一ID,一般使用IP最后一段,slave此处为2
3.重启主从服务器的mysql
/etc/init.d/mysql restart
4.在主服务器建立账户及授权slave
mysql> grant replication slave on *.* to ‘rep‘@‘172.25.8.%‘ identified by ‘hjy123456‘
##设置复制用户rep,“%”表示任意段,密码hjy123456
mysql> show variables like ‘server_id‘; ##查看系统变量及其值
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 | ##server-id=1
+---------------+-------+
1 row in set (0.00 sec)
mysql> select user,host from mysql.user;
+-------+------------+
| user | host |
+-------+------------+
| root | 127.0.0.1 |
| rep | 172.25.8.% | ##rep用户,172.25.8.%
| root | localhost |
| user1 | localhost |
+-------+------------+
mysql> show master status; ##显示记录在案的信息,主从复制时就要从这里(259)开始
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 259 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
【执行完此步骤不要再操作master的mysql,防止Position变化】
5.在从服务器配置连接主服务器
mysql> change master to master_user=‘rep‘,master_host=‘172.25.8.1‘,master_port=‘3306‘,master_password=‘hjy123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=259;
##rep是主服务器上建立的用于复制的用户,172.25.8.1是主服务器的IP,3306是主库端口,password是rep用户的密码,mysql-bin.000001是刚查的二进制日志文件名,259是刚查的二进制日志偏移量,复制读取位置
mysql> show slave status\G; ##查看复制状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.8.1 ##主库地址
Master_User: rep ##授权账户
Master_Port: 3306 ##主库端口
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 259 ##二进制日志偏移量
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes ##IO线程负责从从库到主库读取log-bin日志,此状态必须为YES
Slave_SQL_Running: Yes ##SOL线程负责读取中继日志(relay-log)的数据转化为SQL语句应用到从库中,此状态必须为YES【可以使用监控软件,监控IO和SQL线程,NO时发送警报】 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: 259
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:
6.测试主从复制
1)主库建立rep_db数据库,并插入数据
mysql> create database rep_db ; ##创建rep_db数据库
mysql> use rep_db; ##进入rep_db库
mysql> create table rep_tb(id varchar(10),name varchar(20)); ####创建rep_tb表
mysql> desc rep_tb ; ##查看rep_tb表结构
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | varchar(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> insert into rep_tb values(001,‘Tom‘); ##插入以一条数据
mysql> select * from rep_tb ; ##查看数据
+------+------+
| id | name |
+------+------+
| 1 | Tom |
+------+------+
2)从库查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| rep_db |
+--------------------+
3 rows in set (0.00 sec)
mysql> select * from rep_db.rep_tb;
+------+------+
| id | name |
+------+------+
| 1 | Tom |
+------+------+
本文出自 “12148275” 博客,转载请与作者联系!
标签:mysql
原文地址:http://12158275.blog.51cto.com/12148275/1911469