Home  >  Article  >  Operation and Maintenance  >  How to install and configure a MySQL database on Linux

How to install and configure a MySQL database on Linux

WBOY
WBOYOriginal
2023-07-05 16:13:372198browse

How to install and configure the MySQL database on Linux

Foreword:
MySQL is an open source relational database management system that is widely used in the development and management of Web applications. Installing and configuring MySQL on a Linux system can help us build an efficient and stable database environment. This article will introduce how to install and configure MySQL on Linux, and provide corresponding code examples.

1. Install MySQL

  1. Open the terminal and enter the following command to update the package list:

sudo apt-get update

  1. Enter the following command to install MySQL server:

sudo apt-get install mysql-server

  1. During the installation process, you will be prompted to enter MySQL root The user's password. Please set a strong password and make sure you remember it.

2. Configure MySQL

  1. Start the MySQL service:

sudo service mysql start

  1. Configure MySQL Security:

sudo mysql_secure_installation

After executing the above command, you will be asked to enter the MySQL root password to perform subsequent operations. Follow the prompts to complete the security configuration step by step.

3. Connect to MySQL

  1. Open the terminal and enter the following command to connect to the MySQL database server:

mysql -u root -p

Enter the MySQL root password after the prompt to successfully connect to the MySQL server.

4. Create a database

  1. After connecting to the MySQL server, you can use the following command to create a new database:

CREATE DATABASE database_name;

Where database_name is the name of the database you want to create.

5. Create users and assign permissions

  1. Use the following command to create a new MySQL user:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

where username is the username you want to create, and password is the password you want to set.

  1. Grant new users permissions on the database:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

6. Exit MySQL

  1. In the terminal connected to the MySQL server, you can use the following command to exit MySQL:

exit;

7. Common command examples

  1. Show all databases:

SHOW DATABASES;

  1. Use the specified database:

USE database_name;

  1. Display all tables in the current database:

SHOW TABLES;

  1. Display table structure:

DESCRIBE table_name;

  1. Insert data into the table:

INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3);

8. Summary

This article introduces how to install and configure the MySQL database on Linux, and provides relevant code examples. By following the steps in this article, you can successfully set up a MySQL database environment and use common commands to operate in the database. Hope this article will be helpful to you.

Reference materials:

  • https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/linux-installation.html
  • https://www.mysqltutorial.org/mysql-create-table/
  • https://www.mysqltutorial.org/mysql-insert-statement.aspx

The above is the detailed content of How to install and configure a MySQL database on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn