MySQL Replication configuration example

Database replication will synchronize databases between two different servers.
Should any record change on primary server, it will be automatically sent to secondary (slave) server.

  • Primary server
  • Edit MySQL config file /etc/my.cnf

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

    log_bin – specifies the location of MySQL binary log, which is used for replication.

    You can limit the binary logging to only one database by specifying parameter binlog_do_db=database .
    However this is not recommended, see (dev.mysql.com )

    Save changes and restart MySQL server.
    [code]service mysql restart[/code]

    Now we should grant privileges on slave user which will be used for replcation.

    [code]mysql -u root -p
    GRANT REPLICATION SLAVE ON *.* TO ‘slave_user’@’%’ IDENTIFIED BY ‘pass’;
    FLUSH PRIVILEGES;
    [/code]

    Now lock and dump DB to the file to transfer it to the second server.

    [code]
    FLUSH TABLES READ LOCK;
    SHOW MASTER STATUS;
    [/code]
    Save “Position” field.

    If you do something in this window database will automatic unlock,
    you should open new window and do it there:

    [code]mysqldump -u root -p -f db1 > db1.sql[/code]
    db1 – name of database, that you want to dump

    Unlock database.
    [code]UNLOCK TABLES;[/code]

    Now it’s time to configure secondary server.

  • Secondary server
  • [code]CREATE DATABASE db1;[/code]

    Import dump from first server:

    [code]mysql -u root -p -f db1 < db1.sql[/code]

    Edit MySQL config file /etc/my.cnf

    [code]server-id=2
    relay-log=/var/log/mysql/mysql-relay-bin.log
    log_bin=/var/log/mysql/mysql-bin.log
    [/code]

    Restart MySQL:
    [code]service mysql restart[/code]

    Server configured and ready for replication.
    To activate replication on slave server execute following command in mysql console:

    [code]CHANGE MASTER TO
    MASTER_HOST=’IP-address or primary server’,
    MASTER_USER=’slave_user’,
    MASTER_PASSWORD=’pass’,
    MASTER_LOG_FILE=’mysql-bin.000001′,
    MASTER_LOG_POS=XXX;[/code]

    Remember “Position” field from “SHOW MASTER STATUS” command and write value in place of XXX.

    And start replacation:
    [code]START SLAVE;[/code]

    Look at status::
    [code]SHOW SLAVE STATUS\G[/code]

    Slave_IO_State: Waiting for master to send event

    To stop replication use:
    [code]STOP SLAVE[/code]

    PS: MySQL uses TCP port 3306 for communication, so it should be open between them.

    Zabbix icmpping not working cnt=0 rcv=0

    After doing a clean install of zabbix-proxy on a CentOS 6 server, all our icmpping / icmppingsec items started to return zero values.

    Zabbix debug log showed following lines:

    [code]
    31790:20140611:073610.004 In process_ping() hosts_count:1
    31790:20140611:073610.004 /tmp/zabbix_proxy_31790.pinger
    31790:20140611:073610.004 10.211.7.245
    31790:20140611:073610.004 /usr/sbin/fping -C5 -p200 -b1024 -t1000 2>&1 &1

    Compiling PHP for Zabbix 2.2 frontend

    To compile PHP for Zabbix 2.2 frontend you will need to use the following keys:

    [code]
    ./configure –enable-mbstring –enable-sockets –with-mysql –with-mysqli –with-ldap –enable-fpm –enable-bcmath –with-gettext –with-xmlrpc –with-openssl –with-mcrypt –with-gd –with-zlib –with-freetype-dir=/usr/include/freetype2 –with-jpeg-dir=/usr/lib
    [/code]

    PHP ini required values (minimal):

    [code]
    memory_limit = 128M
    post_max_size = 16M
    upload_max_filesize = 2M
    max_execution_time = 300
    max_input_time = 300
    session.auto_start = 0
    [/code]

    Zabbix backup script without history data

    Here is simple backup script for Zabbix 2 configuration data (hosts, items, templates etc). It will not backup history/event/trend data, so backup file is relatively small.

    Just replace ZBX_DB and ZBX_USER with Zabbix database name and username.

    [code]
    mysqldump -u ZBX_USER -p ZBX_DB \
    –ignore-table=ZBX_DB.acknowledges \
    –ignore-table=ZBX_DB.alerts \
    –ignore-table=ZBX_DB.auditlog \
    –ignore-table=ZBX_DB.auditlog_details \
    –ignore-table=ZBX_DB.escalations \
    –ignore-table=ZBX_DB.events \
    –ignore-table=ZBX_DB.history \
    –ignore-table=ZBX_DB.history_log \
    –ignore-table=ZBX_DB.history_str \
    –ignore-table=ZBX_DB.history_str_sync \
    –ignore-table=ZBX_DB.history_sync \
    –ignore-table=ZBX_DB.history_text \
    –ignore-table=ZBX_DB.history_uint \
    –ignore-table=ZBX_DB.history_uint_sync \
    –ignore-table=ZBX_DB.trends \
    –ignore-table=ZBX_DB.trends_uint \
    | gzip > zbx_db.sql.gz

    [/code]