LATEST TOPICS

Installing MySQL on Linux : Part 1

Introduction:

MySQL is world’s most popular open source database.

MySQL is a popular choice of database for use in web applications, and is a central component of the widely used LAMP open source web application software stack (and other ‘AMP’ stacks like XAMP). LAMP is an acronym for “Linux, Apache, MySQL, Perl/PHP/Python.” Free-software-open source projects that require a full-featured database management system often use MySQL.

MySQL supports both 32-bit and 64-bit versions of the following platforms

Platforms
Linux
Solaris
Windows
AIX
HPUX

The most important feature that makes MySQL different from other open source databases is it’s pluggable storage engine structure wherein we can implement different storage options (MyISAM, InnoDB, Memory, CSV, NDBCLUSTER, etc.) for individual tables within the same database.

Further Readings about MySQL storage engines: MySQL Storage Engines

MySQL can be installed in a number of ways on each of the above mentioned platforms. In today’s article, I would be demonstrating MySQL installation using RPM package on Linux platform.

Installing MySQL using RPM package:

I would be installing the latest version (5.6.21) of MySQL on Linux x86 64-bit machine using RPM package.

First, we need to download the RPM package bundle from http://dev.mysql.com/downloads/

The RPM package has the format MySQL-server-[version].[platform].rpm

In my case the RPM package is MySQL-server-5.6.21-1.linux_glibc2.5.x86_64.run

Where:
linux_glibc2.5 : Indicates that the RPM is a generic RPM and can be installed on any Linux Version
x86_64 : Indicates the RPM is for x86_64 CPU architecture

While installing MySQL using the RPM package, the installer uses the following directory structures for placing files related to MySQL installation.

Directory Contents of Directory
/usr/bin Client programs and scripts
/usr/sbin The mysqld server
/var/lib/mysql Log files, databases
/usr/share/info Manual in Info format
/usr/share/man Unix manual pages
/usr/include/mysql Include (header) files
/usr/lib/mysql Libraries
/usr/share/mysql Miscellaneous support files, including error messages, character set files, sample configuration files, SQL for database installation
/usr/share/sql-bench Benchmarks

Before starting with MySQL RPM installation, lets check if we already have MySQL installed on the machine.

[root@labserver stage]$rpm -qa | grep -i mysql
mysql-community-release-el5-5

Okay, MySQL is not installed. Lets start with a fresh installation.

MySQL RPM bundle consists of multiple RPM (s). However, for the basic functionality, we need to install MySQL Server and client packages.

The RPM bundle comes in a TAR or ZIP package containing all the RPM packages. Lets UNTAR the RPM bundle

[root@labserver stage]$ls -lrt MySQL-5.6.21-1.linux_glibc2.5.x86_64.rpm*
-rw-r--r-- 1 root root 315944960 Oct 31  2014 MySQL-5.6.21-1.linux_glibc2.5.x86_64.rpm-bundle.tar

[root@labserver stage]$tar -xvf MySQL-5.6.21-1.linux_glibc2.5.x86_64.rpm-bundle.tar
MySQL-devel-5.6.21-1.linux_glibc2.5.x86_64.rpm
MySQL-embedded-5.6.21-1.linux_glibc2.5.x86_64.rpm
MySQL-client-5.6.21-1.linux_glibc2.5.x86_64.rpm
MySQL-shared-compat-5.6.21-1.linux_glibc2.5.x86_64.rpm
MySQL-shared-5.6.21-1.linux_glibc2.5.x86_64.rpm
MySQL-test-5.6.21-1.linux_glibc2.5.x86_64.rpm
MySQL-server-5.6.21-1.linux_glibc2.5.x86_64.rpm

Now, we need to install the MySQL-server-[version].[platform].rpm and MySQL-client-[version].[platform].rpm packages for a standard installation to work.

Lets install the server package first, which is the primary package and have the MySQL database core.

