MySQL Installation
The download link for MySQL for all platforms is: MySQL Download. Select the version of MySQL Community Server and the corresponding platform you need.
>
Note: During the installation process, we need to enable administrator privileges to install, otherwise the installation will fail due to insufficient permissions.
Installing MySQL on Linux/UNIX
On Linux platforms, it is recommended to use RPM packages to install MySQL. MySQL AB provides the following RPM packages for download:
- MySQL - MySQL server. You need this option unless you only want to connect to a MySQL server running on another machine.
- MySQL-client - MySQL client programs used to connect to and operate the MySQL server.
- MySQL-devel - Libraries and include files necessary if you want to compile other MySQL clients, such as Perl modules.
- MySQL-shared - This package contains shared libraries (libmysqlclient.so*) that certain languages and applications need to dynamically load to use MySQL.
- MySQL-bench - Benchmark and performance testing tools for the MySQL database server.
Before installation, we can check if the system already has MySQL installed:
rpm -qa | grep mysql
If MySQL is installed, you can choose to uninstall it:
rpm -e mysql // Normal uninstall mode
rpm -e --nodeps mysql // Force uninstall mode, used if the above command indicates dependencies
Installing MySQL:
Next, we will install MySQL using the yum command on CentOS 7. Note that MySQL has been removed from the default program list in CentOS 7, so we need to download the Yum resource package from the official website before installation. The download link is: https://dev.mysql.com/downloads/repo/yum/
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
yum install mysql-server
Set permissions:
chown -R mysql:mysql /var/lib/mysql/
Initialize MySQL:
mysqld --initialize
Start MySQL:
systemctl start mysqld
Check MySQL status:
systemctl status mysqld
Note: If this is the first time you are starting the MySQL service, the MySQL server will first perform an initial configuration.
>
Alternatively, you can also use MariaDB, which is a fork of MySQL primarily maintained by the open-source community under the GPL license. One reason for this fork is the potential risk of MySQL being closed-sourced after being acquired by Oracle. MariaDB aims to be fully compatible with MySQL, including API and command line, making it an easy substitute.
yum install mariadb-server mariadb
Commands for MariaDB database are:
systemctl start mariadb # Start MariaDB
systemctl stop mariadb # Stop MariaDB
systemctl restart mariadb # Restart MariaDB
systemctl enable mariadb # Set to start on boot
Verifying MySQL Installation
After successfully installing MySQL, some basic tables will be initialized. After starting the server, you can verify if MySQL is working correctly with a simple test.
Use the mysqladmin tool to check server status:
Use the mysqladmin command to check the server version. On Linux, this binary is located in the /usr/bin directory, and on Windows, it is located in C:\mysql\bin.
[root@host]# mysqladmin --version
On Linux, this command will output the following result based on your system information:
mysqladmin Ver 8.23 Distrib 5.0.9-0, for redhat-linux-gnu on i386
If the above command does not output any information, your MySQL is not installed successfully.
Executing Simple SQL Commands Using MySQL Client
You can connect to the MySQL server using the mysql command in the MySQL Client. By default, the MySQL server has an empty password for login, so no password is needed in this example.
Command:
[root@host]# mysql
After executing the above command, you will see the mysql> prompt, indicating a successful connection to the MySQL server. You can execute SQL commands at the mysql> prompt:
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.13 sec)
Post-Installation Steps for MySQL
After successfully installing MySQL, the default root user password is empty. You can create a password for the root user using the following command:
[root@host]# mysqladmin -u root password "new_password";
Now you can connect to the MySQL server using the following command:
[root@host]# mysql -u root -p
Enter password:*******
Note: When entering the password, it will not be displayed. Just enter it correctly.
Installing MySQL on Windows
Installing MySQL on Windows is relatively simple. The latest version can be downloaded from MySQL Download (for more detailed installation instructions).
After downloading, unzip the zip file to the desired directory. Here, I have placed the unzipped folder in C:\web\mysql-8.0.11.
Next, we need to configure the MySQL configuration file:
Open the unzipped folder C:\web\mysql-8.0.11 and create a my.ini configuration file. Edit the my.ini file to include the following basic settings:
[client]
# Set the default character set for the MySQL client
default-character-set=utf8
[mysqld]
# Set port 3306
port = 3306
# Set the installation directory of MySQL
basedir=C:\\web\\mysql-8.0.11
# Set the data storage directory for the MySQL database. MySQL 8+ does not require this configuration as it is automatically generated by the system.
# datadir=C:\\web\\sqldata
# Allow maximum connections
max_connections=20
# Server-side character set default is 8-bit encoded latin1
character-set-server=utf8
# Default storage engine when creating new tables
default-storage-engine=INNODB
Next, let's start the MySQL database:
Open the command prompt as an administrator and switch directories:
cd C:\web\mysql-8.0.11\bin
Initialize the database:
mysqld --initialize --console
After execution, the initial default password for the root user will be output, for example:
...
2018-04-20T02:35:05.464644Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: APWCY5ws&hjQ
...
APWCY5ws&hjQ
is the initial password, which will be needed for subsequent logins, and you can also change it after logging in.
Enter the following installation command:
mysqld install
Start with the following command:
net start mysql
>
Note: In version 5.7, you need to initialize the data directory:
cd C:\web\mysql-8.0.11\bin
mysqld --initialize-insecure
After initialization, run net start mysql
to start MySQL.
Logging into MySQL
When the MySQL service is running, you can log into the MySQL database using the MySQL client tool. First, open the command prompt and enter the following command:
mysql -h hostname -u username -p
Parameter explanations:
- -h : Specifies the hostname of the MySQL server the client is trying to log into. This parameter can be omitted for local (localhost or 127.0.0.1) connections.
- -u : Login username.
- -p : Indicates that a password will be used for login. This option can be omitted if the username has an empty password.
If you are logging into the local MySQL database, you only need to enter the following command:
mysql -u root -p
Press Enter to confirm. If installed correctly and MySQL is running, you will get the following prompt:
Enter password:
If a password exists, enter the password to log in. If no password exists, press Enter directly. After successful login, you will see the "Welcome to the MySQL monitor..." prompt.
The command prompt will then remain in the mysql>
state with a blinking cursor, waiting for commands. Enter exit or quit to log out.