Contents
In this tutorial, you will learn how to install MySQL in Linux.
UNDERSTANDING THE SUBJECT MATTER
MySQL, just like MSSQL is a relational database management system. MySQL is open source and it was originally developed in 1994 before it was acquired by Oracle corporation in 2010, it is now supported by Oracle.
The basis of the relational database management system (RDBMS) was from Dr. E.F. Codd in 1970 who proposed relational model for database systems. The relational representation consists of a set of operators and object relations. Hence why in RDMS, data is stored in tables & rows and has fields. You may want to store information about all your employees, you will have to create a table and the table will have fields as well. To learn more about relational database management system (RDMS), click on this link
In RDMS, you use a structured query language (SQL) to access the database. There are other applications / tools that can be used to access MySQL database without using SQL directly. Examples are the likes of PHPMyAdmin, MySQL workbench, etc. However, these tools must also use SQL when querying the database.
There are a lot of SQL statements such as DDL, DML, DCL, TC, etc.
Examples of Data Definition Language statements (DDL) are create, rename, alter, truncate, comment and drop. Just as their name implies, the create statement can be used to create a database, the rename statement can be used to rename a database, the alter statement can also be used to alter a database, the truncate statement can be used to truncate a database while the drop statement can be used to drop a database.
Examples of Data Manipulation Language statements are select, update, insert, merge and delete. Just as their name implies, the select statement can be used to select a table, the insert statement can be used to insert a table, the merge statement can be used to merge a table while the delete statement can be used to delete a table.
An example of a select statement is
sql > select * from employees
which means select all from the employees table.
Examples of Data Control Language (DCL) are grant and revoke. They are self explanatory as their name implies.
Examples of Transaction Control (TC) are commit, savepoint, and rollback.
MySQL is flexible, scalable, reliable and very easy to use. It can run on almost any operating systems like Windows, Unix, Linux, etc, and it is a server/client based system. It is the most popular open source relational database management system (RDBMS).
Having understood what MySQL is, let’s get to the step by step process of how to install MySQL in Linux.
ACTION TIME
How To Install MySQL Server On Linux Step By Step Process
1. download MySQL yum repository file from the official site of MYSQL yum repository
NOTE: For CentOS/RHEL 8, mysql comes by default in the repository. Just do,
[root@HQEBPRD ~]# yum install mysql
For CentOS/RHEL 7, continue with the following steps.
[root@HQPRD2 ~]# wget https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
--2020-02-19 03:54:41-- https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.209.85.141
Connecting to repo.mysql.com (repo.mysql.com)|23.209.85.141|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26024 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-3.noarch.rpm.1’
100%[==============================================================================================================================>] 26,024 --.-K/s in 0.1s
2020-02-19 03:54:42 (252 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm.1’ saved [26024/26024]
2. Install the downloaded package to enable the repository.
[root@HQPRD2 ~]# rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
Retrieving https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
warning: /var/tmp/rpm-tmp.nU1oF4: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
3. Install MySQL server, the latest version will be installed.
As at when I am writing this article, the latest version is MySQL 8.0
[root@HQPRD2 ~]# yum install mysql-community-server
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
mysql-connectors-community | 2.5 kB 00:00:00
mysql-tools-community | 2.5 kB 00:00:00
mysql80-community | 2.5 kB 00:00:00
(1/3): mysql-connectors-community/x86_64/primary_db | 53 kB 00:00:00
(2/3): mysql-tools-community/x86_64/primary_db | 69 kB 00:00:00
(3/3): mysql80-community/x86_64/primary_db | 97 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.19-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.19-1.el7 for package: mysql-community-server-8.0.19-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.19-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.19-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.19-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:8.0.19-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
---> Package mysql-community-libs.x86_64 0:8.0.19-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:8.0.19-1.el7 will be obsoleting
--> Finished Dependency Resolution
- To confirm the version installed, use the command,
[root@HQPRD2 ~]# rpm -qa mysql-*
mysql-community-server-8.0.19-1.el7.x86_64
mysql-community-common-8.0.19-1.el7.x86_64
mysql-community-client-8.0.19-1.el7.x86_64
mysql-community-libs-compat-8.0.19-1.el7.x86_64
mysql-community-libs-8.0.19-1.el7.x86_64
[root@HQPRD2 ~]#
OR
[root@HQPRD2 ~]# mysql --version
mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
4. verify the status of the server.
[root@HQPRD2 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@HQPRD2 ~]#
5. start the server
[root@HQPRD2 ~]# systemctl start mysqld
you can enable the service to make it persistent after reboot if you wish.
[root@HQPRD2 ~]# systemctl enable mysqld
6. verify the status of the server again.
[root@HQPRD2 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-02-19 04:25:08 PST; 37s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 10808 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 10889 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─10889 /usr/sbin/mysqld
Feb 19 04:24:54 HQPRD2 systemd[1]: Starting MySQL Server...
Feb 19 04:25:08 HQPRD2 systemd[1]: Started MySQL Server.
[root@HQPRD2 ~]#
7. get the MYSQL temporary password and secure the installation.
NB: when mysql is installed, mysql user is automatically created as well
- To get the temporary password, use the command,
[root@HQPRD2 ~]# grep 'temporary password' /var/log/mysqld.log
2020-02-19T12:25:04.172139Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 1fCb.gTmXsP!
[root@HQPRD2 ~]#
- secure the MySQL installation.
[root@HQPRD2 ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Enter the temporary password that was generated. In my case, 1fCb.gTmXsP!
The existing password for the user account root has expired. Please set a new password.
New password:
Enter a new password for the MySQL root user . note that this does not have to do with the system’s password.
Also, Make sure that the password satisfy the current policy requirements. In other words, your password must contain at least an uppercase, lowercase, number and a character.
Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
enter the key, y to continue
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? (Press y|Y for Yes, any other key for No) :
Enter the key, y to continue
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? (Press y|Y for Yes, any other key for No) :
enter the key, y to continue if you want to disallow remote connection of MySQL root user
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? (Press y|Y for Yes, any other key for No) :
enter the key y, to continue
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) :
enter the key y, to continue
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
8. Connect to the MySQL server.
[root@HQPRD2 ~]# mysql -u root -p
Enter password:
Enter the password you set and boom !!! you are in.
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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>
9. To logout of the MYSQL server, use the command, “quit”
mysql> quit
Bye
[root@HQPRD2 ~]#
How To Uninstall MySQL Server In Red Hat / CentOS 7&8 Linux
To uninstall the MySQL server, use the command,
[root@HQPRD2 ~]# yum remove mysql-community-server
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.19-1.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Removing:
mysql-community-server x86_64 8.0.19-1.el7 @mysql80-community 1.9 G
Transaction Summary
========================================================================================================================================================================
Remove 1 Package
Installed size: 1.9 G
Is this ok [y/N]:
How To Uninstall MySQL Server In Ubuntu Linux
root@ubuntu:~# apt remove mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
grub-pc-bin libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl
libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl
libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
mysql-server
0 upgraded, 0 newly installed, 1 to remove and 8 not upgraded.
After this operation, 110 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 54757 files and directories currently installed.)
Removing mysql-server (5.7.29-0ubuntu0.16.04.1) ...
root@ubuntu:~#
How To Update MySQL Server In Linux.
To update the MySQL server, use the command,
[root@HQPRD2 ~]# yum update mysql-server
Your feedback is welcomed. If you love others, you will share with others.
Leave a Reply