[root@labserver stage]$rpm -ivh MySQL-server-5.6.21-1.linux_glibc2.5.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-server           ########################################### [100%]
2014-10-26 10:49:04 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-10-26 10:49:04 17471 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-10-26 10:49:04 17471 [Note] InnoDB: The InnoDB memory heap is disabled
2014-10-26 10:49:04 17471 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-10-26 10:49:04 17471 [Note] InnoDB: Memory barrier is not used
2014-10-26 10:49:04 17471 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-10-26 10:49:04 17471 [Note] InnoDB: Using Linux native AIO
2014-10-26 10:49:04 17471 [Note] InnoDB: Not using CPU crc32 instructions
2014-10-26 10:49:04 17471 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-10-26 10:49:04 17471 [Note] InnoDB: Completed initialization of buffer pool
2014-10-26 10:49:04 17471 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2014-10-26 10:49:04 17471 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2014-10-26 10:49:04 17471 [Note] InnoDB: Database physically writes the file full: wait...
2014-10-26 10:49:05 17471 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2014-10-26 10:49:05 17471 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2014-10-26 10:49:06 17471 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2014-10-26 10:49:06 17471 [Warning] InnoDB: New log files created, LSN=45781
2014-10-26 10:49:06 17471 [Note] InnoDB: Doublewrite buffer not found: creating new
2014-10-26 10:49:06 17471 [Note] InnoDB: Doublewrite buffer created
2014-10-26 10:49:06 17471 [Note] InnoDB: 128 rollback segment(s) are active.
2014-10-26 10:49:06 17471 [Warning] InnoDB: Creating foreign key constraint system tables.
2014-10-26 10:49:06 17471 [Note] InnoDB: Foreign key constraint system tables created
2014-10-26 10:49:06 17471 [Note] InnoDB: Creating tablespace and datafile system tables.
2014-10-26 10:49:06 17471 [Note] InnoDB: Tablespace and datafile system tables created.
2014-10-26 10:49:06 17471 [Note] InnoDB: Waiting for purge to start
2014-10-26 10:49:06 17471 [Note] InnoDB: 5.6.21 started; log sequence number 0
A random root password has been set. You will find it in '/root/.mysql_secret'.
2014-10-26 10:49:07 17471 [Note] Binlog end
2014-10-26 10:49:07 17471 [Note] InnoDB: FTS optimize thread exiting.
2014-10-26 10:49:07 17471 [Note] InnoDB: Starting shutdown...
2014-10-26 10:49:08 17471 [Note] InnoDB: Shutdown completed; log sequence number 1625977


2014-10-26 10:49:08 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-10-26 10:49:08 17496 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-10-26 10:49:08 17496 [Note] InnoDB: The InnoDB memory heap is disabled
2014-10-26 10:49:08 17496 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-10-26 10:49:08 17496 [Note] InnoDB: Memory barrier is not used
2014-10-26 10:49:08 17496 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-10-26 10:49:08 17496 [Note] InnoDB: Using Linux native AIO
2014-10-26 10:49:08 17496 [Note] InnoDB: Not using CPU crc32 instructions
2014-10-26 10:49:08 17496 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-10-26 10:49:08 17496 [Note] InnoDB: Completed initialization of buffer pool
2014-10-26 10:49:08 17496 [Note] InnoDB: Highest supported file format is Barracuda.
2014-10-26 10:49:08 17496 [Note] InnoDB: 128 rollback segment(s) are active.
2014-10-26 10:49:08 17496 [Note] InnoDB: Waiting for purge to start
2014-10-26 10:49:08 17496 [Note] InnoDB: 5.6.21 started; log sequence number 1625977
2014-10-26 10:49:08 17496 [Note] Binlog end
2014-10-26 10:49:08 17496 [Note] InnoDB: FTS optimize thread exiting.
2014-10-26 10:49:08 17496 [Note] InnoDB: Starting shutdown...
2014-10-26 10:49:10 17496 [Note] InnoDB: Shutdown completed; log sequence number 1625987




