Friday, December 5, 2014

How to check MySQL databases size

How to check MySQL databases size

Here is a simple select that will give you the size for each databases in your MySQL instance



SELECT table_schema "db_name", 
sum(data_length+index_length) /1024/1024 "db_size in Mb"
FROM information_schema.tables
GROUP BY 1; 


Thursday, November 27, 2014

how to check global and session wait_timeout and interactive_timeout variables

how to check global and session wait_timeout and interactive_timeout


Here is a quick select to validate global and session timeout variables

view al at once

SELECT 
     @@global.wait_timeout, 
     @@global.interactive_timeout, 
     @@session.wait_timeout, 
     @@session.interactive_timeout;

View global

SELECT 
     @@global.wait_timeout, 
     @@global.interactive_timeout;

View session

SELECT
     @@session.wait_timeout, 
     @@session.interactive_timeout;


to set the variables

Global

set global interactive_timeout=28800;   # default
set global wait_timeout=28800;  # default

Session

set session wait_timeout=28800;
set session interactive_timeout=28800;   # default


Tuesday, November 4, 2014

How to install multiple MySQL instances of the same version on a single server

Overview

This will show you how to install multi MySQL instances of the same version on a single server

How to steps
  • Make sure you created the user and group mysql:mysql 
  • Install the MySQL software of your choice without running the mysql_install_db since we'll be doing it at a later time. On my side I used a tarball instead of rpm or yum so I have better control and also I can install multiple version of MySQL on the same server.
  • make sure you have all your filesystems created for your instances. Let say we want 2 instances mysql1 and mysql2. here the folders I created for my exemple
    • /mysql/product/5.5.36 ($MYSQL_HOME)
    • /mysql/product/5.5.36/config ($MYSQL_CONFIG)
    • /mysql/admin/mysql1/noslog
    • /mysql/admin/mysql1/nossql
    • /mysql/admin/mysql1/pid
    • /mysql/admin/mysql1socket
    • /mysql/admin/mysql1backup
    • /mysql/admin/mysql2/noslog
    • /mysql/admin/mysql2/nossql
    • /mysql/admin/mysql2/pid
    • /mysql/admin/mysql2socket
    • /mysql/admin/mysql2backup
    • /mysql1/data
    • /mysql1/logs
    • /mysql2/data
    • /mysql2/logs
  • Make sure the user and group mysql are owner of all the folders  for mysql and the instances
    • chown -R mysql:mysql /mysql /mysql1 /mysql2
    • chmod -R 775  /mysql /mysql1 /mysql2
  • now create a .cnf file onto $MYSQL__CONFIG and call it mysql_5.5.36.cnf and make sure mysql:mysql have access to it
  • here is a simple exemple
[mysqld_safe]
basedir=/mysql/product/5.5.36
ledir=/mysql/product/5.5.36/bin

[mysqld_multi]
mysqld=mysqld_safe
mysqladmin=/mysql/product/5.5.36/bin/mysqladmin
log=/mysql/admin/mysql_multi/logs/mysql_multi.log

[mysqld1]
basedir=/mysql/product/5.5.36datadir=/mysql1/data
log-bin=/mysql1/logs/log-bin
socket=/mysql/admin/mysql1/socket/mysql1.socket
pid-file=/mysql/admin/mysql1/pid/mysql1.pid
log-error=/mysql/admin/mysql1/noslog/mysql1-error.log
max_binlog_size=100M
binlog-format=MIXED
port=3307
expire_logs_days=5
default-storage-engine=innodb
server-id=5401
skip-slave-start

[mysqld2]
basedir=/mysql/product/5.5.36datadir=/mysql2/data
log-bin=/mysql2/logs/log-bin
socket=/mysql/admin/mysql2/socket/mysql2.socket
pid-file=/mysql/admin/mysql2/pid/mysql1.pid
log-error=/mysql/admin/mysql2/noslog/mysql1-error.log
max_binlog_size=100M
binlog-format=MIXED
port=3308
expire_logs_days=5
default-storage-engine=innodb
server-id=5402
skip-slave-start

  •  Now you have to create your instances
  • cd $MYSQL_HOME
  • ./scripts/mysql_install_db --defaults-file=$MYSQL_HOME/config/mysql_5.5.36.cnf --basedir=$MYSQL_HOME --datadir=/mysql1/data --user=mysql
  • ./scripts/mysql_install_db --defaults-file=$MYSQL_HOME/config/mysql_5.5.36.cnf --basedir=$MYSQL_HOME --datadir=/mysql2/data --user=mysql
  • Now you can start the 2 instances using mysqld_multi
    • mysqld_multi --defaults-file=$MYSQL_CONFIG/mysql_5.5.36.cnf start 1
    • mysqld_multi --defaults-file=$MYSQL_CONFIG/mysql_5.5.36.cnf start 2
  • From that point
    your instances are up and running ad you can secure them
    • cd $MYSQL_HOME
    • ./bin/mysql_secure_installation defaults-file=$MYSQL_HOME/config/mysql_5.5.36.cnf --port=yourinstanceport --socket=yourinstancesocket

