Installing MySQL on a Linux Server

Checking for Existing MySQL Installations

First, check if MySQL is already installed on the current system.

  • rpm -qa: Lists all installed software on the system (qa means query all).
  • rpm -qa | grep mysql: Finds installed software containing “mysql” in its name.
  • rpm -qa | grep mariadb: Finds installed software containing “mariadb” in its name.

Note: If MariaDB is installed, it must be uninstalled first; otherwise, the MySQL installation will fail.

Uninstall command:

1
rpm -e --nodeps package_name

For example:

1
rpm -e --nodeps mariadb-libs-xxxxxxx

Steps to Install and Configure MySQL

1. Download MySQL

Download the MySQL 8.0.31 ARM64 version (for macOS M2 chip):

Download Link

File name: mysql-8.0.31-linux-glibc2.17-aarch64.tar.gz

2. Upload and Extract MySQL

  1. Use sftp to upload the installation package to the Linux server:

    1
    put /Users/kathy/Downloads/linux-software/mysql-8.0.31-linux-glibc2.17-aarch64.tar.gz /root/
  1. On the virtual machine, extract the package to /usr/local/mysql (create the directory first):

    1
    tar -zxvf mysql-8.0.31-linux-glibc2.17-aarch64.tar.gz -C /usr/local/mysql
  2. Navigate to /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/ and create data and log directories.

3. Update Environment Variables

  1. Open the environment variables file:

    1
    2
    cd ~
    vim .bash_profile
  2. Add the following line:

    1
    PATH=$PATH:$HOME/bin:/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/bin
  3. Apply the changes:

    1
    source .bash_profile
  4. Verify the mysql command path:

    1
    which mysql

    Output: /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/bin/mysql

4. Create MySQL User and Group

  1. Create the mysql group and user:

    1
    2
    groupadd mysql
    useradd -r -g mysql -s /bin/false mysql
  2. Set permissions for the MySQL directory:

    1
    chown -R mysql:mysql /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/
  3. Create and set permissions for the /var/lib/mysql directory:

    1
    2
    sudo mkdir /var/lib/mysql
    sudo chown -R mysql:mysql /var/lib/mysql

5. Configure MySQL Parameters

  1. Edit the configuration file:

    1
    vim /etc/my.cnf
  2. Add the following content:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [mysql]
    default-character-set=utf8mb4
    socket=/var/lib/mysql/mysql.sock

    [mysqld]
    port=3306
    socket=/var/lib/mysql/mysql.sock
    basedir=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64
    character-set-server=utf8mb4
    default-storage-engine=INNODB
    innodb_buffer_pool_size=200M
    max_allowed_packet=16M
    explicit_defaults_for_timestamp=1

    log-output=FILE
    general_log=0
    general_log_file=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/log/general.err
    slow_query_log=ON
    slow_query_log_file=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/log/query.err
    long_query_time=10
    log-error=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/log/error.err
    default-authentication-plugin=mysql_native_password

6. Initialize and Start MySQL

  1. Initialize MySQL:

    1
    /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64 --datadir=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/data

    Explanation:

    • /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/bin/mysqld: Start the MySQL mysqld service
    • --initialize: Initializes the MySQL data directory.
    • --user=mysql: Runs the MySQL service as the mysql user.
    • --basedir: Specifies the MySQL installation root directory.
    • --datadir: Specifies the MySQL data directory.
  2. Retrieve the generated temporary root password:

    1
    cat /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/log/error.err

    Example output:

    1
    A temporary password is generated for root@localhost: **********

    You can save this temporary password.

7. Add MySQL to systemd Management

  1. Create a systemd service file:

    1
    vim /usr/lib/systemd/system/mysqld.service
  2. Add the following content:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target

    [Install]
    WantedBy=multi-user.target

    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/bin/mysqld --defaults-file=/etc/my.cnf
    LimitNOFILE=65536
    LimitNPROC=65536

8. Enable and Start MySQL Service

  1. Reload systemd configuration:

    1
    systemctl daemon-reload
  2. Start the MySQL service:

    1
    systemctl start mysqld
  3. Enable MySQL to start on boot:

    1
    systemctl enable mysqld

    After execution, it displays: Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

9. Check MySQL Service Status

1
systemctl status mysqld

This command to check the status of the MySQL service allows you to verify whether the MySQL service is running, view the process ID of the service, and see the most recent status change time. If the MySQL service fails to start properly, this command can also provide error information to help diagnose the issue.

To see active network connections (optional):

1
netstat -tunlp

10. Log in to MySQL

Log in using the root user:

1
mysql -u root -p

If you encounter an error like libtinfo.so.5 missing, then execute:

1
ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5

11. Change the Root Password

  1. Switch to the MySQL database:

    1
    use mysql;   
  2. Allow external access (optional):

    1
    2
    3
    CREATE USER 'root'@'%' IDENTIFIED BY 'PASSWORD';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    flush privileges;
  3. Update the root password:

    1
    2
    alter user root@"localhost" identified with mysql_native_password by 'new_password';
    flush privileges;

12. Exit MySQL and Restart

Exit the MySQL database using:

1
2
3
4
5
   mysql> exit
```
Then restart the MySQL service with the following command:
```bash
systemctl restart mysqld

13. External Connection

  1. Open port 3306 on the firewall:

    1
    2
    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    firewall-cmd --reload
  2. Use Navicat to connect to the MySQL server (Connection - MySQL):

    • Host/IP: 192.168.50.158
    • Username: root
    • Password: your_password

Set the connection name to anything, enter the virtual machine’s IP address 192.168.50.158 as the host or IP address.