mysql主从配置

1、配置主库(master)

1.编辑mysql master的配置文件(/etc/mysql/my.cnf):

a)将[mysqld]配置节中的server-id置为1

b)同时打开log_bin(即将log_bin前边的#去掉,确保此文件可写)

c)改侦听地址bind-address = 本机IP

d)如果配置文件做了修改,需重启数据库

e)若是首次安装配置主库,需建立授权账号并执行:flush privileges;


2.查看master的日志文件(File)和偏移量(Position):

a)进入到mysql控制台:mysql �Curoot

b)执行命令:show master status;

c)将file和Position的值记录下来,后面进行从服务器操作的时候需要使用


2、配置从库(slave)

1.安装从库,编辑mysql slave的配置文件:

a)将[mysqld]配置节中的server-id置为2

b)同时打开log_bin(即将log_bin前边的#去掉,确保此文件可写)

c)改侦听地址bind-address = 本机IP


2.重启数据库,建立和master一样(包括密码)的授权账号,并执行:>flush privileges;

 

3、导出主库数据

1.如果数据库中的表很多都是innodb引擎,所以不能直接拷贝主数据库的文件,这里需要在从库所在服务器上,将数据导出(这里我们将数据导入到masterdata.sql):

 mysqldump -h <master_ip> -u <username> -p <passwd> --master-data=1 --opt -R --databases  <mysql>  <db_1>  <db_2>  <db_3>  <db_4>  ......  > masterdata.sql

注:--databases 也即-B 

2.将master数据导入从库:mysql -uroot < masterdata.sql &


4、配置主从连接信息

在从库的mysql控制台下执行:

 mysql> change master to//开始更改从库的master参数

     -> master_host='<master_ip>',//从库的master的ip

     -> master_user='<username>',//用户名为授权账户,主从必相同

     -> master_password='<passwd>',//主从密码一定要相同且正确

     -> master_port='3306',//端口一定是3306

     -> master_log_file='<mysql-bin.000022>',//此值为master的file值

     -> master_log_pos=<2919783>;//此值为master的Position值

命令如下:

change master     to master_host='<master_ip>',master_user='<username>',master_password='<passwd>',master_port='3306',master_log_file='<mysql-bin.000022>',master_log_pos=<2919783>;


5、启动从库

1.启动从库:>start slave;

2.再查看主从库是否同步:show slave status\G;

3.必须保证Slave_IO_Running和Slave_SQL_Running这两个值都为yes,否则逐一排查。

4.在进行mysql主从同步时,建议把业务暂停。


6.附:配置文件

#

# The MySQL database server configuration file.

#

# You can copy this to one of:

# - "/etc/mysql/my.cnf" to set global options,

# - "~/.my.cnf" to set user-specific options.

# One can use all long options that the program supports.

# Run program with --help to get a list of available options and with

# --print-defaults to see which it would actually understand and use.

#

# For explanations see

# http://dev.mysql.com/doc/mysql/en/server-system-variables.html


# This will be passed to all mysql clients

# It has been reported that passwords should be enclosed with ticks/quotes

# escpecially if they contain "#" chars...

# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

[client]

port= 3306

socket= /var/run/mysqld/mysqld.sock


# Here is entries for some specific programs

# The following values assume you have at least 32M ram


# This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]

socket= /var/run/mysqld/mysqld.sock

nice= 0


[mysqld]

#

# * Basic Settings

skip-name-resolve

innodb_buffer_pool_size = 2048M

这个参数主要作用是缓存innodb表的索引,数据,插入数据时的缓冲

默认值:128M

专用mysql服务器设置的大小: 操作系统内存的70%-80%最佳

innodb_flush_log_at_trx_commit = 2

default-character-set=utf8

lower_case_table_names = 1

#


#

# * IMPORTANT

#   If you make changes to these settings and your system uses apparmor, you may

#   also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.

#


user= mysql

socket= /var/run/mysqld/mysqld.sock

port= 3306

basedir= /usr

datadir= /var/lib/mysql

tmpdir= /tmp

skip-external-locking

#

# Instead of skip-networking the default is now to listen only on

# localhost which is more compatible and is not less secure.

