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):
File name: mysql-8.0.31-linux-glibc2.17-aarch64.tar.gz
2. Upload and Extract MySQL
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/
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
Navigate to
/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/
and createdata
andlog
directories.
3. Update Environment Variables
Open the environment variables file:
1
2cd ~
vim .bash_profileAdd the following line:
1
PATH=$PATH:$HOME/bin:/usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/bin
Apply the changes:
1
source .bash_profile
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
Create the
mysql
group and user:1
2groupadd mysql
useradd -r -g mysql -s /bin/false mysqlSet permissions for the MySQL directory:
1
chown -R mysql:mysql /usr/local/mysql-8.0.31-linux-glibc2.17-aarch64/
Create and set permissions for the
/var/lib/mysql
directory:1
2sudo mkdir /var/lib/mysql
sudo chown -R mysql:mysql /var/lib/mysql
5. Configure MySQL Parameters
Edit the configuration file:
1
vim /etc/my.cnf
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
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 themysql
user.--basedir
: Specifies the MySQL installation root directory.--datadir
: Specifies the MySQL data directory.
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
Create a systemd service file:
1
vim /usr/lib/systemd/system/mysqld.service
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
Reload systemd configuration:
1
systemctl daemon-reload
Start the MySQL service:
1
systemctl start mysqld
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
Switch to the MySQL database:
1
use mysql;
Allow external access (optional):
1
2
3CREATE USER 'root'@'%' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
flush privileges;Update the root password:
1
2alter user root@"localhost" identified with mysql_native_password by 'new_password';
flush privileges;
12. Exit MySQL and Restart
Exit the MySQL database using:
1 | mysql> exit |
13. External Connection
Open port 3306 on the firewall:
1
2firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reloadUse Navicat to connect to the MySQL server (Connection - MySQL):
- Host/IP:
192.168.50.158
- Username:
root
- Password:
your_password
- Host/IP:
Set the connection name to anything, enter the virtual machine’s IP address 192.168.50.158 as the host or IP address.