How to install MySQL from Scratch

In this tutorial we will learn about how to install MySQL Database server using the RPM’s on Linux operating system. As we all know MySQL’s community version is free of cost so we will simply download the RPMs from MySQL site and I’ll show you the procedure of installing it on any of your Linux OS.

Step by Step guide to install MySQL:

1.  Download the latest RPMs from MySQL site from here https://dev.mysql.com/downloads/mysql/

Install MySQL

Download the RPMs as per your CPU architecture, if your CPU is 32 bit then download the RPMs with i386 at the end or x64 if your CPU is 64 bit. If you do not know your CPU architecture just execute this command

uname -a
Linux proprogramming 2.6.18-419.el5xen #1 Fri Feb 24 22:50:37 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

As you can see mine is x86_64 that means I have 64 bit CPU.

Now as you can see there are so many RPMs so which one to download, just download the following one as others are not that much necessary.

  • MySQL-client-advanced-5.7.19-1.el6.x86_64.rpm
  • MySQL-server-advanced-5.7.19-1.el6.x86_64.rpm
  • MySQL-shared-advanced-5.7.19-1.el6.x86_64.rpm
  • MySQL-shared-compat-advanced-5.7.`9-1.el6.x86_64.rpm

 

2. Install the RPMs

Now as we have downloaded the RPMs we just need to install it now, to install execute to below command:

rpm -ivh --noscripts MySQL-client-advanced-5.7.19-1.el6.x86_64.rpm
rpm -ivh --noscripts MySQL-server-advanced-5.7.19-1.el6.x86_64.rpm
rpm -ivh --noscripts MySQL-shared-advanced-5.7.19-1.el6.x86_64.rpm
rpm -ivh --noscripts MySQL-shared-compat-advanced-5.7.`9-1.el6.x86_64.rpm

This will install the RPMs, just install it one by one.

3. Configuring MySQL:

Now as have the RPMs installed we will now configure MySQL as per our needs, now you can find the configuration file of mysql at:

/etc/my.cnf

Simply edit it and change whatever your requirements are, for example if you need a custom data directory instead of default (/var/lib/mysql is default) just edit the datadir variable in the my.cnf file.

If you do not know about the parameters simply leave it as it is.

 

4. Run the installation script:

Just execute below command:

mysql_install_db --datadir=/var/lib/mysql --defaults-file=/etc/my.cnf

The purpose of the mysql_install_db program is to initialize the data directory, including the tables in the mysql system database. It does not overwrite existing MySQL privilege tables, and it does not affect any other data.

 

5. Starting MySQL Server:

Now as we have installed MySQL, we will start the MySQL server now, to do this simply execute below commands:

/etc/init.d/mysql status      # : status of Mysql
/etc/init.d/mysql start       # : start Mysql
/etc/init.d/mysql stop        # : stop Mysql

We can also use these commands too:

systemctl mysqld status
systemctl mysqld start
systemctl mysqld stop

 

6. Script for setting passwords and other basic stuff:

Now we have a running MySQL server, its time do some basic stuff like setting root password, if you want to keep some default databases for playing with stuff you will get the option. So for all this execute the below command:

/usr/bin/mysql_secure_installation

This script will give you options various options like:

  • Change the root password
  • Remove the anonymous user
  • Disallow root login from remote machines
  • Remove the default sample test database

Below will be the output while executing the script:

[proprogramming]# /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 authorization.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

7. Verify MySQL Installation and Login:

Now we have everything set, we can verify the current version of MySQL running using following command:

Mysql -V

Connect to the MySQL database using the root user and make sure the connection is successful.

[local-host]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Now you have entered the MySQL console and you can do anything as per your requirement.

Hope you find this useful, any problems, suggestions are most welcome via comments.

 

Leave a Comment