How to Install and Setup MySQL on CentOS

This article describes basic installation of MySQL database server on CentOS 6. You might need to install other packages to let applications use MySQL, i.e PHP.

To can read how to install LAMP on CentOS here.

Installing and Setting up MySQL database Server:


1 – To install MySQL, input the following yum command:

yum -y install mysql-server


2 – To enable MySQL daemon to start on boot:

chkconfig mysqld on


/usr/bin/chkconfig mysqld on


3 – Start the service:

service mysqld start


/etc/init.d/mysqld start


4 – You should secure MySQL with the following command:





5 – Script will assist you with this process by presenting several questions. Recommended default action is to select YES for all questions.The command prompt will ask for your current MySQL root password. Leave it blank by pressing ENTER.
Answer the command prompt as specified below:

Set root password? [Y/n] Y
New password: YourOwnPassword
Re-enter new password: YourOwnPassword
Remove anonymous user? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y


Login to MySQL:

1 – After successfully installing MySQL, login as root user:

mysql -u root -p mysql


2 – Give the password we have assigned when we ran the secure script, when prompted.


3 – Create new User and Database:

mysql> CREATE DATABASE demodb;
mysql> GRANT ALL ON demodb.* TO 'demouser'@'localhost' IDENTIFIED BY 'password';
mysql> QUIT;


4 – Connect to MySQL new database and user:

mysql -u demouser -p demodb


Adding MySQL port in iptables of CentOS:

1 – To edit /etc/sysconfig/iptables file, enter:

vi /etc/sysconfig/iptables


2 – Now append the following code to open tcp port 3306 for MySQL:

-A INPUT -m state –state NEW,ESTABLISHED -m tcp -p tcp –dport 3306 -j ACCEPT


3 – Save and close the file and restart iptables by giving following command:

service iptables restart