Engine23
blogBannerImage
By Engine23

Setting up a NEW MySQL Master Slave replication

Step 1: SSH into both servers

Step 2: Install mysql 

sudo apt-get install mysql-server -y

Step 3: Master Server update to my.cnf. If your my.cnf has 

#server-id              = 1
#log_bin                = /var/log/mysql/mysql-bin.log

Simply remove the # and then restart your mysql server and if not, just add them it should look like this:

server-id              = 1
log_bin                = /var/log/mysql/mysql-bin.log

From MySQL: For the greatest possible durability and consistency in a replication setup using InnoDB with transactions, you should use innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the master my.cnf file.

Here is what the Master my.cnf might look like

    #
    # 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 # user= mysql pid-file= /var/run/mysqld/mysqld.pid socket= /var/run/mysqld/mysqld.sock port= 3306 basedir= /usr datadir= /var/lib/mysql tmpdir= /tmp lc-messages-dir= /usr/share/mysql 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= 127.0.0.1 # # * 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
# Error log - should be very few entries. # 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 = dbNameToBackup
# From MySQL: http://dev.mysql.com/doc/refman/5.0/en/replication-howto-masterbaseconfig.html
# For the greatest possible durability and consistency in a replication setup using InnoDB with transactions, you should use innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the master my.cnf file.
innodb_flush_log_at_trx_commit=1 sync_binlog=1
#binlog_do_db= include_database_name #binlog_ignore_db= include_database_name # # * 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/

Step4: Stop MySQL 

sudo stop mysql

Step 5: Start MySQL

sudo start mysql

Step 6: Setup slave my.cnf

Now stop mysql

sudo stop mysql

Now start mysql

sudo start mysql

Step 7: Now setup a database on the new Master DB server

CREATE DATABASE `some_database_name`;

Step 8: Create the slave user for the database

CREATE USER 'someuser'@'%.some_database_name' IDENTIFIED BY 'somepassword';

Step 9: On the Master DB display the Master log info

mysql > SHOW MASTER STATUS;

STep 10: On the master, release the read lock:

mysql > UNLOCK TABLES;

Step 11: Stop the slave:

mysql> STOP SLAVE;

Step 11: On the slave, Execute a CHANGE MASTER TO statement to set the master replication server configuration.

mysql> CHANGE MASTER TO 
    -> MASTER_HOST='192.168.1.1',
    -> MASTER_USER='someuser',
    -> MASTER_PASSWORD='somepassword',
    -> MASTER_LOG_FILE='mysql-bin.000002',
    -> MASTER_LOG_POS=329;

Step 12: Start the slave

START SLAVE

Thats pretty much about it.  You may need to verify that your two databases can communicate with one another.  You can run this in mysql terminal:

MYSQL> SHOW SLAVE STATUS \G;

What that \G does is make it human readable.  Here is an example of the two databases NOT being able to communicate with one another so you can look out for this potential issue:

mysql> SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.38.120
                  Master_User: hlarepl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 329
               Relay_Log_File: mysqld-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Connecting
            Slave_SQL_Running: 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: 329
              Relay_Log_Space: 107
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error connecting to master 'hlarepl@192.168.38.120:3306' - retry-time: 60  retries: 86400
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

Another way to test communication is to try to connect to the remote database via old school command line:

mysql -h 192.168.1.101 -u someusername -p'somepassword'

The -h in the above example is the IP for the remote database!

If you dont see that example your probably good to go!

NOTES:

I found that creating a mysqldump of the MASTER and importing it into the slave works best.

Also, in the my.cnf for the SLAVE, you should enter:

replicate-do-db         = hla_magento
read-only               = 1