📜  setup mysql ubuntu - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:20:06.991000             🧑  作者: Mango

Setting up MySQL on Ubuntu

MySQL is an open-source relational database management system that is widely used by developers to store and manage data. This guide will walk you through the process of setting up MySQL on Ubuntu, a popular Linux distribution.

Prerequisites

Before starting, ensure that you have a Ubuntu machine or virtual machine set up. You should also have root or sudo access to install packages and make system-level changes.

Step 1: Update System Packages

Before installing MySQL, let's update the system packages to their latest versions:

sudo apt update
sudo apt upgrade -y
Step 2: Install MySQL Server

Now, let's install the MySQL server package using the following command:

sudo apt install mysql-server -y

During the installation, you will be prompted to select and confirm a password for the MySQL root user. Choose a strong password and make sure to remember it.

Step 3: Secure MySQL Installation

After the installation, it is recommended to run the security script provided by MySQL. This script will guide you through some security-related steps and remove unnecessary default settings:

sudo mysql_secure_installation

Follow the prompts and answer the questions according to your preference. This will help to enhance the security of your MySQL installation.

Step 4: Verify MySQL Installation

To ensure that MySQL is up and running, you can use the following command to check the service status:

sudo systemctl status mysql

If MySQL is running, you should see an output indicating that the service is active and running.

Optional: Create a New MySQL User and Database

By default, MySQL comes with a root user that has full privileges. However, it is good practice to create a separate user and database for your applications:

sudo mysql

Enter your MySQL root password when prompted. Once inside the MySQL prompt, you can create a new user and database:

CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace your_username, your_password, and your_database with your desired values.

Conclusion

Congratulations! You have successfully set up MySQL on Ubuntu. You can now use it to store and manage your data in various applications. Remember to always follow best practices for securing your database and regularly update your system to ensure the latest security patches.