<\/span><\/h4>\nTo configure a master to use binary log file based replication, Configure binary logging first and include a unique server ID. Once done these steps restart MySQL server. Binary logging is enabled by default (log_bin variable is set to ON). It’s always a best practice to create binary log files with a non-default base name to avoid confusion as your database infrastructure grows. To configure binary logging, first shutdown the MySQL and edit my.cnf or my.ini<\/p>\n
root@DA:\/home\/shiv\/sakila# vi \/etc\/mysql\/my.cnf\r\n<\/pre>\n!includedir \/etc\/mysql\/conf.d\/\r\n\r\n!includedir \/etc\/mysql\/mysql.conf.d\/\r\n\r\n[mysqld]\r\n\r\nserver-id = 900\r\n\r\nlog-bin = mysql-bin\r\n\r\nbind-address = 192.168.56.13\r\n\r\nbinlog-do-db = sakila\r\n<\/pre>\nTo guarantee the ultimate transaction durability and consistency in the replication setup please confirm innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the master my.cnf file. You must also confirm skip-networking option is not enabled on your replication master.<\/p>\n
<\/span>Configure MySQL slave (192.168.56.23)<\/span><\/h4>\nMySQL slave must have a unique server ID, You must restart MySQL server after setting (this must be done in [mysqld] section of the configuration file) this in my.cnf . A slave is not required to have binary logging enabled to setup an simple MySQL master-slave \u00a0replication but if you are building an relay-slave MySQL replication where server A serves as the master for slave B, and B serves as the master for slave C then slave B binary logging must be enabled. In addition to binary logging, this kind of replication topology requires the –log-slave-updates option to be enabled. You can disable binary logging on slave by configuring –skip-log-bin and –skip-log-slave-updates options in my.cnf<\/p>\n
root@DBA:\/home\/shiv# vi \/etc\/mysql\/my.cnf\r\n<\/pre>\n!includedir \/etc\/mysql\/conf.d\/\r\n\r\n!includedir \/etc\/mysql\/mysql.conf.d\/\r\n\r\n[mysqld]\r\n\r\nserver-id=901<\/pre>\n<\/span>Creating user for MySQL replication<\/span><\/h4>\nThe slave connect to master using MySQL username and password, There must a user account on the master that slave can use to connect. This user account created in the master must be granted with REPLICATION SLAVE privilege. You may create different account for each slave or connect to master using the same account for each slave. Though it’s not compulsory to create user account specifically for replication, You must be aware that the replication user name and password is stored in plain text in the master info data dictionary table mysql.slave_master_info so from an MySQL security perspective it is recommended to have a dedicated user account for successful replication.<\/p>\n
Please create the user below in MySQL master – 192.168.56.13<\/p>\n
mysql> CREATE USER 'repl'@'192.168.56.23' IDENTIFIED BY 'repl';\r\nmysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.56.23';\r\n<\/pre>\n<\/span>\u00a0Collecting MySQL replication Master Binary Log Coordinates<\/span><\/h4>\nMySQL slave must be configured to start replication from the correct point, To do this task successfully you must note down master’s current coordinates with its binary log info. To collect MYSQL master binary log coordinates please FLUSH TABLES WITH READ LOCK, which blocks COMMIT operations for InnoDB tables (data consistency in replication is very important )Please run the below SQL in MySQL master – 192.168.56.13<\/p>\n
mysql> FLUSH TABLES WITH READ LOCK;\r\n<\/pre>\nNow from different MySQL session on the master run SHOW MYSQL STATUS statement to collect current binary log file name and it’s position<\/p>\n
mysql> SHOW MASTER STATUS;\r\n\r\n+------------------+----------+--------------+------------------+-------------------+\r\n\r\n| File \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |\r\n\r\n+------------------+----------+--------------+------------------+-------------------+\r\n\r\n| mysql-bin.000001 |\u00a0 \u00a0 \u00a0 771 | sakila \u00a0 \u00a0 \u00a0 |\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 | \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |\r\n\r\n+------------------+----------+--------------+------------------+-------------------+\r\n\r\n1 row in set (0.00 sec)<\/pre>\nThis is the position (Position 771) from which the slave database will start replication, Take a note the number as you need them while setting up slave for MySQL replication. Now you are ready to proceed with exporting your database using mysqldump from a new window \u00a0( please confirm you are not typing this command from MySQL, It’s done from bash shell )<\/p>\n
mysqldump -u root -p --opt sakila > sakila-backup.sql\r\n<\/pre>\nNow unlock tables in the master<\/p>\n
UNLOCK TABLES;\r\n<\/pre>\n<\/span>Restore the mysqldump backup to slave (192.168.56.23)<\/span><\/h4>\nmysql -u root -p sakila < sakila-backup.sql\r\n<\/pre>\n<\/span>Connecting slave to master<\/span><\/h4>\nRun the script below from MySQL terminal in the slave<\/p>\n
CHANGE MASTER TO \r\nMASTER_HOST='192.168.56.13', \r\nMASTER_USER='repl', \r\nMASTER_PASSWORD='repl', \r\nMASTER_LOG_FILE='mysql-bin.000001', \r\nMASTER_LOG_POS= 771;\r\n<\/pre>\n