MySQL主从复制与读写分离

来源:互联网 发布:网络奇兵ign 编辑:程序博客网 时间:2024/06/05 14:07

参考:http://www.cnblogs.com/luckcs/articles/2543607.html

WINDOWS :my-default.ini  LINUX: /etc/mysql/my.cnf

主服务器(虚拟机Ubuntu)上的操作:

主服务器修改配置文件my.cnf
[mysqld]之后添加
################设置主服务器###############
log-bin=mysql-bin  #打开mysql二进制日志
server-id=1    #设置mysql_id,主从不能相同
binlog-do-db=mytest  #设置二进制日志记录的库
binlog-ignore-db=mysql ##设置二进制日志不记录的库
sync_binlog=1 
###############从服务器

主服务器创建MYSQL 用户 rep1,创建数据库mytes

授权给从数据库服务器
mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.10.131' identified by ‘password’;

查询主数据库状态

mysql> show master status;
Empty set (0.00 sec)

重启MYSQL

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000021      107| mytest       | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

从服务器(虚拟机Ubuntu)上的操作:

###设置MYSQL从服务器##########my.cnf 的[mysqld]之后添加
log-bin=mysql-bin
server-id=2
replicate-do-db=mytest   #设置同步的库
replicate-ignore-db=mysql  #设置不同步的库
log-slave-updates    #同步后记录二进制日志
slave-skip-errors=all    
sync_binlog=1
slave-net-timeout=60
##############################

创建数据库mytes

执行同步SQL语句
change master to master_host='192.168.1.3',
master_user='rep_master', master_password='123',
master_log_file='mysql-bin.000001', #和上面的一样(show master status;之后的提示)
master_log_pos=107;#和上面的一样(show master status;之后的提示)

正确执行后启动Slave同步进程
mysql> start slave;

主从同步检查
mysql> show slave status\G
==============================================
**************** 1. row *******************
Slave_IO_State:
Master_Host: 192.168.10.130
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 415
Relay_Log_File: localhost-relay-bin.000008
Relay_Log_Pos: 561
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: YES
Slave_SQL_Running: YES
Replicate_Do_DB:
……………省略若干……………
Master_Server_Id: 1
1 row in set (0.01 sec)

其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
出现:Slave_IO_Running: Connecting(参考http://bbs.csdn.net/topics/390273497?page=1)

测试:

mysql -urep_master -p -h192.168.1.6//在从服务器上连接主服务器的MYSQL,提示:ERROR 2003 (HY000): Can't connect to MySQL server on "host"

发现无法连接主服务器,要将主服务器的配置文件做如下更改:

#bind-address  = 127.0.0.1#将该行注释掉,让MYSQL从服务器可以访问主MYSQL服务器

,再重启MYSQL ,再连接mysql -urep_master -p -h192.168.1.6//在从服务器上连接主服务器的MYSQL,就OK

 

切换到root 登录MYSQL,mysql> show slave status\G

Slave_IO_Running: Connecting改为Slave_IO_Running: Yes

 

验证主从复制效果:

主服务器上的操作
在主服务器上创建数据库first_db
mysql> create database first_db;
Query Ok, 1 row affected (0.01 sec)

在主服务器上创建表first_tb
mysql> create table first_tb(id int(3),name char(10));
Query Ok, 1 row affected (0.00 sec)

在主服务器上的表first_tb中插入记录
mysql> insert into first_tb values (001,’myself’);
Query Ok, 1 row affected (0.00 sec)

在从服务器上查看)

 

MYSQL 的多线程复制:

http://blog.chinaunix.net/uid-10661836-id-4101154.html

http://dinglin.iteye.com/blog/search?query=transfer

 

0 0
原创粉丝点击