MySQL on Linux

1. Install

CentOS 7:

Import the key before running the installer:

$ sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
  • Download and install:

$ sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm
$ sudo yum install mysql-server -y
  • Enable and start MySQL service:

$ sudo systemctl restart mysqld.service
$ sudo systemctl enable mysqld.service
  • Setting a permanent root password and MySQL security settings:

MySQL expects that your new password should consist of at least 8 characters, contain uppercase and lowercase letters, numbers and special characters (do not forget the password you set, it will come in handy later). After a successful password change, the following questions are recommended to answer "Y":

Note:

  • In CentOS 7, the default root password can be found using

$ sudo grep "A temporary password" /var/log/mysqld.log

after receiving the password, run mysql_secure_installation, for setup new root password:

$ sudo mysql_secure_installation

The following is an example for CentOs 7:

Enter password for user root:

  The existing password for the user account root has expired. Please set a new password.

  New password:
  Re-enter new password:

  Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

  Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

  Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

  Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

Ubuntu 18.04:

$ wget -c  https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb
$ sudo dpkg -i mysql-apt-config_0.8.22-1_all.deb

Note:

  • click "ok" to confirm the server installation

$ sudo apt update
$ sudo apt install mysql-server -y

during the installation you will be prompted to enter the MySQL user password. Remember this password for future use.

  • run mysql_secure_installation, to complete the installation:

$ sudo mysql_secure_installation

Ubuntu 20.04, 22.04:

$ sudo apt install mysql-server -y
  • Change the authentication parameters:

$ sudo mysql 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '<mysql_root_password>';

Сhange <mysql_root_password> to a strong password,

To exit from the MySQL console, press Ctrl+D.

  • run mysql_secure_installation, to complete the installation:

$ sudo mysql_secure_installation

2. Creating MySQL User and Database

2.1. Run MySQL client and enter your MySQL root password

sudo mysql -h localhost -u root -p

2.2. The following lines create a database hesdb, the user with name hesuser and password <user_password>. Сhange <user_password> to a strong password, otherwise you may get a password validator error.

mysql> CREATE DATABASE hesdb;
mysql> CREATE USER 'hesuser'@'%' IDENTIFIED BY '<user_password>';
mysql> GRANT ALL ON hesdb.* TO 'hesuser'@'%';
mysql> FLUSH PRIVILEGES;

To exit from the MySQL console, press Ctrl+D.