How to run mysql and other commands for each instance

To run mysql you have to run the mysql and other command with the --defaults-file, --port and --socket options so you indicate to mysql on wich instance you want to connect

Note

  • I had few issues with some of the command where even if I specified the location of the .cnf that contains the location of the socket. it was not reading it. this is a know issue ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'.In order to fix it  create a soft link then remove it after
Ex: ln -fs /mysql/admin/mysql1/socket/mysql1.socket /tmp/mysql.sock
once done remove it
rm -f /tmp/mysql.sock








Monday, November 3, 2014

How to install a MySQL using a tarball as source

Overview

This will give view an overview on how to install a MySQL using a tarball. This way of installation will also allow you to install multiple version of MySQL on the same version.

Software Download

  • The first step is to download a version that is on tar format instead of the RPM.On my side I'm using generic Linux 64 bit 5.5.40
  • The next step is to prepare your server so you can install MySQL
    • Create a user mysql:mysql
    • Create a filesystem /mysql/product/5.5.40 (MYSQL_HOME) but you can also create folders
    • create the folder $MYSQL_HOME/config (MYSQL_CONFIG)
    • create a small  filesystem /mysql1
    • Create a filesystem /mysql1/data (mysql1 is my instance) (MYSQL_DATA)
    • Create a filesystem /mysql1/logs (MYSQL_BINLOG)
    • Create a filesystem /mysql
    • Create /mysql/admin/mysql1 (MYSQL_ADMIN)
      • scripts
      • logs
      • socket
      • pid
      • backup 
    • create /mysql/admin/mysqld_safe/logs
    • create /mysql/admin
    • Once your filesystems and folders are created make sure you do the following 
      • chown -R mysql;mysql /mysql /mysql1
      • chmod -R 775 /mysql /mysql1
  • copy the .tar.gz file in /mysql/product/5.5.40
  • untar (tar -zxvf)  the content into /mysql/product/5.5.40
  •  At this point the software is installed

 Create the instance (mysql1)

  • create your my.cnf into $MYSQL_HOME/config (I use mysql_5.5.40.cnf)
Here is an exemple of my .cnf file
#############################################33
[mysqld_safe]
basedir=/mysql/product/5.5.36

ledir=/mysql/product/5.5.36/bin

[mysqld_multi]
mysqld=mysqld_safe
mysqladmin=/mysql/product/5.5.36/bin/mysqladmin
log=/mysql/admin/mysql_multi/logs/mysql_multi.log

[mysqld1]
basedir=/mysql/product/5.5.36datadir=/mysql1/data
log-bin=/mysql1/logs/log-bin
socket=/mysql/admin/mysql1/socket/mysql1.socket
pid-file=/mysql/admin/mysql1/pid/mysql1.pid
log-error=/mysql/admin/mysql1/noslog/mysql1-error.log
max_binlog_size=100M
binlog-format=MIXED
port=3307

expire_logs_days=5
default-storage-engine=innodb

server-id=5401
skip-slave-start

##################################

  •  connect as mysql
  • cd $MYSQL_HOME
  • ./scripts/mysql_install_db --defaults-file=$MYSQL_HOME/config/mysql_5.5.36.cnf --basedir=$MYSQL_HOME --datadir=/mysql1/data --user=mysql
  • Your instance is now ready to be started

Start your Application

  • cd $MYSQL_HOME
  • ./bin/mysqld_multi --defaults-file=$MYSQL_CONFIG/mysql_5.5.36.cnf start 1 
  • I highly suggest you run the $MYSQL_HOME/bin/mysql_secure installation 
To stop it
  • ./bin/mysqld_multi --defaults-file=$MYSQL_CONFIG/mysql_5.5.36.cnf stop 1

Note

I discover that there is a bug (
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
)
for some of the tools like mysqladmin or mysqlcheck.
even if you specify the location of the my.cnf with the --defaults-file option you will get an error about not finding /tmp/my.cnf
the solution
  • create a symbolic link 
    • ln -fs $MYSQL_HOME/config/mysql_5.5.36.cnf /tmp/my.cnf
    • run your command
    • if you do not have multiple instance you can leave otherwise remove it after
 

Saturday, November 1, 2014

mysql_secure_installation : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Overview 