bind-address= 192.168.0.140

#

# * Fine Tuning

#

key_buffer= 16M

max_allowed_packet= 16M

thread_stack= 192K

thread_cache_size       = 8

# This replaces the startup script and checks MyISAM tables if needed

# the first time they are touched

myisam-recover         = BACKUP

#max_connections        = 100

#table_cache            = 64

#thread_concurrency     = 10

#

# * Query Cache Configuration

#

query_cache_limit= 1M

query_cache_size        = 16M

#

# * Logging and Replication

#

# Both location gets rotated by the cronjob.

# Be aware that this log type is a performance killer.

# As of 5.1 you can enable the log at runtime!

#general_log_file        = /var/log/mysql/mysql.log

#general_log             = 1


log_error                = /var/log/mysql/error.log


# Here you can see queries with especially long duration

#log_slow_queries= /var/log/mysql/mysql-slow.log

#long_query_time = 2

#log-queries-not-using-indexes

#

# The following can be used as easy to replay backup logs or for replication.

# note: if you are setting up a replication slave, see README.Debian about

#       other settings you may need to change.

server-id= 1

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

expire_logs_days= 10

max_binlog_size         = 100M

#binlog_do_db= include_database_name

binlog_ignore_db= db_lobbylog

binlog_ignore_db= mysql

#

# * InnoDB

#

# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.

# Read the manual for more InnoDB related options. There are many!

#

# * Security Features

#

# Read the manual, too, if you want chroot!

# chroot = /var/lib/mysql/

#

# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".

#

# ssl-ca=/etc/mysql/cacert.pem

# ssl-cert=/etc/mysql/server-cert.pem

# ssl-key=/etc/mysql/server-key.pem




[mysqldump]

quick

quote-names

max_allowed_packet= 16M


[mysql]

#no-auto-rehash# faster start of mysql but no tab completition


[isamchk]

key_buffer= 16M


#

# * IMPORTANT: Additional settings that can override those from this file!

#   The files must end with '.cnf', otherwise they'll be ignored.

#

!includedir /etc/mysql/conf.d/


二.

MYSQL数据库复制跳过错误

在MYSQL进行Replication的时候,有时候会由于主从端的POS点不同,导致复制的SQL进程出现错误,从而导致主从复制失败。比如在主端复制一个ID是100的到从端,而此时由于某种原因从端已经有了ID=100的记录,这时进行INSERT操作的时候,就会主键重复,插入失败。这时候需要跳过这条错误。方法如下

1:停止SLAVE 服务

mysql> STOP SLAVE;

2:设置跳过的EVENT个数

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;

3:启动SLAVE服务

mysql> START SLAVE;

 

下面阐述下N的意思,转帖

