mysql主从复制 之 一主一从
2012-03-30 14:47
337 查看
Linux下配置Mysql主从复制
系统环境:CentOS 5.6
Mysql版本:5.1.58
拓扑图:
这里是一主一从,要配置一主多从,其他从服务器把servier -id 修改为不同的数字,其他的按照从服务器的配置就 OK。
一、安装mysql
在主从服务器上安装mysql,最好是同样版本,如果主服务器版本高,从服务器版本低可能会出问题,如果主服务器版本低,从服务器版本高那倒是没有问题,这里用的是5.1.58
看Mysql主从复制安装篇
http://linux5588.blog.51cto.com/65280/800139
如果主从服务器型号一样,配置一样,主服务器安装完mysql之后,直接打包mysql安装目录,然后传到从服务器上,然后添加mysql用户,修改目录的权限,就可以启动从服务器的mysql服务。
二、配置主从复制
1.设置主库(在主服务器上操作)
以下操作,如果没有指定在从服务器上操作的,都是在主服务器上操作
1)修改主库my.cnf,vim /usr/local/mysql5.1.58/my.cnf
在[mysqld]部分,添加如下语句
2).赋予从库权限帐号,允许在主库上读取日志
(在从服务器上操作)立即到从库的机器上登录试试,看能否登录上:
3).检查用户是否创建成功
4)创建bookfm数据库
5)锁主库表
6)显示主库信息,记录File和Position,从库设置将会用到
7)将主库数据目录打包,发送给从库机器,这种方式适合于数据库刚安装时,数据库比较单一,如果数据库比较大可以使用mysqldump的方式把数据导出为.sql文件,然后在从库上创建同名数据库,把数据导入
2.设置从库(在从服务器上操作)
以下操作,如果没有指定在主服务器上操作的,都是在从服务器上操作
1)备份从库data目录,把从主库复制过来的data.tar.gz直接解压出来
2)解锁主库表(在主服务器上操作)
3)启动从库mysql服务
4)在从库上设置同步
5)在主库上的bookfm数据库上建立book表
6)在从库上查询
系统环境:CentOS 5.6
Mysql版本:5.1.58
拓扑图:
这里是一主一从,要配置一主多从,其他从服务器把servier -id 修改为不同的数字,其他的按照从服务器的配置就 OK。
一、安装mysql
在主从服务器上安装mysql,最好是同样版本,如果主服务器版本高,从服务器版本低可能会出问题,如果主服务器版本低,从服务器版本高那倒是没有问题,这里用的是5.1.58
看Mysql主从复制安装篇
http://linux5588.blog.51cto.com/65280/800139
如果主从服务器型号一样,配置一样,主服务器安装完mysql之后,直接打包mysql安装目录,然后传到从服务器上,然后添加mysql用户,修改目录的权限,就可以启动从服务器的mysql服务。
二、配置主从复制
1.设置主库(在主服务器上操作)
以下操作,如果没有指定在从服务器上操作的,都是在主服务器上操作
1)修改主库my.cnf,vim /usr/local/mysql5.1.58/my.cnf
在[mysqld]部分,添加如下语句
server-id = 1 //主从库id不能重复 log-bin=binlog //开启二进制日志文件 binlog-do-db=bookfm //要同步的数据库名字 如果不指定这条那么是同步所有新建的数据库 character-set-server = utf8 //数据库字符集 replicate-ignore-db = mysql //不进行同步的数据库 replicate-ignore-db = test //不进行同步的数据库 replicate-ignore-db = information_schema //不进行同步的数据库
在[mysql]部分,找到 #no-auto-rehash,去掉no,这个功能就是按table键自动补全功能,只能补齐表,列名 [mysql] #no-auto-rehash auto-rehash
2).赋予从库权限帐号,允许在主库上读取日志
mysql>grant replication slave on *.* to 'admin'@'192.168.100.247' identified by '123456';
(在从服务器上操作)立即到从库的机器上登录试试,看能否登录上:
[root@server2 ~]# mysql -uadmin -p -h 192.168.100.248 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.58-log Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> 成功显示mysql>界面表示设置成功。
3).检查用户是否创建成功
mysql> use mysql; 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> select user,host,password from user; +-------+-----------------+-------------------------------------------+ | user | host | password | +-------+-----------------+-------------------------------------------+ | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | root | server3.com | | | root | 127.0.0.1 | | | | localhost | | | | server3.com | | | admin | 192.168.100.247 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-------+-----------------+-------------------------------------------+ 6 rows in set (0.00 sec) 可以看到已经创建成功。
4)创建bookfm数据库
mysql> create database bookfm; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bookfm | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
5)锁主库表
mysql> flush tables with read lock; Query OK, 0 rows affected (0.01 sec)
6)显示主库信息,记录File和Position,从库设置将会用到
7)将主库数据目录打包,发送给从库机器,这种方式适合于数据库刚安装时,数据库比较单一,如果数据库比较大可以使用mysqldump的方式把数据导出为.sql文件,然后在从库上创建同名数据库,把数据导入
[root@server3 mysql5.1.58]# tar zcf data.tar.gz data/ [root@server3 mysql5.1.58]# scp data.tar.gz root@192.168.100.247:/usr/local/mysql5.1.58/
2.设置从库(在从服务器上操作)
以下操作,如果没有指定在主服务器上操作的,都是在从服务器上操作
1)备份从库data目录,把从主库复制过来的data.tar.gz直接解压出来
[root@server2 mysql5.1.58]# mv data data_bak [root@server2 mysql5.1.58# tar zxf data.tar.gz
2)解锁主库表(在主服务器上操作)
mysql>unlock tables;
3)启动从库mysql服务
编辑从库/usr/local/mysql5.1.58/my.cnf ,找到server-id把值修改为2 server-id = 2 [root@server2 mysql5.1.58]# /usr/local/mysql5.1.58/bin/mysqld_safe --defaults-file=/usr/local/mysql5.1.58/my.cnf --user=mysql &
4)在从库上设置同步
设置连接MASTER MASTER_LOG为主库的File,MASTER_LOG_POS为主库的Position mysql> slave stop; mysql> change master to master_host='192.168.100.248',master_user='admin',master_password='123456',master_log_file='binlog.000006',master_log_pos=278; mysql> slave start; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.248 Master_User: admin Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000006 Read_Master_Log_Pos: 278 Relay_Log_File: server2-relay-bin.000002 Relay_Log_Pos: 248 Relay_Master_Log_File: binlog.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes //这2项要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: 278 Relay_Log_Space: 405 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
5)在主库上的bookfm数据库上建立book表
mysql> create table book (id int,name char(10)) engine=MYISAM; mysql> insert into book values(1,'a');
6)在从库上查询
mysql> select * from book; +------+------+ | id | name | +------+------+ | 1 | a | +------+------+ 1 row in set (0.00 sec) 可以查询到,说明配置成功
相关文章推荐
- MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误
- MySQL主从复制介绍
- MyCat 在 MySQL 主从复制基础上实现读写分离
- MySQL主从复制的原理及配置方法(比较详细)
- MySql学习之主从复制
- MYSQL 主从复制---简单易学
- MySql 主从复制配置之GTID
- mysql主从复制及自动备份脚本
- mysql主从数据库复制
- yii2操作数据库 mysql 读写分离 主从复制
- mysql主从复制配置
- Mysql主从复制
- MySQL 主从复制,mysql主从复制
- 详解MySQL主从复制读写分离搭建
- 小马分享(如何使用Spring实现读写分离(MySQL实现主从复制))
- 关于解决 MySQL 数据库主从复制延迟的问题
- mysql主从复制环境搭建
- Mysql主从复制和读写分离方案分析
- 关于mysql主从复制error_code 1593的小异常BUG
- Mysql之主从复制原理