Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Thursday, January 20, 2011

Backup a MySQL database using IBM Storwize V7000's FlashCopy

I wanted to backup my MySQL databases data dir using the Storwize V7000's FlashCopy functionality.
I first created a small database called address_book, and then added a table called address to it. Next I entered some data into the table address.
[root@isvx3 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.8 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, 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              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database address_book;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| address_book       |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)


mysql> use address_book;
Database changed
mysql> create table address (id int not null auto_increment, primary key(id), name varchar(30), address varchar(60));
Query OK, 0 rows affected (0.06 sec)


mysql> insert into address (id, name, address) values (1,"amitabh", "mumbai");
Query OK, 1 row affected (0.00 sec)

mysql> insert into address (id, name, address) values (2,"de niro", "hollywood");
Query OK, 1 row affected (0.00 sec)


mysql> select * from address;
+----+---------+-----------+
| id | name    | address   |
+----+---------+-----------+
|  1 | amitabh | mumbai    |
|  2 | de niro | hollywood |
+----+---------+-----------+
2 rows in set (0.00 sec)

I started the Flashcopy(backup) process on the IBM Storwize V7000. The V7000 created another volume, and started the backup of the volume on which the data dir resided.

/dev/mapper/mpath32p1
9.9G 404M 9.0G 5% /home/mysql

The picture below shows how to backup using the GUI of the Storwize V7000.






As you see below, this has started the FlashCopy backup on the Storwize V7000. The FlashCopy has now created another volume, to which it has started copying our original volume which has the data dir for our MySQL database.


We now map the newly created volume to the MySQL server host as shown below.







While the MySQL data dir is being backed up I went back to my database, and inserted another entry into the mysql table.
mysql> insert into address (id, name, address) values (3,"sean connery", "edinburgh");
Query OK, 1 row affected (0.00 sec)

mysql> select * from address;
+----+--------------+-----------+
| id | name         | address   |
+----+--------------+-----------+
|  1 | amitabh      | mumbai    |
|  2 | de niro      | hollywood |
|  3 | sean connery | edinburgh |
+----+--------------+-----------+
3 rows in set (0.00 sec)

mysql>

As you see above I was able to make another entry into the table, which means that the volume that is being
backuped up the Stowize V7000 is still accessible for use by the MySQL database.

