How to Set Up MySQL 8.0 on Linux

Let's quickly set up MySQL 8.0 on Linux

Posted by Byolio on November 23, 2024
🌐 中文 English

This article aims to set up MySQL 8.0 on a CentOS 7 server.

Before setting up, you need to have a virtual machine or server deployed and possess some knowledge of Linux.

Why Deploy MySQL on Linux

I believe there are four main reasons:

  1. Closer to Real-World Scenarios: Deploying MySQL on Linux is more aligned with real enterprise production environments compared to Windows, helping to familiarize oneself with server deployment and operations in advance.
  2. High-Performance Support: Enterprise Linux servers typically have higher software and hardware configurations, allowing for better testing of MySQL’s performance. (This post only covers a simple deployment and does not require an enterprise-level server.)
  3. Team Collaboration: Deploying MySQL on Linux enables team members to separately log into the server for management and development.
  4. Centralized Management: Teams can use a unified Linux environment to set up the database, avoiding development issues caused by differences in individual environments.

How to Deploy MySQL 8.0 on Linux

Many people first think of using tools like Xftp, Cyberduck, or FileZilla to transfer RPM packages. However, finding the corresponding RPM packages is not a simple task. Therefore, in this post, I will use a simpler and more efficient method: deploying the MySQL repository to download and install the MySQL RPM packages (it’s cool, isn’t it?).

  1. Deploy the MySQL repository with the following commands:
    1
    2
    3
    
    cd /opt
    wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
    sudo rpm -ivh mysql80-community-release-el7-7.noarch.rpm
    

    (The RPM package should follow the format: software name + version number + release count + system version + hardware architecture platform + extension.) If wget is not installed, execute the following command to download wget: yum install wget -y

  2. Deploy the core components of MySQL on the Linux system, including the MySQL service and necessary dependencies. Before deployment, ensure these two dependencies exist:
    1
    2
    
    rpm -qa | grep libaio
    rpm -qa | grep net-tools
    

    If the output is empty, install the dependencies:

    1
    2
    
    yum install -y libaio
    yum install -y net-tools
    

    Fetch the MySQL service and necessary dependencies:

    1
    2
    3
    4
    5
    
    wget http://example.com/mysql-community-common-8.0.25-1.el7.x86_64.rpm
    wget http://example.com/mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
    wget http://example.com/mysql-community-libs-8.0.25-1.el7.x86_64.rpm
    wget http://example.com/mysql-community-client-8.0.25-1.el7.x86_64.rpm
    wget http://example.com/mysql-community-server-8.0.25-1.el7.x86_64.rpm
    
  3. Install the MySQL service and necessary dependencies: Due to dependencies between components, install them in the following order to avoid dependency errors:
    1
    2
    3
    4
    5
    
    rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
    rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
    

    Since MariaDB is forked from the MySQL source code branch, it may conflict with MySQL. If you encounter a conflict error with mariadb-libs, execute the following command:

    1
    
    yum remove -y mariadb-libs
    

    Then continue with the installation.

  4. Start the MySQL service:
    1
    2
    3
    
    systemctl start mysqld;
    systemctl enable mysqld;
    systemctl status mysqld;
    
  5. Check if the installation is complete: mysql --version

Other Issues

Database Initialization

  1. View the initial password by entering the following command:
    cat /var/log/mysqld.log
    Find the line: A temporary password is generated for root@localhost: ******* (Here, ******* is the initial password. Copy it.)
  2. Log in to MySQL:
    mysql -uroot -p
    Then paste the initial password to log in.
  3. Change the password:
    After logging in, you must change the password to continue. Execute the following code:
    1
    
    alter user 'root'@'localhost' identified by '******';
    

    Here, ****** is the new password.

Remote Login

If you want to log in to MySQL remotely using a client, log in to MySQL and enter the following code to modify permissions:

1
2
3
4
USE mysql;
update user set host = '######' where user = 'root';
ALTER USER 'root'@'######' IDENTIFIED WITH mysql_native_password BY '******';
flush privileges;

(Here, ###### is the address to grant permissions to. For learning purposes, you can directly enter: %. ****** is the new password.) Then log in via the client.

Permission Errors

If permission modifications in MySQL cause login issues, you can skip permission checks by executing the following commands: (To prevent the root user from damaging MySQL, you cannot directly use the root user to skip permission login):

1
2
3
4
5
systemctl stop mysqld;
useradd -r -s /bin/false mysql
chown -R mysql:mysql /var/lib/mysql
su -s /bin/bash mysql
mysqld --skip-grant-tables &

Then log in directly to MySQL and modify the permissions.

Summary

Above is my method for setting up MySQL on Linux. If you have any questions, feel free to comment in the section below.