Ubuntu Linux: Install MySQL Server Version 5

How do I install Oracle (Sun) MySQL database server version 5.x under Ubuntu Linux operating systems? How do I add a new user and database for Apache web server and php access under Ubuntu Linux?

MySQL database server is now owned by Oracle (formally Sun Microsystems) but can be installed using command line options without compiling anything under Ubuntu Linux.
Tutorial details
Difficulty Intermediate (rss)
Root privileges Yes
Requirements bash/apt-get
Time N/A
mysqld, also known as MySQL Server, is the main program that does most of the work in a MySQL installation. Open a terminal and type the following commands to upgrade package database:
$ sudo apt-get update
$ sudo apt-get upgrade


Installing MySQL 5 server on Ubuntu

Type the following command to install latest stable MySQL server software:
$ sudo apt-get install mysql-server mysql-common mysql-client
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libnet-daemon-perl libplrpc-perl mysql-client-5.5 mysql-client-core-5.5
  mysql-server-5.5 mysql-server-core-5.5
Suggested packages:
  libipc-sharedcache-perl libterm-readkey-perl tinyca mailx
The following NEW packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libnet-daemon-perl libplrpc-perl mysql-client mysql-client-5.5
  mysql-client-core-5.5 mysql-common mysql-server mysql-server-5.5