This is about
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Solution

I installed mysql 5.5.40 using a tarball on a non standard location

One the software installed and the instance created/started I wanted to secure it using mysql_secure_installation.

If you are not using the default socket /tmp/mysql.sock mysql_secure_installation won't work and you will get the following error
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

In order to fix the issue, there is a simple solution :

1. create a soft link in /tmp for your socket
EX: ln -s /mysql/admin/mysql1/socket/mysql1.sock /tmp/mysql.sock

2. run your mysql_secure_installation

3. once completed remove the soft link
rm -f /tmp/mysql.sock

Problem fixed

Sunday, February 16, 2014

How to avoid errno 1593 in MySQL 5.6

Overview

Here is how to avoid the errno: 1593 in MySQL 5.6
Errno: 1593
Fatal error: The slave I/O thread stops because master and slave
have equal MySQL server UUIDs; these UUIDs must be different for
replication to work.
 
This errno can occur from version 5.6 and later.

Starting 5.6, when starting MySQL the server generate a UUID to help replication teatures.
If the file auto.cnf located in /var/lib/mysql exists it will read it otherwise it will create a new one.

When creating a replica/Slave, you have to do a cold backup of the Master to the Replica/Slave.
Once to done the copy just make sure that you remove the auto.cnf file and you won't have the error.

If you are getting the error,
  •  Stop your MySQL,
  • Remove the auto.cnf file 
  • Restart MySQL.




 

How to change a password in MySQL

Overview

Often, people are wondering how to change a password in MySQL.

How to change the root password

Root Password

  • After first installation
 After the first installation and the first startup the root password should be changed using

mysqladmin -u root password 'new_root_password'

if the password already exists

mysqladmin -u root -p'oldpassword' password newpassword

Once the root password changed you should change it for all root accounts using the mysql_secure_installation
  • Change password using mysqladmin
mysqladmin -u user -p'oldpassword' password 'newpassword'

  • Here how to change a password in MySQL
Make sure you are connected to a database
use mysql;

    • Change the password for all user where user = 'username'

update user set password=PASSWORD('newpassword') where user='username';
flush privileges;

    • Change the password for a specific user
update user set password=PASSWORD('newpassword') where user='username' and host='hostname';
EX:  update user set password=PASSWORD('changeme') where user='user1' and host='%';
update user set password=PASSWORD('changeme') where user='user1' and host='192.168.0.10';

or

set password for useryouwanttochange = PASSWORD ( 'new_password' );






How to implement basic security on your MySQL instance

Overview

The first things you should do when installing MySQL is to secure your installation.

Secure your MySQL installation

Here are few step to perform basic security on your MySQL instance
  • Once you install your MySQL you should run mysql_secure_installation
  • You should change the default port by ediiting my.cnf and change the port number. Default port number is 3306. to change the port number in MySQL, add port=new_port_number in the [mysqld] section in /etc/my.cnf then restart you MySQL after the change.
  • Once this is done you can start your mysql. then you should change the root password 
    • mysqladmin -u root password your_new_password

Friday, February 14, 2014

How to format the output of mysql in command line

Use of switches in command line

Here are some interesting switches that can be helpful..

-N removes header
-s removes separator chars
-r raw output

exemple

if you need to get the result of a count(*)

mysql -uusername -ppassword -N -s -r -e 'select count(*) from user'  

if you get the ERROR : 2003 you may need to add more options like the port number and the socket

mysql --port=$MYSQL_PORT --socket=$MYSQL_SOCKET -uroot -p -Dmysql -h localhost -N -s -r -e 'select count(*) from user' 






Saturday, February 1, 2014

How to backup and restore MySQL

Overview

This post is to help you backup and restore MySQL.
Concerning the backups, this post will mainly talk about it using either cold backups or mysqldump.

Backup MySQL

Cold Backup

Assuming that your MySQL instance is installed on /var/lib/mysql.
doing a cold backup for mysql is very simple. you first shutdown then you backup your instance the file level.

mkdir /dbabackup
cd /var/lib
cp -Rp mysql /dbabackup/. 

If you want you can then tar the /dbabackup/mysql folder
you can use one of those 2 commands

tar -cvf mysql.tar /dbabackup/mysql
or
tar -cvzf mysql.tar.gz /dbabackup/mysql

Backups using mysqldump

Mysqldump has many parameters and if you want to look at them.

Mysqldump infoMySQL Dev site
Mysqldump description from linuxcomend.org

Here are few basic exemples for mysqldump. Please note that by default the --opt option is ON.

mysqldump -uusername -ppassword my_database > my_database.sql
mysqldump -uusername -ppassword --databases my_d1 my_db2 my_db3 > multiple_db.sql
mysqldump -uusername -ppassword --all_databases > all_databases.sql

