Skip to main content
In this article, you will learn the fundamentals of MySQL, a popular open-source database renowned for its speed, reliability, and SQL-based data storage. We’ll walk through the installation process, initial configuration, connecting with the MySQL client, and executing basic commands and queries. MySQL is trusted by many high-profile websites, including Facebook, Google, and YouTube. There are two main editions of MySQL: the free Community Edition and a suite of commercial editions designed for enterprises.
The image is a presentation slide about MySQL, highlighting its open-source nature, speed, reliability, and various editions, including community and commercial options.

Installing MySQL

To install MySQL on your system, follow these steps:
  1. Download the RPM Package:
    Retrieve the MySQL RPM package from the MySQL downloads page.
  2. Install the Repository:
    Install the repository using the RPM command. Then, install the MySQL server using the yum package manager.
  3. Start the MySQL Service:
    Once installed, MySQL is configured as a service. Start the server and check its status.
Execute the following commands in your terminal:
The expected output should indicate that the service is active. For example:
In production environments, ensure to create dedicated users and groups, and configure additional security settings. Always refer to the official documentation for advanced configurations.

Viewing MySQL Logs

MySQL logs, located at /var/log/mysqld.log, provide essential information about the server’s startup, version, and listening port (default is 3306). You can view the log by running:
A sample log excerpt might look like this:

Connecting to MySQL

After installation, MySQL automatically generates a temporary root password logged in /var/log/mysqld.log. Use this password to connect via the MySQL client. For example:
Using a password directly on the command line can be insecure. Proceed with caution and consider using alternative methods to secure your credentials.
Once connected, you will see a welcome message similar to the following:

Changing the Root Password

At the MySQL prompt, change the default root password by running:
For additional hardening, it is recommended to run the “mysql_secure_installation” script from the Linux shell.

Basic Database Operations

Once you are authenticated, you can create and manage databases. Here are some common tasks:

Displaying Databases

To list the internal databases installed by MySQL, use:
Sample output:

Creating a New Database

Create a new database called “school”:
Select the “school” database:

Creating Tables

Create a table named “persons” with columns for name, age, and location:
Insert a record into the “persons” table:
View the data in the table:
Expected output:
Verify table creation within the “school” database:
Sample output:

User Management and Authorization

For security and best practices in production, avoid using the root account for application access. Instead, create additional users with restricted privileges.

Creating a New User

To create a user (e.g., “john”) who connects from localhost:
For remote connections, specify the host IP address or use % to allow connections from any system:
Connect as the new user:

Granting Permissions

To authorize the user with appropriate privileges, use the GRANT command. For example, to allow user “john” to run SELECT queries on the persons table in the “school” database:
Grant multiple permissions in one command, such as SELECT and UPDATE on the persons table:
To grant privileges on all tables within the “school” database:
Granting all privileges should be done carefully. Use:
only when absolutely necessary.
To view the grants for a specific user:
Sample output:

Summary

By following these steps, you have:
  • Installed and started the MySQL server.
  • Connected to MySQL using the client utility.
  • Changed the default root password.
  • Created a new database and table.
  • Inserted and queried data.
  • Managed user creation and permissions.
Practice these commands in your environment to build a solid foundation for managing MySQL databases and preparing for more advanced topics, such as integrating web servers with databases and implementing multi-tier application security. For further details, refer to the MySQL Documentation.

Watch Video

Practice Lab