How to Set Up Replication

.1 Setting the Replication Master Configuration

/etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /var/lib/mysql
port = 3306
#socket = /var/lib/mysql/mysql.sock
user=mysql
bind_address = ::

#bin log conf
log_bin = mysql-bin
binlog_checksum = NONE
binlog_format = ROW
server-id = 2
innodb_flush_log_at_trx_commit=1
sync_binlog=1

2 Setting the Replication Slave Configuration

/etc/my.cnf

basedir = /usr/local/mysql
datadir = /var/lib/mysql
port = 3306
server_id = 3
# socket = .....
user=mysql
relay-log=mysqld-relay-bin
report-host=hostname
 

3 Creating a User for Replication In master

mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';

4 Obtaining the Replication Master Binary Log Coordinates

To obtain the master binary log coordinates, follow these steps:

  1. Start a session on the master by connecting to it with the command-line client, and flush all tables and block write statements by executing the FLUSH TABLES WITH READ LOCK statement:

    mysql> FLUSH TABLES WITH READ LOCK;
    

    For InnoDB tables, note that FLUSH TABLES WITH READ LOCK also blocks COMMIT operations.

    Warning

    Leave the client from which you issued the FLUSH TABLES statement running so that the read lock remains in effect. If you exit the client, the lock is released.

  2. In a different session on the master, use the SHOW MASTER STATUS statement to determine the current binary log file name and position:

    mysql > SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000003 | 73       | test         | manual,mysql     |
    +------------------+----------+--------------+------------------+
    

    The File column shows the name of the log file and Position shows the position within the file. In this example, the binary log file is mysql-bin.000003 and the position is 73. Record these values. You need them later when you are setting up the slave. They represent the replication coordinates at which the slave should begin processing new updates from the master.

    If the master has been running previously without binary logging enabled, the log file name and position values displayed by SHOW MASTER STATUS or mysqldump --master-data will be empty. In that case, the values that you need to use later when specifying the slave's log file and position are the empty string ('') and 4.

5 Creating a Data Snapshot Using mysqldump

shell> mysqldump --all-databases --master-data > dbdump.db

6 Setting Up Replication with Existing Data

  1. Start the slave, using the --skip-slave-start option so that replication does not start.

  2. Import the dump file:

    shell> mysql < fulldb.dump
    
c. 
mysql> CHANGE MASTER TO
    ->     MASTER_HOST='master_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;

 

d. Start the slave threads:

mysql> START SLAVE;

http://dev.mysql.com/doc/refman/5.6/en/replication-howto.html

猜你喜欢

转载自ylzhj02.iteye.com/blog/2168442