Posts

How to Configure MySQL Master-Master Replication

Step 1: Install MySQL Server

Install MySQL Server on both machines.

# apt-get update
# apt-get install mysql-server mysql-client

Step 2: Configure MySQL Servers

Edit the /etc/mysql/my.cnf file on both machines. Add or modify the following values:

Server 1:

# vi /etc/mysql/my.cnf
        server_id           = 1
        log_bin             = /var/log/mysql/mysql-bin.log
        log_bin_index       = /var/log/mysql/mysql-bin.log.index
        relay_log           = /var/log/mysql/mysql-relay-bin
        relay_log_index     = /var/log/mysql/mysql-relay-bin.index
        expire_logs_days    = 10
        max_binlog_size     = 100M
        log_slave_updates   = 1
        auto-increment-increment = 2
        auto-increment-offset = 1

Server 2:

# vi /etc/mysql/my.cnf
        server_id           = 2
        log_bin             = /var/log/mysql/mysql-bin.log
        log_bin_index       = /var/log/mysql/mysql-bin.log.index
        relay_log           = /var/log/mysql/mysql-relay-bin
        relay_log_index     = /var/log/mysql/mysql-relay-bin.index
        expire_logs_days    = 10
        max_binlog_size     = 100M
        log_slave_updates   = 1
        auto-increment-increment = 2
        auto-increment-offset = 2

Step 3: Modify my.cnf file with bind-address

Edit or add bind-address in my.cnf

# vi /etc/mysql/my.cnf    	
        bind-address    = x.x.x.x

Step 4: Restart MySQL Server

After inserting the above entries restart mysql server.

# service mysqld restart

Step 5: Create Replication Users

Connect with mysql server as root

# mysql -u root -p

Configure the replication users on each Linode. Replace x.x.x.x with the private IP address of the opposing Linode, and password with a strong password:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'x.x.x.x' IDENTIFIED BY 'password';

Run the following command to test the configuration. Use the private IP address of the opposing Linode:

 mysql -ureplication -p -h x.x.x.x -P 3306

Step 6: Configure Database Replication

Server 1:

While logged into MySQL on Server 1, query the master status:

mysql> SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000001 |      120 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)

Server 2:

On Server 2 at the MySQL prompt, set up the slave functionality for that database. Replace x.x.x.x with the private IP from the first server. Also replace the value for master_log_file with the file value from the previous step, and the value for master_log_pos with the position value.

mysql> SLAVE STOP;
mysql> CHANGE MASTER TO master_host='x.x.x.x', master_port=3306, master_user='replication', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=106;
mysql> SLAVE START;

Step 7: Configure Database Replication Vice Versa

Server 2:

On Server 2, query the master status. Again note the file and position values.

mysql> SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000001 |      160 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)

Server 1:

Set the slave database status on Server 1, replacing the same values swapped in step 2 with those from the Server 2.

mysql> SLAVE STOP;
mysql> CHANGE MASTER TO master_host='x.x.x.x', master_port=3306, master_user='replication', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=160;
mysql> SLAVE START;

Step 8: Create Database

Test by creating a database and inserting a row:

Server 1:

mysql> create database test;
mysql> create table test.books (`id` varchar(10));

Server 2:

mysql> show tables in test;

How to Delete Database in mysql through Linux Shell

There are 2 methods in order to delete the database in mysql through Linux Shell.

Method 1:

Delete through mysqladmin Command

You can delete the database without logging in mysql database;

# mysqladmin -u[username] -p[password] drop [database]

Method 2:

Step 1: Login in mysql Database

First we’ll login to the MySQL server from the command line with the following command:

# mysql -u root -p
mysql>

Step 2: Delete Database

mysql> DROP DATABASE test_database;

How to Install WordPress in Linux

Step 1: Prerequisites

Step 2: Create WordPress Database

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 834
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)

mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on wordpress.* to wordpress@'localhost' identified by 'password';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

Step 3: Install WordPress

To Install wordpress package use the yum command i.e.

# yum --enablerepo=epel -y install wordpress

Step 4: Configure WordPress

Edit the wp-config.php file to configure wordpress

# vi /etc/wordpress/wp-config.php

define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', 'password');
define('WPLANG', 'ja');
# vi /etc/httpd/conf.d/wordpress.conf
Allow from all
# mkdir /usr/share/wordpress/wp-content/languages
# wget -P /usr/share/wordpress/wp-content/languages \
http://svn.automattic.com/wordpress-i18n/ja/tags/`rpm -q wordpress | cut -d"-" -f2`/messages/ja.mo \
http://svn.automattic.com/wordpress-i18n/ja/tags/`rpm -q wordpress | cut -d"-" -f2`/messages/admin-ja.mo \
http://svn.automattic.com/wordpress-i18n/ja/tags/`rpm -q wordpress | cut -d"-" -f2`/messages/admin-network-ja.mo \
http://svn.automattic.com/wordpress-i18n/ja/tags/`rpm -q wordpress | cut -d"-" -f2`/messages/continents-cities-ja.mo

Step 5: Restart apache Server

Restart apache server:

# service httpd restart
OR
# /etc/rc.d/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]

How to Enable mysql slow query logs in Debian/Ubuntu

Step 1: Modify /etc/mysql/my.cnf File