On my side here are the options that I like to add when backing up databases.
I add the following options
  • --add-drop-database that add the drop database statement for each database.
  • --flush-logs 
  • --master-data or --master-data=1 if the backup is to create a Slave/Replica
  • --master-data=2 if this is mainly a backup of a Master. it still can be used for a Slave/Replica but first make sure you iformation from the show master status; on the master so you can update the Slave/Replica using CHANGE MASTER TO command, then restart the slave.
  • --opt I put --opt even if it is ON by default
so if you are doing a backup that will be used for a stand alone instance or just the master of a Master/Slave.

mysqldump -uusername -ppassword --all-databases --add-drop-database --flush-logs --master-data=2 > full_backup.sql

If you want a backup  to create a Slave/Replica then use the following

mysqldump -uusername -ppassword --all-databases --add-drop-database --flush-logs --master-data=2 > full_backup.sql

Restore MySQL

The restore of a database, multiple databases or the full instance is fairly easy. make sure that there is a drop database in the .sql file so you overwrite the full database(s).

If you are restoring a single database:

mysql -uusername -ppassword -D my_database < my_database.sql

If you are restoring multiple databases:

mysql -uusername -ppassword  < multiple_databases.sql

If you are restoring a FULL backup:

mysql -uusername -ppassword  < full_backup.sql

 If you toke a cold backup using cp -Rp :

service mysql stop
mv /dbabackup/mysql.tar /var/lib
cd /var/lib
rm -Rf mysql
tar -xvf mysql.tar
service mysql start



Thursday, January 30, 2014

How to upgrade MySQL using RPM

Overview

This post will show you a way to upgrade your MySQL from one version to another using RPM packages.
Before using this procedure you need to have downloaded the necessary rpm packages to your server.
I used this procedure to upgrade from 5.4.xx to 5.5.27.
This was also done on a Master/Slave environment so I will give you the additional step I did.

Close your application

Before you start you should stop your application so there is no transaction.

Promote the Slave as a Master

You will have to do the following to promote the Slave to Master
  • Stop the slave process
  • Reset the slave process
  • Change the 'change master command'
  • restart mysql on the slave
stop slave;
reset slave;
change master to '';

service mysql restart

Point your application to the new Master

Now the Slave is started as a Master you could point back your application on this instance while you are upgrading the original Master.

Upgrade the Original Master

Now the Slave is started as a Master you could point back your application on this instance while you are upgrading the original Master.

In order to do an easy upgrade I will recommend to follow those steps.
  • Stop MySQL
  • Do a full backup of the /var/lib/mysql folder
  • Remove all MySQL packages using RPM
  • Validate that you have no more mysql rpm packages installed 
  • Install your new packages
  • Start MySQL
  • Validate the MySQL Version
  • Check your databases using mysqlcheck
  • Upgrade your databases
  • Stop MySQL
  • Start MySQL
  • Check your databases again using mysqlcheck 
  • Check the status

Stop Mysql

service mysql stop

Backup your mysql folder

mkdir /backup
cp -Rp /var/lib/mysql /backup/.

Remove the MySql rpm packages from your server

rpm -qa | grep -i mysql | xargs rpm -e 

Validate that all your mysql packages are removed

rpm -qa | grep -i mysql

Install the new MySQL version using RPM

Go to the folder where your packages are saved
rpm -i MySQL*.*

Note: I prefer to specify the version I want to install just in case I have multiple versions in the same folder.
So i you want to install a specific version here is an exemple
rpm -i MySQL*5.5*

Restart MySQL

service mysql start

Validate the version

mysql_config --version

Check all your databases

mysqlcheck -uroot -pyour_password --all-databases --auto-repair

If everything if good upgrade your databases

mysql_upgrade -uroot -pyour_password

Restart MySQL

service mysql restart

Validate your databases (Without auto-repair)

mysqlcheck -uroot -pyour_password --all-databases

Check the status of the upgraded instance

show master status\G

From that point if you do not have a Master/Slave environment your instance is upgraded and you just have to restart your application.

Upgrade the old Master

This part is only for those who have a Master/Slave environment.

right now only your Slave has been upgraded, so you need to upgrade your Master Environment as well.

To do so the procedure is pretty much the same as the Slave.

Here are main steps for the old master
  • Make sure the apps is still down
  • Stop the old Master instance
  • Backup the /var/lib/mysql on the old master
  • Uninstall the MySQL old RPM packages
  • Check the MySQL packages
  • Install new MySQL packages
  • Start the old Master MySQL
  • check the MySQL Version
  • Check all databases using mysqlcheck and --auto-repair
  • Upgrade all the databases 
  • Check all databases using mysqlcheck
  • Stop MySQL on the old Master
  • Start MySQL on the old Master
  • Check the instance status