Next I want inject a fault onto my original MySQL data dir file system, and then test my backup volume.
Imagine this to be the fault that causes the volume to be unmounted:
[root@isvx3 ~]# umount /home/mysql
[root@isvx3 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             255G   60G  182G  25% /
/dev/sda1              99M   33M   62M  35% /boot
tmpfs                  32G   16G   17G  49% /dev/shm
[root@isvx3 ~]#

Next, when I try to start mysqld I notice that my database "address_book" is not present.
[root@isvx3 ~]# mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.8 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, 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              |
| test               |
+--------------------+
3 rows in set (0.03 sec)

mysql>

[root@isvx3 ~]# /etc/init.d/mysql stop
Stopping MySQL:                                            [  OK  ]

Edit the data dir entry in the /etc/my.cnf file to point to the backup 
[root@isvx3 ~]# vi /etc/my.cnf
[root@isvx3 ~]# /etc/init.d/mysql start
Starting MySQL:                                            [  OK  ]
[root@isvx3 ~]# ps -ef | grep mysql
root      9154     1  0 14:48 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/home/mysqlbak/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/lib/mysql/mysqld.log --pid-file=/var/lib/mysql/mysqld/mysqld.pid --user=mysql
mysql     9204  9154  0 14:48 pts/1    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/home/mysqlbak/mysql --user=mysql --pid-file=/var/lib/mysql/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root      9222  8065  0 14:49 pts/1    00:00:00 grep mysql
[root@isvx3 ~]#


[root@isvx3 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.8 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, 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 |
| address_book       |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> use address_book;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from address;
+----+---------+-----------+
| id | name    | address   |
+----+---------+-----------+
|  1 | amitabh | mumbai    |
|  2 | de niro | hollywood |
+----+---------+-----------+
2 rows in set (0.02 sec)

mysql>
As we see we can see the backup of the first two entries on the new volume. The third entry of the table was entered after the backup was started on Storwize V7000, hence we don't see it.

We can now take another back of original volume, but this time it will be a lot faster then the earlier backup as only the new entries will be copied.

The picture below shows how to start the follow on back



The next time mounted the back up file system, and re-started MySQL I could see the up to date entries in the my MySQL table.

Friday, January 14, 2011

Starting MySQL failed while changing datadir from /var/lib/mysql to /root/metro1/mysql

I created a volume on my IBM Storwize V7000, and presented to my host running Red Hat Enterprise Linux Server release 5.5 (Tikanga)

I then mounted an ext3 file system on a 10 GB partition of the volume.
/dev/mapper/mpath32p              19.9G  171M  9.2G   2% /root/metro1

I had initially installed MySql on the Linux host, and the datadir for my installation was /var/lib/mysql
I now thought that it would be a good idea to move my datadir from /var/lib/mysql to /root/metro1

It seemed a rather straight forward thing to do, so I edited /etc/my.cnf file and changed
[mysqld]
#datadir=/var/lib/mysql
datadir=/root/metro1/mysql

changed the ownership of /root/metro1/mysql to mysql
[root@isvx3 lib]# chown mysql:mysql /root/metro1/mysql
[root@isvx3 lib]# chmod 755 /root/metro1/mysql
When I tried to start mysqd as follows, I got the message that it FAILED
[root@isvx3 lib]# /etc/init.d/mysqld start
Timeout error occurred trying to start MySQL Daemon.
Starting MySQL:                                            [FAILED]


In the log file under /var/lib/mysql/mysqld.log I noticed the following message:
InnoDB: Foreign key constraint system tables created
110114 11:23:25  InnoDB: Started; log sequence number 0 0
110114 11:23:25 [ERROR] bdb:  /root/metro1/mysql: Permission denied
110114 11:23:25 [ERROR] bdb:  /root/metro1/mysql/log.0000000001: Permission denied
110114 11:23:25 [ERROR] bdb:  PANIC: Permission denied
110114 11:23:25 [ERROR] bdb:  PANIC: DB_RUNRECOVERY: Fatal error, run database recovery
110114 11:23:25 [ERROR] bdb:  fatal region error detected; run recovery
110114 11:23:25 [ERROR] bdb:  /root/metro1/mysql: Permission denied
110114 11:23:25 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
110114 11:23:25  mysqld ended

I then changed datadir from /var/lib/mysql to /var/lib/test , and made the ownership and permission changes
to /var/lib/test. I then started the mysqld again, this time mysqld started fine.

Everything seemed the same between the /root/metro1/mysql where mysqld failed, and /var/lib/mysql where mysqld started.

On digging further I noticed the following difference in the permission:
[root@isvx3 lib]# ls -l /
drwxr-xr-x  24 root   root  4096 Sep  1 11:16 var
drwxr-x---  31 root   root  4096 Jan 12 17:37 root

Created a new mount point /home/mysql and mounted the file system on it, intead of /root/metro1
Then I updated the /etc/my.cnf file with the new datadir ie. /home/mysql/mysql

/dev/mapper/mpath32p1          9.9G  171M  9.2G   2% /home/mysql

[root@isvx3 ~]# mkdir -p /home/mysql/mysql
[root@isvx3 ~]# chown mysql:mysql /home/mysql/mysql
[root@isvx3 ~]# chmod 755 /home/mysql/mysql
[root@isvx3 ~]#


After when I started mysqld with the below mentioned command, MySQL started fine.
[root@isvx3 ~]# /etc/init.d/mysqld start
Starting MySQL:                                            [  OK  ]

Also, under /home/mysql/mysql I could see the newly created files

[root@isvx3 mysql]# pwd

/home/mysql/mysql

[root@isvx3 mysql]# ls

ibdata1  ib_logfile0  ib_logfile1  mysql  test

[root@isvx3 mysql]# ls -lh

total 21M

-rw-rw---- 1 mysql mysql  10M Jan 14 14:19 ibdata1

-rw-rw---- 1 mysql mysql 5.0M Jan 14 14:19 ib_logfile0

-rw-rw---- 1 mysql mysql 5.0M Jan 14 14:19 ib_logfile1

drwx------ 2 mysql mysql 4.0K Jan 14 14:19 mysql

drwx------ 2 mysql mysql 4.0K Jan 14 14:19 test

[root@isvx3 mysql]#



Moral of the story is that the error stemmed from the permission of the /root director. I didn't to change the permissions of the /root but instead created the datadir under /home/mysql/mysql to fix the issue.

Thursday, January 13, 2011

What does /etc/init.d/mysqld start really do?

Well I started mysql using the following
# /etc/init.d/mysqld start

First let us see what the script /etc/init.d/mysqld does when "start" is passed to it.

1. It creates the error log file /var/log/mysqld.log

2. Changes the permission of the owner and group to "mysql"

3. If /var/lib/mysql/mysql is not present, then initialize the database by running the script /usr/bin/mysql_install_db

4. Change the user and group of /var/lib/mysql to "mysql", and the permission of it to 0755

5. Next call /usr/bin/mysqld_safe and pass it the data directory, socket file name, error log file, the pid file name, and the user name.

6. Finally check if the server has come up by checking the response /usr/bin/mysqladmin

Wednesday, January 12, 2011

MySQL installation on Red Hat Enterprise Linux is yummy!!!

Login as root onto the Red Hat Enterprise Linux server

[root@isvx3 ~]# yum install mysql-server mysql
Loaded plugins: rhnplugin, security
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.i386 0:5.0.77-4.el5_5.4 set to be updated
--> Processing Dependency: perl(DBI) for package: mysql
---> Package mysql.x86_64 0:5.0.77-4.el5_5.4 set to be updated
---> Package mysql-server.x86_64 0:5.0.77-4.el5_5.4 set to be updated
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:3.0007-2.el5 set to be updated
---> Package perl-DBI.x86_64 0:1.52-2.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch      Version              Repository               Size
================================================================================
Installing:
 mysql             i386      5.0.77-4.el5_5.4     rhel-x86_64-server-5    4.8 M
 mysql             x86_64    5.0.77-4.el5_5.4     rhel-x86_64-server-5    4.8 M
 mysql-server      x86_64    5.0.77-4.el5_5.4     rhel-x86_64-server-5    9.8 M
Installing for dependencies:
 perl-DBD-MySQL    x86_64    3.0007-2.el5         rhel-x86_64-server-5    147 k
 perl-DBI          x86_64    1.52-2.el5           rhel-x86_64-server-5    605 k

Transaction Summary
================================================================================
Install       5 Package(s)
Upgrade       0 Package(s)

Total download size: 20 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): perl-DBD-MySQL-3.0007-2.el5.x86_64.rpm            | 147 kB     00:00
(2/5): perl-DBI-1.52-2.el5.x86_64.rpm                    | 605 kB     00:00
(3/5): mysql-5.0.77-4.el5_5.4.i386.rpm                   | 4.8 MB     00:02
(4/5): mysql-5.0.77-4.el5_5.4.x86_64.rpm                                                                                                             | 4.8 MB     00:15
(5/5): mysql-server-5.0.77-4.el5_5.4.x86_64.rpm                                                                                                      | 9.8 MB     00:10
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       657 kB/s |  20 MB     00:31
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : perl-DBI                                                                                                                                             1/5
  Installing     : mysql                                                                                                                                                2/5
  Installing     : perl-DBD-MySQL                                                                                                                                       3/5
  Installing     : mysql                                                                                                                                                4/5
  Installing     : mysql-server                                                                                                                                         5/5

Installed:
  mysql.i386 0:5.0.77-4.el5_5.4                        mysql.x86_64 0:5.0.77-4.el5_5.4                        mysql-server.x86_64 0:5.0.77-4.el5_5.4

Dependency Installed:
  perl-DBD-MySQL.x86_64 0:3.0007-2.el5                                                     perl-DBI.x86_64 0:1.52-2.el5

Complete!
[root@isvx3 ~]#

The installation created user "mysql" in the /etc/passwd file
-bash-3.2$ cat /etc/passwd | grep mysql
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

[root@isvx3 ~]# su - mysql
-bash-3.2$ pwd
/var/lib/mysql
 -bash-3.2$ id
uid=27(mysql) gid=27(mysql) groups=27(mysql)
Now that we have completed the installation bits, let us try and start it.
[root@isvx3 etc]# ps -ef | grep mysql
root      2560 21023  0 17:13 pts/1    00:00:00 grep mysql
[root@isvx3 etc]# chkconfig mysqld on
[root@isvx3 etc]# /etc/init.d/mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h isvx3.storage.tucson.ibm.com password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
Starting MySQL:                                            [  OK  ]

Now let us check the MySQL processes on the system
[root@isvx3 etc]# ps -ef | grep mysql
root      2929     1  0 17:14 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --user=mysql
mysql     2979  2929  0 17:14 pts/1    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root      3188 21023  0 17:14 pts/1    00:00:00 grep sql
[root@isvx3 etc]#