A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.
This is strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

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

New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

The RPM installation also creates the core databases (database dictionaries). In case, where the core databases are not created by default; we can use the script ‘mysql_install_db’ located under the installation base bin directory (/usr/bin) to create the core databases manually.

Now, we have MySQL server installed along with the core dictionary databases. Let’s verify

[root@labserver stage]$rpm -qa | grep -i mysql
MySQL-server-5.6.21-1.linux_glibc2.5
mysql-community-release-el5-5

Following files were be created as part of this installation.

--- These the scripts and tools that supports the MySQL database configuration
---
[root@labserver stage]$ls -lrt /usr/bin/mysql*
-rwxr-xr-x 1 root root    3976 Sep 11 19:50 /usr/bin/mysql_zap
-rwxr-xr-x 1 root root   17572 Sep 11 19:50 /usr/bin/mysql_setpermission
-rwxr-xr-x 1 root root   10106 Sep 11 19:50 /usr/bin/mysql_secure_installation
-rwxr-xr-x 1 root root   34598 Sep 11 19:50 /usr/bin/mysql_install_db
-rwxr-xr-x 1 root root   34938 Sep 11 19:50 /usr/bin/mysqlhotcopy
-rwxr-xr-x 1 root root    1366 Sep 11 19:50 /usr/bin/mysql_fix_extensions
-rwxr-xr-x 1 root root    7424 Sep 11 19:50 /usr/bin/mysqldumpslow
-rwxr-xr-x 1 root root   24691 Sep 11 19:50 /usr/bin/mysqld_safe
-rwxr-xr-x 1 root root   26356 Sep 11 19:50 /usr/bin/mysqld_multi
-rwxr-xr-x 1 root root    4334 Sep 11 19:50 /usr/bin/mysql_convert_table_format
-rwxr-xr-x 1 root root   11580 Sep 11 19:50 /usr/bin/mysqlbug
-rwxr-xr-x 1 root root 4381713 Sep 11 19:50 /usr/bin/mysql_tzinfo_to_sql
-rwxr-xr-x 1 root root 4701590 Sep 11 19:51 /usr/bin/mysql_plugin
-rwxr-xr-x 1 root root 4859871 Sep 11 19:51 /usr/bin/mysql_upgrade
-rwxr-xr-x 1 root root 8682150 Sep 11 19:51 /usr/bin/mysqltest

--- mysqld is the server demon that runs the MySQL server
---
[root@labserver stage]$ls -lrt /usr/sbin/mysql*
-rwxr-xr-x 1 root root 99673165 Sep 11 19:49 /usr/sbin/mysqld-debug
-rwxr-xr-x 1 root root 78519281 Sep 11 19:53 /usr/sbin/mysqld

Following files and directories under the datadir (/var/lib/mysql) were created during the RPM installation. Each directory created under the datadir corresponds to a specific MySQL database.

[root@labserver data]$cd /var/lib/mysql/
[root@labserver mysql]$ls -lrt
total 110732
drwxr-xr-x 2 mysql mysql     4096 Oct 26 10:49 test
-rw-rw---- 1 mysql mysql 50331648 Oct 26 10:49 ib_logfile1
drwx------ 2 mysql mysql     4096 Oct 26 10:49 performance_schema
drwx--x--x 2 mysql mysql     4096 Oct 26 10:49 mysql
-rw-rw---- 1 mysql mysql 50331648 Oct 26 10:49 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 Oct 26 10:49 ibdata1
-rw-r--r-- 1 mysql mysql      122 Oct 26 10:49 RPM_UPGRADE_MARKER-LAST
-rw-r--r-- 1 root  root       122 Oct 26 10:49 RPM_UPGRADE_HISTORY

Observe the file ownership for the database files. They are owned by ‘mysql’ OS user. The installation process also creates the OS user and OS group with name ‘mysql’ if it doesn’t exist and makes it as the owner of the MySQL database.