To enable mysql slow query logs add the lines in my.cnf file.

#vi /etc/mysql/my.cnf

log_slow_queries = /var/log/mysql/mysql-slow.log
long_query_time = 2
log-queries-not-using-indexes

Step 2: Restart mysql service

After inserting the above entries restart mysql server.

#service mysqld restart

How to Uninstall MySQL Server in Debian/Ubuntu

Step 1: Remove mysql Server

We can use apt-get command in order to remove both MySQL server and client in Debian / Ubuntu:

# apt-get --purge remove mysql-client mysql-server mysql-common
# apt-get autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-3.2.0-31-virtual linux-headers-3.2.0-31
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
libdbd-mysql-perl* libmysqlclient18* mysql-client* mysql-client-5.5* mysql-common* mysql-server*
mysql-server-5.5*
0 upgraded, 0 newly installed, 7 to remove and 0 not upgraded.
After this operation, 67.5 MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 105097 files and directories currently installed.)
Removing mysql-server ...
Removing mysql-server-5.5 ...
mysql stop/waiting
Purging configuration files for mysql-server-5.5 ...
Removing mysql-client ...
Removing mysql-client-5.5 ...
Removing libdbd-mysql-perl ...
Removing libmysqlclient18 ...
Purging configuration files for libmysqlclient18 ...
Removing mysql-common ...
Purging configuration files for mysql-common ...
dpkg: warning: while removing mysql-common, directory '/etc/mysql' not empty so not removed.
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

Step 2: Delete mysql Directory

Also delete the mysql directory

rm -rf /etc/mysql/

How to Install phpmyadmin in CentOS / RedHat / Fedora

Step 1: Prerequisites

Step 2: Download Package

Download rpm package for CentOS / RedHat /Fedora

# rpm -ivh http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/i386/epel-release-6-8.noarch.rpm
# yum install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
# yum check-update

Step 3: Install phpmyadmin

# yum install phpMyAdmin

Step 4: Reload Apache Service

# service httpd restart

Step 5: Check on Browser

Open your favorite browser and Hit the  URL:http://192.168.1.1/phpMyAdmin

Troubleshooting

»  #2002 – Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
The server is not responding (or the local server’s socket is not correctly configured).
( This means your mysql server service is stopped , you must start the service  “service mysql start”)

» You don’t have permission to access /phpMyAdmin/ on this server.
Open /etc/httpd/conf.d/phpMyAdmin.conf file and find the lines “Deny from All” and comment those lines and restart httpd service

vi /etc/httpd/conf.d/phpMyAdmin.conf

» If you are installing phpmyadmin on Redhat 7 you may face this issue:
Error: Package: php-php-gettext-1.0.11-10.el7.noarch (epel)
Requires: php-mbstring
Error: Package: php-tcpdf-6.0.091-1.el7.noarch (epel)
Requires: php-bcmath
Error: Package: phpMyAdmin-4.2.8.1-2.el7.noarch (epel)
Requires: php-mbstring >= 5.3.0
Error: Package: php-tcpdf-6.0.091-1.el7.noarch (epel)
Requires: php-mbstring
You could try using –skip-broken to work around the problem
You could try running: rpm -Va –nofiles –nodigest

In order to resolve this issue follow the following steps:

# wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/7/x86_64/yum-plugin-replace-0.2.7-1.ius.el7.noarch.rpm

Install yum replace package and replace entire PHP stack

# rpm -i yum-plugin-replace-0.2.7-1.ius.el7.noarch.rpm 
# yum replace php --replace-with php56u

How to Export & Import MySQL database in Linux

Export MySQL database

To export mysql database, use the mysqldump command.

# mysqldump -u root -p database-name > backup.sql
e.g
# mysqldump -u test -p abc > abc_backup.sql

Import MySQL database

To import mysql database, use the mysql command.

# mysql -u root -p database-name < backup.sql
e.g
# mysql -u test -p abc < abc_backup.sql

How to Install MySQL Server on CentOS / RHEL / FEDORA

Introduction

MySQL Community Edition is a freely downloadable version of the world’s most popular open source database that is supported by an active community of open source developers and enthusiasts. MySQL is a popular choice of database for use in web applications, and is a central component of the widely used LAMP open source web application software stack.

By Default Port # 3306

Step 1: Prerequisite

# yum install wget

Download the rpm package, which will create a yum repo file for MySQL Server installation.

# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

Now install the downloaded rpm package by using rpm command.

# rpm -ivh mysql-community-release-el7-5.noarch.rpm

Step 2: MySQL Server Installation

Execute the yum command in order to download mysql-server:

# yum install mysql-server

Step 3: Restart MySQL Server

To start MySQL Service, run command

# systemctl start mysqld

Step 4: MySQL Server Secure Installation

In order to reset the root password and make the mysql-server secure execute the following command and follow the steps:

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): [Press Enter]
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Step 5: Create database in MySQL

To create a new database in MySQL, following are the steps we have to follow:

# mysql -u root -p

mysql> CREATE DATABASE test;
mysql> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'new-password';
mysql> GRANT ALL ON test.* TO testuser@localhost IDENTIFIED BY 'new-password';
mysql> FLUSH PRIVILEGES;
mysql> quit