0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.
Need to get 27.3 MB of archives.
After this operation, 97.5 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-common all 5.5.31-0ubuntu0.12.04.1 [13.4 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main libmysqlclient18 amd64 5.5.31-0ubuntu0.12.04.1 [948 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu/ precise/main libnet-daemon-perl all 0.48-1 [43.1 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu/ precise/main libplrpc-perl all 0.2020-2 [36.0 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu/ precise/main libdbi-perl amd64 1.616-1build2 [849 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu/ precise/main libdbd-mysql-perl amd64 4.020-1build2 [106 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-client-core-5.5 amd64 5.5.31-0ubuntu0.12.04.1 [1,936 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-client-5.5 amd64 5.5.31-0ubuntu0.12.04.1 [8,350 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-server-core-5.5 amd64 5.5.31-0ubuntu0.12.04.1 [6,085 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-server-5.5 amd64 5.5.31-0ubuntu0.12.04.1 [8,877 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu/ precise/main libhtml-template-perl all 2.10-1 [65.0 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-client all 5.5.31-0ubuntu0.12.04.1 [11.5 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu/ precise-updates/main mysql-server all 5.5.31-0ubuntu0.12.04.1 [11.6 kB]
Fetched 27.3 MB in 1min 6s (411 kB/s)                                          
Preconfiguring packages ...
Selecting previously unselected package mysql-common.
(Reading database ... 222510 files and directories currently installed.)
Unpacking mysql-common (from .../mysql-common_5.5.31-0ubuntu0.12.04.1_all.deb) ...
Selecting previously unselected package libmysqlclient18.
Unpacking libmysqlclient18 (from .../libmysqlclient18_5.5.31-0ubuntu0.12.04.1_amd64.deb) ...
Selecting previously unselected package libnet-daemon-perl.
Unpacking libnet-daemon-perl (from .../libnet-daemon-perl_0.48-1_all.deb) ...
Selecting previously unselected package libplrpc-perl.
Unpacking libplrpc-perl (from .../libplrpc-perl_0.2020-2_all.deb) ...
Selecting previously unselected package libdbi-perl.
Unpacking libdbi-perl (from .../libdbi-perl_1.616-1build2_amd64.deb) ...
Selecting previously unselected package libdbd-mysql-perl.
Unpacking libdbd-mysql-perl (from .../libdbd-mysql-perl_4.020-1build2_amd64.deb) ...
Selecting previously unselected package mysql-client-core-5.5.
Unpacking mysql-client-core-5.5 (from .../mysql-client-core-5.5_5.5.31-0ubuntu0.12.04.1_amd64.deb) ...
Selecting previously unselected package mysql-client-5.5.
Unpacking mysql-client-5.5 (from .../mysql-client-5.5_5.5.31-0ubuntu0.12.04.1_amd64.deb) ...
Selecting previously unselected package mysql-server-core-5.5.
Unpacking mysql-server-core-5.5 (from .../mysql-server-core-5.5_5.5.31-0ubuntu0.12.04.1_amd64.deb) ...
Processing triggers for man-db ...
Setting up mysql-common (5.5.31-0ubuntu0.12.04.1) ...
Selecting previously unselected package mysql-server-5.5.
(Reading database ... 222875 files and directories currently installed.)
Unpacking mysql-server-5.5 (from .../mysql-server-5.5_5.5.31-0ubuntu0.12.04.1_amd64.deb) ...
Selecting previously unselected package libhtml-template-perl.
Unpacking libhtml-template-perl (from .../libhtml-template-perl_2.10-1_all.deb) ...
Selecting previously unselected package mysql-client.
Unpacking mysql-client (from .../mysql-client_5.5.31-0ubuntu0.12.04.1_all.deb) ...
Selecting previously unselected package mysql-server.
Unpacking mysql-server (from .../mysql-server_5.5.31-0ubuntu0.12.04.1_all.deb) ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Processing triggers for man-db ...
Setting up libmysqlclient18 (5.5.31-0ubuntu0.12.04.1) ...
Setting up libnet-daemon-perl (0.48-1) ...
Setting up libplrpc-perl (0.2020-2) ...
Setting up libdbi-perl (1.616-1build2) ...
Setting up libdbd-mysql-perl (4.020-1build2) ...
Setting up mysql-client-core-5.5 (5.5.31-0ubuntu0.12.04.1) ...
Setting up mysql-client-5.5 (5.5.31-0ubuntu0.12.04.1) ...
Setting up mysql-server-core-5.5 (5.5.31-0ubuntu0.12.04.1) ...
Setting up mysql-server-5.5 (5.5.31-0ubuntu0.12.04.1) ...
mysql start/running, process 9683
Setting up libhtml-template-perl (2.10-1) ...
Setting up mysql-client (5.5.31-0ubuntu0.12.04.1) ...
Setting up mysql-server (5.5.31-0ubuntu0.12.04.1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

You will be prompted to setup mysql root user account password as follows:

Fig.01: Set a password for the mysql admin root user

Fig.01: Set a password for the mysql admin root user

Next, you can install the php mysql access module as follows:
$ sudo apt-get install php5-mysql
$ sudo /etc/init.d/apache2 restart

A Perl5 database interface to the MySQL data can be installed as follows:
$ sudo apt-get install libdbd-mysql-perl

How do I access MySQL server?

Type the following command:
$ mysql -u root -p
You need to supply root user account password:

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 107
Server version: 5.1.41-3ubuntu12.3 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


Task: List MySQL databases

Type the following sql command at mysql> prompt:
mysql> show databases;
Sample outputs:

| Database           |
| information_schema |
| mysql              |
| wiki               |
| wikidb             |
| wpmu               |
5 rows in set (0.02 sec)


Task: Add A new MySQL database

To add a new database called myapps, enter:
mysql> create database myapps;
Sample outputs:

Query OK, 1 row affected (0.00 sec)

Task: Add a new yser for myapps MySQL database

Add a user called vivek and grant access from localhost:
mysql> GRANT ALL ON myapps.* TO vivek@localhost IDENTIFIED BY 'Add-Your-Password-Here';

Task: Add a new user for myapps MySQL database (network access)

Make sure user vivek can access myapps database from the Apache web server installed at IP address
mysql> GRANT ALL ON myapps.* TO vivek@ IDENTIFIED BY 'Your-Password-Here';

How do I view MySQL log files?

The log is stored at the file location /var/log/mysql/error.log, enter:
$ tail -f /var/log/mysql/error.log
Sample outputs:

130511 13:36:51 InnoDB: Completed initialization of buffer pool
130511 13:36:51 InnoDB: highest supported file format is Barracuda.
130511 13:36:52  InnoDB: Waiting for the background threads to start
130511 13:36:53 InnoDB: 5.5.31 started; log sequence number 1595675
130511 13:36:53 [Note] Server hostname (bind-address): ''; port: 3306
130511 13:36:53 [Note]   - '' resolves to '';
130511 13:36:53 [Note] Server socket created on IP: ''.
130511 13:36:53 [Note] Event Scheduler: Loaded 0 events
130511 13:36:53 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.31-0ubuntu0.12.04.1'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

Use the grep command to search for error message, enter:
$ grep 'something' /var/log/mysql/error.log

How do I configure MySQL database server?

The default configuration file is located at /etc/mysql/my.cnf, enter:
$ sudo vi /etc/mysql/my.cnf
Change network binding to so that web server located at can access the database:
bind-address =
Save and close the file.

How do I start / stop / restart the MySQL database server using command line tools?

Type the following commands:
$ sudo service mysql restart
$ sudo service mysql stop
$ sudo service mysql start

Sample outputs:

mysql start/running, process 4930

You can also use the following command for older version:
$ sudo /etc/init.d/mysql start
$ sudo /etc/init.d/mysql stop
$ sudo /etc/init.d/mysql restart

Latest version of Ubuntu can use the following commands too:

sudo restart mysql
sudo stop mysql
sudo start mysql
See Also:
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

8 comments… add one
  • Nilson Silva Apr 7, 2013 @ 22:17

    Just THX BRO

  • CoreM May 4, 2013 @ 2:42

    Thanks dude!

  • Vishwa Jul 19, 2013 @ 11:18

    Hi Friend,
    I am facing so much issues while installing MySQL on my Ubuntu.
    vishwa@ubuntu:/$ sudo apt-get install mysql-server mysql-common mysql-client
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package mysql-server
    E: Unable to locate package mysql-client
    Please help me out!!! Think its very urgent!!


    • Saravanan Aug 11, 2014 @ 13:30

      Hi Vishwa,

      Please execute sudo apt-get update command first, then execute sudo apt-get install mysql-server mysql-common mysql-client

  • suraj madde Aug 5, 2013 @ 11:40

    nice experience with urs site

  • Utsav Popli Apr 26, 2014 @ 10:12

    It really helped me out. I installed mysql workbench but it was not able to connect. Later on i found out that there was no server with workbench. This tutorial really worked for me !
    Thanks !

  • Abhinav Oct 28, 2015 @ 4:47

    It is an awesome work that you are doing..keep on doing..and further you can use this website for ad purpose

  • tom sullivan Mar 8, 2016 @ 2:38

    I come back to this post on the rare occasions I set up a mysql server. In my browser bookmarks this page has the notation “THIS IS IT!” Most helpful — thanks.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.