From that point since both old master and Slave were synchronized, there should be no need to refresh the old Master from the upgraded Slave.

Here are the details to upgrade the old Master

Make sure the apps is still down

Check that the apps is still down

Stop the updated MySQL instance

service mysql stop

Stop the old Master instance

service mysql stop

Backup the /var/lib/mysql on the old master

mkdir /backup
cp -Rp /var/lib/mysql /backup/.

Uninstall the MySQL old RPM packages

Before uninstalling the MySQL RPM packages, make sure the new one are downloaded on the server
rpm -qa | grep -i mysql \ xargs rpm -e

Check the MySQL packages

rpm -qa | grep -i mysql

Install new MySQL packages

Go  to the folder where your packages are located
rpm -i MySQL*.*

Start the old Master MySQL

service mysql start

Check the MySQL Version

rpm -qa | grep -i mysql

Check all databases using mysqlcheck and --auto-repair

mysqlcheck -uroot -pyour_password --all-databases --auto-repair

Upgrade all the databases 

mysql_upgrade -uroot -pyour_password

Check all databases using mysqlcheck

mysqlcheck -uroot -pyour_password --all-databases

Stop MySQL on the old Master

service mysql stop

Start MySQL on the old Master

service mysql start

Check the instance status

show master status\G

Rebuild the Master/Slave environment

Since the binaries have been upgraded on both servers, the next step will be to rebuild the Master/Slave environment using a copy of the Master.

I already posted a How to create a Maste/Slave.

Here is the link : How to create a Master Slave in MySQL



Wednesday, January 29, 2014

How to upgrade the engine of your databases in MySQL

Overview

Upgrading your MySQL on linux is fairly easy as long as you follow the proper steps
This exemple will take the change to InnoDB as exemple

Backup your database

The first step is to backup Your environment including the my.cnf usually located in /etc

cp /etc/my.cnf /etc/my.cnf.yyymmdd
service mysql stop
cd /backup
cp -Rp /var/lib/mysql .
service mysql start

To backup your databases you can also use my ksh script from one of my posts from my blog

Upgrade each database

the second step is to run the following command for each instance except mysql and the other system databases.

echo 'SHOW TABLES;' \
| mysql -uroot --password=your_password -D your_database \
| awk '!/^Tables_in_/ {print "ALTER TABLE `"$0"` ENGINE = InnoDB;"}' \
| column -t \
| mysql -uroot --password=your_password -D your_database 

Check all the tables in the database

The last step is to validate that all your tables for each database are now InnoDB.
I prefer the second option that is much easier to run

mysql -uroot --password=your_password -D your_database
show table engine;
or

You can use mysqlcheck to check all tables on the database.
mysqlcheck -c your_database  -uroot  -pyour_password

Set InnoDB as the default engine

If you want to set innodb as the default engine you can do it by adding the following parameter in you my.cnf in the [mysqld] section

default-storage-engine=innodb




MySQL Basics

Overview

This Post is to give you the basic commands in mysql so you can do basic administration.
This is based on mysql installed on a linux host

How to start/Stop/Restart MySQL

service mysql start

service mysql stop
service mysql restart 

How to check MySQL instance status

mysql -uroot -pyour_password -e STATUS
 
--------------
mysql  Ver 14.14 Distrib 5.5.27, for Linux (x86_64) using  EditLine wrapper

Connection id:          70
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.5.27-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 14 hours 47 min 44 sec

Threads: 1  Questions: 546080  Slow queries: 0  Opens: 19103  Flush tables: 27  Open tables: 2079  Queries per second avg: 10.252
--------------
 

How to see the MySQL processes in linux

ps -ef | grep -i mysql
 
root     16111     1  0 08:55 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/prodmysql02.pid
mysql    16540 16111  1 08:55 ?        00:12:45 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/prodmysql02.err --pid-file=/var/lib/mysql/prodmysql02.pid --socket=/var/lib/mysql/mysql.sock 

How to connect to the instance mysql

mysql -uusername -pyour_password

How to create a database

The simpliest way is

create database my_database;


or to avoid error message if the database already exist

create database if not exists my_database;


How to drop a database

Just a warning, when dropping a database, the system won't ask you to confirm if you want to drop it.

drop database my_database;


How to connect or change database

use rcinet
or
use rcinet;

How to list the databases in MySQL

show databases;
 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
10 rows in set (0.01 sec)

How to check the size of the databases

This is a select that was given to me by one of my friend when I was looking for this information. I kept it and scripts it.


