In some setups we need to change to data dir of MySQL, this way we can customize datadir as application name and make it more human readable. for this we only need to slight change in MySQL configuration file my.cnf
My setup details mysql-logo
For this setup I am using Linxu (CentOS5.8 64 bit) Machine
[root@srv3 ~]# cat /etc/redhat-release
CentOS release 5.8 (Final)
[root@srv3 ~]# uname -r
2.6.18-308.20.1.el5xen
My MySQL version is
[root@srv3 ~]# rpm -qa | grep mysql-server
mysql-server-5.0.95-1.el5_7.1
[root@srv3 ~]# mysql -V
mysql  Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (x86_64) using readline 5.1
For this, I like separate this into two cases
Case one.
In some case we know that we need change the mysql data sir before installing the MySQL
In this case just after installing the MySQL package and before starting the MySQL service edit /etc/my.cnf file. In some version my.cnf file is not present, in this copy /usr/share/doc/mysql-server-5.0.95/my-small.cnf to /etc/my.cnf. like in my case I like t change data dir to /data
[root@srv3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql   #This we need to change
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Case two.
In some we need to change data dir during the MySQL is in process.
Edit my.cnf as above
service mysqld stop
mkdir /data/mysql
cp  -rvfp /var/lib/mysql/* /data/mysql/
service mysqld start
Now we also need to check our configuration, for this we can use ps command.
[root@srv110 ~]# ps -ef | grep mysql
root      3900     1  0 Dec10 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe –datadir=/data/mysql –socket=/var/lib/mysql/mysql.sock –log-error=/var/log/mysqld.log –pid-file=/var/run/mysqld/mysqld.pid –user=mysql
mysql     3947  3900  0 Dec10 pts/0    00:00:00 /usr/libexec/mysqld –basedir=/usr –datadir=/data/mysql –user=mysql –pid-file=/var/run/mysqld/mysqld.pid –skip-external-locking –socket=/var/lib/mysql/mysql.sock
root      4250  3681  0 00:44 pts/0    00:00:00 grep mysql