Okay, we are now good to start MySQL server.

[root@labserver stage]$/etc/init.d/mysql start
Starting MySQL..                                           [  OK  ]
[root@labserver stage]$ps -ef | grep mysql
root     17877     1  0 11:05 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/labserver.home.com.pid
mysql    17980 17877 22 11:05 pts/1    00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/labserver.home.com.err --pid-file=/var/lib/mysql/labserver.home.com.pid
root     18014 16298  0 11:06 pts/1    00:00:00 grep mysql

Now MySQL server is up and running along with the core dictionary databases. MySQL server can be accessed now.

To be able to access MySQL server and the databases, we need a database account. As part of the installation, MySQL database user ‘root’ was created with a random password being generated in file ‘/root/.mysql_secret’. This password is set to expired and we must reset it.

[root@labserver stage]$cat /root/.mysql_secret
# The random password set for the root user at Sun Oct 26 10:49:07 2014 (local time): RTBKL3yV5NUH94Ax

A ‘root’ user account is the database administration account which has the highest level of access in MySQL database.

To be able to reset the password and access the databases, we need to connect to the mysql client prompt

However, we haven’t installed MySQL client on our machine yet. Hence, we can’t use MySQL client at this point. We need to install MySQL client to be able to access and administrate the MySQL server and databases.

Lets install the client package.

[root@labserver stage]$rpm -ivh MySQL-client-5.6.21-1.linux_glibc2.5.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client           ########################################### [100%]

This will create following client side binaries or tools to facilitate database access and administration.

-rwxr-xr-x 1 root root 8045256 Sep 11 19:51 /usr/bin/mysqladmin
-rwxr-xr-x 1 root root 8022600 Sep 11 19:51 /usr/bin/mysqlcheck
-rwxr-xr-x 1 root root 9025468 Sep 11 19:51 /usr/bin/mysql
-rwxr-xr-x 1 root root 8094374 Sep 11 19:51 /usr/bin/mysqlslap
-rwxr-xr-x 1 root root 8015095 Sep 11 19:51 /usr/bin/mysqlshow
-rwxr-xr-x 1 root root 8036057 Sep 11 19:51 /usr/bin/mysqlimport
-rwxr-xr-x 1 root root 8261366 Sep 11 19:51 /usr/bin/mysqldump
-rwxr-xr-x 1 root root 8682150 Sep 11 19:51 /usr/bin/mysqltest
-rwxr-xr-x 1 root root 8843373 Sep 11 19:51 /usr/bin/mysqlbinlog

Now, we are ready to access the databases. Trying to access the databases without resetting root password will lead to errors.

[root@labserver stage]$cat /root/.mysql_secret
# The random password set for the root user at Sun Oct 26 10:49:07 2014 (local time): RTBKL3yV5NUH94Ax


[root@labserver stage]$mysql -u root -pRTBKL3yV5NUH94Ax
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.21

Copyright (c) 2000, 2014, 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;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

Lets reset the root password and query the databases.

[root@labserver stage]$mysql -u root -pRTBKL3yV5NUH94Ax
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.21

Copyright (c) 2000, 2014, 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> SET PASSWORD = PASSWORD('mysql');
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
---- information_schema, mysql and performance_schema are the core databases 
---- that forms the dictionaries and provide core functionality of MySQL server
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

We are done with MySQL Installation !!

Optionally, we can secure the MySQL installation by running the script mysql_secure_installation as suggested by the MySQL installer during the RPM installation. This step is highly recommended for production installation.

[root@labserver stage]$/usr/bin/mysql_secure_installation



NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

We can now create our own databases to be hosted under the installed MySQL server !!

Disclaimer: While installing MySQL server with RPM package, you need to have root access to be able to install the RPM

In my next article, I would be covering the custom installation of MySQL server. Till then stay tuned ..

%d bloggers like this:
Visit Us On LinkedinVisit Us On TwitterVisit Us On Google PlusCheck Our Feed