SELECT table_schema "db_name", 
sum(data_length+index_length) /1024/1024 "db_size in Mb"
FROM information_schema.tables
GROUP BY 1; 
 
+--------------------+---------------+
| db_name            | db_size in Mb |
+--------------------+---------------+
| information_schema |    0.00878906 |
| mysql              |    0.62183380 |
| performance_schema |    0.00000000 |
+--------------------+---------------+
9 rows in set (10.94 sec) 

How to create a table in MySQL

CREATE TABLE my_table (
mycol1 INT(5) NOT NULL DEFAULT 0,
mycol2 CHAR(100) DEFAULT NULL,
PRIMARY KEY (mycol1) 
);

How to show the tables in a database

show tables; 
 
+-------------------------+
| Tables_in_xmas_card2012 |
+-------------------------+
| my_table                |
+-------------------------+
1 row in set (0.00 sec) 

How to create a user in MySQL

create user account1 identified by 'your_password';
grant all privileges on *.* to account1;  

How to list the users in mysql

select user,host from mysql.user; 
 
+-------------+--------------------------------+
| user        | host                           |
+-------------+--------------------------------+
| root        | %                              |
| root        | 127.0.0.1                      |
| dbaadmin    | localhost                      |
| root        | localhost                      |
+-------------+--------------------------------+
15 rows in set (0.02 sec) 

How to show the status of a Master or Slave/Replica in MySQL

show master status\G   This is on a master instance
 
*************************** 1. row ***************************
            File: log-bin.000078
        Position: 64150203
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
 
or
show slave status\G    This is on a Slave/Replica instance 
 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: xxx.xxx.xxx.xxx
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: log-bin.000056
          Read_Master_Log_Pos: 986876833
               Relay_Log_File: mysql_relay-bin.000130
                Relay_Log_Pos: 64787846
        Relay_Master_Log_File: log-bin.000056
             Slave_IO_Running: Yes
            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: 986876833
              Relay_Log_Space: 64972302
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 123
1 row in set (0.00 sec) 

How to flush the data to disk and set the database in read only

flush tables with read lock; 

How to remove the read only on a database

unlock tables; 

How to check and repair a database

mysqlcheck -c your_database  -uroot -pyour_password --auto-repair

Character set in MySQL

Character sets are mainly used to tell MySQL what to store in MySQL dependng of the language. Character set can be defined at 
  • the server level 
  • the database level
  • the table level
  • the column level

show character set;
 
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.03 sec)
 

Collation in MySQL

Collation in MySQL is used for how the data is sort. Each character has a position in the ordering process.

show character set;
 
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.01 sec)
 


Sunday, January 26, 2014

How to install/remove/check MySQL rpm packages

Overview

this post is to help you with MySQL package and rpm on linux boxes.

How to check What MySQL RPMs are installed

If you are using RPM to manage your MySQL components, there is an easy way to check what MySQL components are installed.

rpm –qa | grep –i mysql

How to remove MySQL RPMs

when using RPM to manage your package on your system you can user  the following command to remove the  MySQL packages.

rpm –e MySQL*5.5*

If this command does not work you can use the following.
rpm –qa | grep –i MySQL|xargs rpm –e

How to install a MySQL RPM

first download the RPMs you want to install.
Once you downloaded the packages for a specific version, you can install the packages with the following command

rpm –i MySQL*5.5*
 



Saturday, August 25, 2012

How to Promote a Slave/Replica to Master in MySQL

Overview

This post is to show you how to promote a Slave/Replica on a master/Slave configuration.

1. Stop transactions/writes on the current Master

    When you want to promote a slave to Master, the first step is to set the master to read only, to make sure all transactions are commited and to kill all opened sessions.
    You can stop transactions/writes by running on of the following commands on your Master.
     
        Using the Flush tables.

flush tables with read lock;

        Setting the global READ_ONLY to ON.

show variables like "read_only";
set global read_only=1;   You can also type set global read_only = ON;
show variables like "read_only"; 

    If all transsactions are not gone after few minutes, kill all remaining opened transactions if necessary.

When when moving the master role, I prefer to stop the Primary Master then promote the Slave as Master.

2. Make sure your Slave has the same data as the current Master

       On the current Master if it is not stopped.

  show master status\G
 
*************************** 1. row ***************************
            File: log-bin.000056
        Position: 425307404
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)
 
       On the Slave

show slave status/G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: log-bin.000058
          Read_Master_Log_Pos: 39696180
               Relay_Log_File: mysql_relay-bin.000127
                Relay_Log_Pos: 39696324
        Relay_Master_Log_File: log-bin.000058
             Slave_IO_Running: Yes
            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: 39696180
              Relay_Log_Space: 39696521
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 123
1 row in set (0.00 sec) 