大家都知道,当slave出现错误时,可以通过SET GLOBAL sql_slave_skip_counter = N来跳过错误,但是这个N,又真正代表什么呢,开始时,
理解错了,以为对于事务型,N代表N个事务,而非事务型,代表一条sql 语句。后经过linuxtone曹哥指导发现,其实并不是这么回事
文档中有介绍说(http://dev.mysql.com/doc/refman/ ... e-skip-counter.html):
This statement skips the next N events from the master
即他是跳过N个events,这里最重要的是理解event的含义
在mysql中,对于sql的 binary log 他实际上是由一连串的event组成的一个组,即事务组。
我们在master上可以通过
SHOW BINLOG EVENTS 来查看一个sql里有多少个event。
通过例子来说明下,真正的event的含义:
在slave上
show slave status
Last_Errno: 1062
  Last_Error: Error 'Duplicate entry '193' for key 'PRIMARY'' on query. Default database: 'ssldb'. Query: 'insert slave_no_skip1  values (193,'y10')'
Skip_Counter: 0
在 master 上,执行
mysql> SHOW BINLOG EVENTS in 'mysql-bin.000010' from 46755013;
+------------------+----------+------------+-----------+-------------+--------------------------------------------------------+
| Log_name         | Pos      | Event_type | Server_id | End_log_pos | Info                                                   |
+------------------+----------+------------+-----------+-------------+--------------------------------------------------------+
| mysql-bin.000010 | 46755013 | Query      |         1 |    46755082 | BEGIN                                                  |
1| mysql-bin.000010 | 46755082 | Query      |         1 |    46755187 | use `ssldb`; insert slave_no_skip1  values (193,'y10') |
2| mysql-bin.000010 | 46755187 | Xid        |         1 |    46755214 | COMMIT /* xid=4529451 */                               |
3| mysql-bin.000010 | 46755214 | Query      |         1 |    46755283 | BEGIN                                                  |
4| mysql-bin.000010 | 46755283 | Query      |         1 |    46755387 | use `ssldb`; insert slave_no_skip1 values (194,'y11')  |
5| mysql-bin.000010 | 46755387 | Xid        |         1 |    46755414 | COMMIT /* xid=4529452 */                               |
6| mysql-bin.000010 | 46755414 | Query      |         1 |    46755483 | BEGIN                                                  |
7| mysql-bin.000010 | 46755483 | Query      |         1 |    46755587 | use `ssldb`; insert slave_no_skip1 values (195,'y12')  |
8| mysql-bin.000010 | 46755587 | Xid        |         1 |    46755614 | COMMIT /* xid=4529453 */                               |
9| mysql-bin.000010 | 46755614 | Query      |         1 |    46755683 | BEGIN                                                  |
10| mysql-bin.000010 | 46755683 | Query      |         1 |    46755788 | use `ssldb`; insert slave_no_skip1  values (196,'y13') |
11| mysql-bin.000010 | 46755788 | Xid        |         1 |    46755815 | COMMIT /* xid=4529454 */                               |
12| mysql-bin.000010 | 46755815 | Query      |         1 |    46755884 | BEGIN                                                  |
13| mysql-bin.000010 | 46755884 | Query      |         1 |    46755989 | use `ssldb`; insert slave_no_skip1  values (197,'y14') |
14| mysql-bin.000010 | 46755989 | Xid        |         1 |    46756016 | COMMIT /* xid=4529455 */                               |
15| mysql-bin.000010 | 46756016 | Query      |         1 |    46756085 | BEGIN                                                  |
16| mysql-bin.000010 | 46756085 | Query      |         1 |    46756190 | use `ssldb`; insert slave_no_skip1  values (198,'y15') |
17| mysql-bin.000010 | 46756190 | Xid        |         1 |    46756217 | COMMIT /* xid=4529456 */                               |
18| mysql-bin.000010 | 46756217 | Query      |         1 |    46756286 | BEGIN                                                  |
19| mysql-bin.000010 | 46756286 | Query      |         1 |    46756391 | use `ssldb`; insert slave_no_skip1  values (199,'y16') |
20| mysql-bin.000010 | 46756391 | Xid        |         1 |    46756418 | COMMIT /* xid=4529457 */                               |
21| mysql-bin.000010 | 46756418 | Query      |         1 |    46756487 | BEGIN                                                  |
| mysql-bin.000010 | 46756487 | Query      |         1 |    46756592 | use `ssldb`; insert slave_no_skip1  values (190,'y17') |
| mysql-bin.000010 | 46756592 | Xid        |         1 |    46756619 | COMMIT /* xid=4529458 */                               |
+------------------+----------+------------+-----------+-------------+--------------------------------------------------------+
24 rows in set (0.00 sec)          
   
通过错误可知,他是use `ssldb`; insert slave_no_skip1  values (193,'y10') 这条语句导致错误了
如果我们想跳到最后一条语句“use `ssldb`; insert slave_no_skip1  values (190,'y17')“的话 ,我们必须简单计算下中间有多少个event
很明显,是21,那么我们可以执行SET GLOBAL sql_slave_skip_counter =21(这里你SET GLOBAL sql_slave_skip_counter =19或者20都可以)
在slave 在次执行show slave status查看
Last_Errno: 1062
   Last_Error: Error 'Duplicate entry '190' for key 'PRIMARY'' on query. Default database: 'ssldb'. Query: 'insert slave_no_skip1  values (190,'y17')'
Skip_Counter: 0

可见 他已经如我所愿,跳到use `ssldb`; insert slave_no_skip1  values (190,'y17')这里了。



你可能感兴趣的:(Mysql主从)