How to Install MySQL on CentOS 7: Step by Step Guide

MySQL is one of the most widely used for websites and server applications. It is an essential tool that every webmaster should learn. In this particular tutorial, we’ll explain how to install over an SSH connection.

How to install MySQL on a CentOS 7 server

There are three necessary steps that you need to perform when installing MySQL: download the MySQL repository, install it, and check its status. We’ll go over each step in more detail below.

1. Download and prepare the MySQL repository

Before you start learning how to install MySQL on CentOS 7, make sure that your VPS or dedicated server is running on CentOS 7 and that you have root access. You can learn how to connect to your server via SSH by following our .

  1. You will need to update the system by typing the following command: sudo yum update
  2. Once the system is updated, it is time to download the MySQL repositories. You can also see other versions of the repository. sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  3. At the end of the download, you should see a confirmation message that the file was saved .rpm.
  4. Now, you need to prepare the repository so that you can later install MySQL packages from it. Simply enter: sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
  5. Once the output indicates that the upgrade and installation are complete, you can install MySQL.

2. Install MySQL

  1. Use the following command yum install to install MySQL: sudo yum install mysql-server
  2. The script will return a list of packages and ask you for confirmation to download and install them. Write Y and press ENTER for each of the requests.
  3. You will have to do this several times, but eventually you will see the message Complete! At the end of the installation, this means that you have successfully installed MySQL on your server.

3. Start MySQL and check its status

  1. Once you have MySQL ready on CentOS 7, it will not start automatically immediately after installation. Therefore, you need to start it manually using the following command: sudo systemctl start mysqld
  2. You will get no response once MySQL starts, so to check if it is working properly, use the following command: sudo systemctl status mysqld
  3. Information about the MySQL process will appear:
See also  How to improve security in WordPress

If you see that MySQL is up and running like in the screenshot above, you have successfully installed and started MySQL on your server.

How to configure MySQL?

Now that everything is configured, we will show you some useful commands and settings that you should know when working with MySQL.

Change MySQL root user password

When installing MySQL on CentOS 7, a temporary root password is generated. Run the following command to see it:

sudo grep ‘password’ /var/log/mysqld.log

To change it, follow these steps:

  1. First, run the following command: sudo mysql_secure_installation
  2. Enter the temporary password and the following message will appear: The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password:
  3. Now type in a new strong password.

PROFESSIONAL TIP: A strong password is between 8 and 12 characters long and includes numbers, special characters, and upper and lower case letters.

  • Follow the instructions on the screen and press Y to finish the setup. You have successfully changed the password for the root user.
  • Check the current version of MySQL

    Once you have installed MySQL on CentOS 7, you can test if everything is configured correctly by checking its version. Type the following command:

    mysql -u root -p

    Enter the root password you have created and the response will be as follows:

    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.

    Reset MySQL root password

    If you want to reset your password, the process is quite simple. Follow the steps below to change your MySQL root password:

    1. Stop the MySQL server with this command: sudo systemctl stop mysqld
    2. Restart MySQL on Safe Mode or without a password by typing: sudo mysqld_safe –skip-grant-tables
    3. press ENTER if the MySQL server has started but appears to be blocked.
    4. Use the following command to reconnect to MySQL as root: mysql -uroot
    5. To change the root password, use the following commands: USE MYSQL; UPDATE USER SET PASSWORD=PASSWORD(“newpassword”) WHERE USER=’root’; FLUSH PRIVILEGES; EXIT
    6. replaces newpassword with your own secure password.
    7. Finally, restart MySQL using: sudo systemctl start mysqld
    8. Now you can login to MySQL with your new password.
    See also  How to use HeidiSQL to connect to a MySQL database

    Creating a new MySQL user database

    As the root user in MySQL, you have full access to all databases.

    However, if you work with a team, there are cases where you may need to apply some restrictions. You should either create a new database or create users with custom permissions.

    Here’s how to quickly fix this problem:

    1. Use the following command to create a new database: CREATE DATABASE newdb
    2. change newdb by the name of the database.
    3. Create a new user by typing the following line: CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’
    4. Change the variables username Y password to create your new user.
    5. If you want to remove a certain user, use the command: DROP USER ‘username’@’localhost’

    Manage MySQL user permissions

    Give the new user access to a database by typing:

    GRANT ALL PRIVILEGES ON newdb.* TO ‘username’@’localhost’

    You can also individually grant privileges, including:

    • SELECT: Users can read the database using the select command
    • CREATE: can generate new tables
    • DROP: allows users to delete tables
    • DELETE: users can delete rows from tables
    • INSERT: allows users to add rows in tables
    • UPDATE: users can update the rows
    • GRANT OPTION: they can grant or remove the privileges of other users

    For example, to grant the privilege CREATEyou must write:

    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’re done managing your users, it’s a good idea to reset all privileges using this command for your changes to take effect:

    FLUSH PRIVILEGES

    Other useful MySQL commands

    MySQL also has a list of other useful commands. just enter \h either help to see the list shown below:

    See also  Types of e-commerce with examples

    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 . 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 . 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>

    further reading

    conclusion

    You have learned how to install MySQL on a CentOS 7 server. By installing MySQL, you can efficiently store and manage your databases on your server.

    Be sure to check out our and leave a comment below if you have any questions.

    Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

    Loading Facebook Comments ...
    Loading Disqus Comments ...