3. Stop the replication on the slave

stop slave;
change master to master_host=’’;
reset slave;

4. check the status on the new master

show master status\G

5. If you have multiple Slaves

If you have a multiple slaves, make sure they are now pointing to the new master.


Friday, August 24, 2012

How to view config variables in MySQL

How to view all variables

show variables;

How to view a specific variable

show variables like ‘%xxxx%’;


Thursday, August 23, 2012

How to create a replication (Master/Slave) in MySQL

Overview

This post is based on my work on Master/Slave environments creation.
I Discover 2 way of creating a Slave/Replica from a Master.
the first way is by using a cold backup of the/var/lib/mysql folder from the Master which means that you will have to have a down time on your application if you want a consistent backup.
The second solution that I found out not long ago during a Slave/Replica crash that I couldn't resync with the master since they were out of sync since about 60 days. That second option for creating a Slave/Replica from a Master is by using a mysqldump backup using to proper options.
So here are the 2 solutions to share.

These 2 procedures assume that both Master and Slave/Replica hosts have already been install with MySQL.

If not you can follow this link to install MySQL How to Install MySQL on linux using RPM

NOTE:
Assuming
master host = 192.168.10.1
slave/replica host = 192.168.10.2 


Create a Slave/Replica using a cold backup (requires applications down time)

1. Create a replication account on the Master

create user repl@'192.168.10.1' IDENTIFIED BY 'yourpassword';
GRANT REPLICATION SLAVE, REPLICATION CLIENT 
        ON *.*
        TO repl@'192.168.10.1' [ IDENTIFIED BY 'yourpassword' ];
GRANT REPLICATION client, REPLICATION CLIENT 
        ON *.*
        TO repl@'192.168.10.1' [ IDENTIFIED BY 'yourpassword' ]; 

2. Modify the /etc/my.cnf file on the Master

under [mysqld] 

expire_logs_days=2 #  purge bin logs older than 2 days
log-bin=/var/lib/mysql/log-bin # bin location (use full path)
server-id=100 # unique id for the master (default is 1)
sync_binlog=1 # to sync the binlogs 

if you are using InnoDB it is recommended to use

innodb-flush-log-at-trx-commit=1 # This will flush the log write
innodb-support-xa=1 # mysql 5.5 and later 

3. Take note of the Master current bin log and position

This information will be needed when setting the Slave/Replica later one.

show master status\G
 
*************************** 1. row ***************************
            File: log-bin.000056
        Position: 425307404
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec) 

4. Restart your Master to enable the new parameters

You need to restart the Master in order to take the new parameters from themy.cnf.

service mysql restart

5. Do a cold backup of the Master

This backup is a cold backup of the /var/lib/mysql folder using the cp command.

service mysql stop
cp -Rp /var/lib/mysql  /tmp/.
cd /tmp
tar -zcvf mysql.tar mysql
rm -Rf /tmp/mysql

6. Restart the master

service mysql start

7. Transfer the backup to the Slave/Replica host using SCP

cd /tmp
scp mysql.tar root@192.168.10.2:/tpm/.

8. Stop MySQL on the slave host (192.168.10.2)

service mysql stop

9. Remove the content of /var/lib/mysql on the slave

I highly suggest that you take a backup of /var/lib/mysql before you erase it.


cd /var/lib/mysql
rm -Rf *

10. Untar the mysql.tar into /var/lib on the Slave

cp /tmp/mysql.tar /var/lib/.
cd /var/lib
tar -xvf mysql.tar
 
NOTE: if you are using 5.6 and later remove the file auto.cnf in /var/lib/mysql/ to avoid errno 1593
Errno: 1593
Fatal error: The slave I/O thread stops because master and slave
have equal MySQL server UUIDs; these UUIDs must be different for
replication to work.


11. Change the /etc/my.cnf on the slave

    under  [mysqld]
slave-skip-errors=1062 # skip duplicate transaction errors
log-bin=/var/lib/mysql/log-bin # log bin location
relay_log=/var/lib/mysql/mysql_relay-bin # Relay log location
log_slave_updates=1
server-id=101 # we usually put the last digits of the slave IP
skip-slave-start # Does not restart replication on instance restart


I recommend that once the Slave/Replica will be created and sync with the Master that you remove the slave-skip-errors=1062 from the my.cnf file then that you restart the Slave/Replica and you restart the replication.

12. Start MySQL on the Slave

service mysql start

13. Configure the replication on the slave

Note: Remember the bin log file name and position from step 3


