August 7, 2020
August 7, 2020
MySQL is one of the most widely used database management systems for websites and server applications. It’s an essential tool that every webmaster should learn. That’s why in this tutorial, we will show you how to install MySQL on CentOS 7 through an SSH connection.
Try Hostinger’s VPS Hosting and get a massive 77% discount!
There are three steps needed when you want to install MySQL — download the MySQL repository, install it, and check its status. We’ll go through each step in greater detail.
Before we begin learning how to install MySQL on CentOS 7, make sure that either your VPS or dedicated server is running on CentOS 7 and you have root access to it. You can learn how to connect to your server via SSH by following our tutorial.
sudo yum update
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
sudo yum install mysql-server
sudo systemctl start mysqld
sudo systemctl status mysqld
If you see that MySQL is active and running like in the screenshot above, you have successfully installed and started MySQL on your server.
Now that you’re all set up, we’re going to show you some useful commands and tweaks you should know when working with MySQL.
When installing MySQL on CentOS 7, a temporary root password is generated. Issue the command below to see it:
sudo grep 'password' /var/log/mysqld.log
In order to change it, follow these steps:
The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password:
PRO TIP: A strong password is 8-12 characters long and includes numbers, special characters, uppercase, and lowercase letters.
Once you’ve MySQL installed on CentOS 7, you can test if everything was set up correctly by checking its version. Type in the following command:
mysql -u root -p
Enter the root password that you’ve created, and the response will be as below:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 22 Server version: 8.0.20 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.
If you want to reset your password, the process is pretty straightforward. Follow the steps below to change your MySQL root password:
sudo systemctl stop mysqld
sudo mysqld_safe --skip-grant-tables
USE MYSQL; UPDATE USER SET PASSWORD=PASSWORD(“newpassword”) WHERE USER=’root’; FLUSH PRIVILEGES; EXIT
sudo systemctl start mysqld
As the root user in MySQL, you have full access to all of the databases.
However, if you work with a team, there are cases where you might need to apply some restrictions. You’d either have to build a new database or create users with custom permissions.
Here’s how to quickly solve this issue:
CREATE DATABASE newdb
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'
DROP USER ‘username’@‘localhost’
Grant the new user access to a database with by typing in:
GRANT ALL PRIVILEGES ON newdb.* TO 'username'@'localhost'
You can also grant privileges individually, including:
For example, to grant the CREATE privilege, you’d need to type:
GRANT CREATE ON newdb.* TO 'username'@'localhost'
On the other hand, if you want to remove someone’s access, use the following:
REVOKE permission_type ON newdb.* TO 'username'@'localhost'
You can also check what current privileges a user has:
SHOW GRANTS username
Finally, once you’ve finished managing your users, it’s a good idea to reset all the privileges by using this command for your changes to take effect:
MySQL also has a list of other helpful commands. Simply enter \h or help to view the list shown below:
List of all MySQL commands: Note that all text commands must be first on line and end with ';' ? (\?) Synonym for `help'. clear (\c) Clear command. connect (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter. edit (\e) Edit command with $EDITOR. ego (\G) Send command to mysql server, display result vertically. exit (\q) Exit mysql. Same as quit. go (\g) Send command to mysql server. help (\h) Display this help. nopager (\n) Disable pager, print to stdout. notee (\t) Don't write into outfile. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. print (\p) Print current command. prompt (\R) Change your mysql prompt. quit (\q) Quit mysql. rehash (\#) Rebuild completion hash. source (\.) Execute an SQL script file. Takes a file name as an argument. status (\s) Get status information from the server. system (\!) Execute a system shell command. tee (\T) Set outfile [to_outfile]. Append everything into given outfile. use (\u) Use another database. Takes database name as argument. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (\W) Show warnings after every statement. nowarning (\w) Don't show warnings after every statement. For server side help, type 'help contents' mysql>
You have learned how to install MySQL on a CentOS 7 server. By installing MySQL, you are able to store your databases and manage them efficiently on your server.
Be sure to check out our other VPS tutorials and leave a comment down below if you have any questions.
April 18 2017
I have a Ubuntu 14.04 server. Can I follow these same steps to install MySQL there?
April 18 2017
I have a VPS account with Ubuntu 14.04 installed. Do we install mysql on it the same way shown above?
March 26 2018
Thank you very much. Very simple, nice and clean tutorial. Worked for me.
June 06 2020
Thank you, this was a simple guide to follow and worked like a charm. Had me running in minutes, now I can play around.