CentOS Linux 5/6 Install Mysql Database Server

How do I install MySQL database server on CentOS Linux version 5 or version 6 using command line options? How do I configuring MySQL server on CentOS Linux?

You need to install the following packages on CentOS Linux:

  1. mysql-server : The MySQL server and related files
  2. mysql : MySQL client programs and shared libraries

Additionally, you may need to install the following packages to access mysql using various programming languages:

  1. php-mysql : A module for PHP applications that use MySQL databases.
  2. perl-DBD-MySQL : A MySQL interface for perl.
  3. MySQL-python : An interface to MySQL.

How to install MySQL on CentOS

To install mysql server type the following yum command:
# yum install mysql-server mysql
Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security
Loading mirror speeds from cached hostfile
 * base:
 * extras:
 * updates:
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.x86_64 0:5.1.71-1.el6 will be installed
---> Package mysql-server.x86_64 0:5.1.71-1.el6 will be installed
--> Processing Dependency: perl-DBI for package: mysql-server-5.1.71-1.el6.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.71-1.el6.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-server-5.1.71-1.el6.x86_64
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package             Arch        Version           Repository
 mysql               x86_64      5.1.71-1.el6      base      893 k
 mysql-server        x86_64      5.1.71-1.el6      base      8.6 M
Installing for dependencies:
 perl-DBD-MySQL      x86_64      4.013-3.el6       base      134 k
 perl-DBI            x86_64      1.609-4.el6       base      705 k
Transaction Summary
Install       4 Package(s)
Total download size: 10 M
Installed size: 29 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): mysql-5.1.71-1.el6.x86_64.rpm        | 893 kB     00:00     
(2/4): mysql-server-5.1.71-1.el6.x86_64.rpm | 8.6 MB     00:01     
(3/4): perl-DBD-MySQL-4.013-3.el6.x86_64.rp | 134 kB     00:00     
(4/4): perl-DBI-1.609-4.el6.x86_64.rpm      | 705 kB     00:00     
Total                              4.2 MB/s |  10 MB     00:02     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : perl-DBI-1.609-4.el6.x86_64                     1/4 
  Installing : perl-DBD-MySQL-4.013-3.el6.x86_64               2/4 
  Installing : mysql-5.1.71-1.el6.x86_64                       3/4 
  Installing : mysql-server-5.1.71-1.el6.x86_64                4/4 
  Verifying  : perl-DBD-MySQL-4.013-3.el6.x86_64               1/4 
  Verifying  : mysql-server-5.1.71-1.el6.x86_64                2/4 
  Verifying  : mysql-5.1.71-1.el6.x86_64                       3/4 
  Verifying  : perl-DBI-1.609-4.el6.x86_64                     4/4 
  mysql.x86_64 0:5.1.71-1.el6  mysql-server.x86_64 0:5.1.71-1.el6 
Dependency Installed:
  perl-DBD-MySQL.x86_64 0:4.013-3.el6                              
  perl-DBI.x86_64 0:1.609-4.el6                                    

Import CentOS Mysql server files and ports

  1. Mysql server config file: /etc/my.cnf
  2. Mysql default tcp port: 3306 (TCP/UDP)
  3. Mysql server log file: /var/log/mysqld.log
  4. Mysql database storage directory: /var/lib/mysql/
  5. Mysql rc script to start/stop/restart server: /etc/init.d/mysqld {start|stop|restart|reload|status|try-restart}

Howto configure MySQL on CentOS Linux

First, turn on mysql service, type the following chkconfig command:
# chkconfig mysqld on

Howto start / stop / restart mysql server on CentOS Linux

Use any one of the following command to start / stop / restart the mysql server:
# service mysqld start
# service mysqld restart
# service mysqld stop

# /etc/init.d/mysqld start
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld restart

Sample outputs:

Fig.01: Starting / stopping / restarting the mysql server on centos

Set the root user password

The root user is mysql admin user. By default there is no password assigned to the root user. Use the following command to set a new password for the root user:
# mysqladmin -u root password NEWPASSWORD
See how to Change/update the root password for mysqld for more information.

Configure mysqld via /etc/my.cnf config file

Edit /etc/my.cnf, enter:
# vi /etc/my.cnf
Sample default config file:

# Disabling symbolic-links is recommended to prevent assorted security risks

Mysql CentOS Linux firewall configuration

Edit /etc/sysconfig/iptables file, enter:
# vi /etc/sysconfig/iptables
Append the following code open tcp port 3306 on CentOS base server:

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

Save and close the file. To restart the firewall on CentOS/RHEL, enter:
# service iptables restart
See CentOS / Redhat Iptables Firewall Configuration Tutorial for more information.

How do I view mysql server logs?

To see server error log use the following tail command:
# tail -f /var/log/mysqld.log
Sample outputs:

131202 11:44:41 [Note] /usr/libexec/mysqld: Shutdown complete

131202 11:44:41 mysqld_safe mysqld from pid file /var/run/mysqld/ ended
131202 11:44:41 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
131202 11:44:41  InnoDB: Initializing buffer pool, size = 8.0M
131202 11:44:41  InnoDB: Completed initialization of buffer pool
131202 11:44:41  InnoDB: Started; log sequence number 0 44233
131202 11:44:41 [Note] Event Scheduler: Loaded 0 events
131202 11:44:41 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.1.71'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution

How do I connect to mysql server?

Use the mysql command line as follows:
$ mysql -u root -p

Sample outputs:

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.1.71 Source distribution
Copyright (c) 2000, 2013, 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
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

What next?

This entry is 1 of 5 in the CentOS Linux MySQL Server Tutorial series. Keep reading the rest of the series:
  1. CentOS install Mysql database server
  2. CentOS install MySQL client only
  3. MySQL Create a user accounts
  4. MySQL Create a database & tables and data
  5. Reinstall MySQL On Linux

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...