CHANGE MASTER TO
    MASTER_HOST='192.168.10.1', 
    MASTER_USER='repl',
    MASTER_PASSWORD='yourpassword',
    MASTER_LOG_FILE='binlognamefrompoint3',
    MASTER_LOG_POS=positionfrompoint3;

14. Start the replication on the slave

start slave;

15. Validate the replication

In the result of this command the most important line is Seconds_Behind_Master.
If the value in the line is NULL, this means that your replication with the Master is not working.
If the value is greater than 0, this means that your replication is catching on. if you run this command few time, the value should go down.
If the value is equal to 0, that means that your replication is running and up to date.

show slave status\G 

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: log-bin.000056
          Read_Master_Log_Pos: 982311504
               Relay_Log_File: mysql_relay-bin.000130
                Relay_Log_Pos: 60222517
        Relay_Master_Log_File: log-bin.000056
             Slave_IO_Running: Yes
            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: 982311504
              Relay_Log_Space: 60406973
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 123

Create a Slave/Replica using a mysqldump backup (No down time)

This option is almost identical to the first one except this time you are using a mysqldump backup and there will be no down time on the master.

1. Create a replication account on the Master

create user repl@'192.168.10.2' IDENTIFIED BY 'yourpassword';
GRANT REPLICATION SLAVE, REPLICATION CLIENT 
        ON *.*
        TO repl@'192.168.10.2' [ IDENTIFIED BY 'yourpassword' ];

2. Modify the /etc/my.cnf file on the Master

under [mysqld] 

expire_logs_days=2 #  purge bin logs older than 2 days
log-bin=/var/lib/mysql/log-bin # bin location (use full path)
server-id=100 # unique id for the master (default is 1)
sync_binlog=1 # to sync the binlogs 

if you are using InnoDB it is recommended to use

innodb-flush-log-at-trx-commit=1 # This will flush the log write
innodn-support-xa=1 # mysql 5.5 and later 

3. Take note of the Master current bin log and position

This information will be needed when setting the Slave/Replica later one.

show master status\G
 
*************************** 1. row ***************************
            File: log-bin.000056
        Position: 425307404
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec) 

4. Restart your Master to enable the new parameters

You need to restart the Master in order to take the new parameters from themy.cnf.

service mysql restart

5. Take a backup of the Master using mysqldump

mysqldump -uroot --password=your_password --all-databases  --add-drop-database --flush-logs --master-data=2  > /tmp/mysql_backup.sql
gzip mysql_backup.sql

6. Restart the master

service mysql start

7. Transfer the backup to the Slave/Replica host using SCP

cd /tmp
scp mysql_backup.gz root@192.168.10.2:/tpm/.


8. Unzip the mysql_backup.sql.gz

cd /tmp 
gunzip mysql_backup.sql.gz 

9. Change the /etc/my.cnf on the slave

    under  [mysqld]
slave-skip-errors=1062 # skip duplicate transaction errors
log-bin=/var/lib/mysql/log-bin # log bin location
relay_log=/var/lib/mysql/mysql_relay-bin # Relay log location
log_slave_updates=1
server-id=101 # we usually put the last digits of the slave IP
skip-slave-start # Does not restart replication on instance restart


I recommend that once the Slave/Replica will be created and sync with the Master that you remove the slave-skip-errors=1062 from the my.cnf file then that you restart the Slave/Replica and you restart the replication.

10. Start MySQL on the Slave

service mysql start

11. Restore the mysqldump backup into the Slave/Replica environment

this restore will overwrite all the databases.

cd /tmp
mysql -uroot -pyour_password < mysql_backup.sql

12. restart the Slave/Replica instance

service mysql restart

13. Configure the replication on the slave

Note: Remember the bin log file name and position from step 3


CHANGE MASTER TO
    MASTER_HOST='192.168.10.1', 
    MASTER_USER='repl',
    MASTER_PASSWORD='yourpassword',
    MASTER_LOG_FILE='binlognamefrompoint3',
    MASTER_LOG_POS=positionfrompoint3;

14. Start the replication on the slave

start slave;

15. Validate the replication

In the result of this command the most important line is Seconds_Behind_Master.
If the value in the line is NULL, this means that your replication with the Master is not working.
If the value is greater than 0, this means that your replication is catching on. if you run this command few time, the value should go down.
If the value is equal to 0, that means that your replication is running and up to date.

show slave status\G 

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: log-bin.000056
          Read_Master_Log_Pos: 982311504
               Relay_Log_File: mysql_relay-bin.000130
                Relay_Log_Pos: 60222517
        Relay_Master_Log_File: log-bin.000056
             Slave_IO_Running: Yes
            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: 982311504
              Relay_Log_Space: 60406973
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 123