<\/span><\/h2>\n\n\n\nInstall the MySQL database server with the following command:<\/p>\n\n\n\n
sudo apt install mysql-server -y<\/pre>\n\n\n\nAfter this command is executed the installation process of MySQL 8.0 will start. The MySQL 8.0 is included by default in the Ubuntu 20.04 repository. Once the installation is completed you can check if the service is up and running:<\/p>\n\n\n\n
sudo systemctl status mysql<\/pre>\n\n\n\nYou should get the following output:<\/p>\n\n\n\n
root@vps:~# systemctl status mysql\n\u25cf mysql.service - MySQL Community Server\n Loaded: loaded (\/lib\/systemd\/system\/mysql.service; enabled; vendor preset: enabled)\n Active: active (running) since Thu 2021-11-04 17:07:12 UTC; 39s ago\n Main PID: 98088 (mysqld)\n Status: \"Server is operational\"\n Tasks: 38 (limit: 4617)\n Memory: 355.4M\n CGroup: \/system.slice\/mysql.service\n \u2514\u250098088 \/usr\/sbin\/mysqld<\/pre>\n\n\n\n<\/span>3. Secure the MySQL server<\/span><\/h2>\n\n\n\nBefore we start using MySQL, we need to secure it with multiple steps with the following command:<\/p>\n\n\n\n
sudo mysql_secure_installation<\/pre>\n\n\n\nThis command will take us through multiple steps which will help us to configure the security of our MySQL, set a strong root password and, etc. You should use the options with bold letters from the output below:<\/p>\n\n\n\n
root@vps:~# sudo mysql_secure_installation\n\nSecuring the MySQL server deployment.\n\nConnecting to MySQL using a blank password.\n\nVALIDATE PASSWORD COMPONENT can be used to test passwords\nand improve security. It checks the strength of password\nand allows the users to set only those passwords which are\nsecure enough. Would you like to setup VALIDATE PASSWORD component?\n\nPress y|Y for Yes, any other key for No: Y <\/b>\n\nThere are three levels of password validation policy:\n\nLOW Length >= 8\nMEDIUM Length >= 8, numeric, mixed case, and special characters\nSTRONG Length >= 8, numeric, mixed case, special characters and dictionary file\n\nPlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2<\/b>\nPlease set the password for root here.\n\nNew password: YourStrongRootPassword<\/b>\n\nRe-enter new password:YourStrongRootPassword<\/b>\n\nEstimated strength of the password: 100\nDo you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y<\/b>\nBy default, a MySQL installation has an anonymous user,\nallowing anyone to log into MySQL without having to have\na user account created for them. This is intended only for\ntesting, and to make the installation go a bit smoother.\nYou should remove them before moving into a production\nenvironment.\n\nRemove anonymous users? (Press y|Y for Yes, any other key for No) : Y<\/b>\nSuccess.\n\n\nNormally, root should only be allowed to connect from\n'localhost'. This ensures that someone cannot guess at\nthe root password from the network.\n\nDisallow root login remotely? (Press y|Y for Yes, any other key for No) : Y<\/b>\nSuccess.\n\nBy default, MySQL comes with a database named 'test' that\nanyone can access. This is also intended only for testing,\nand should be removed before moving into a production\nenvironment.\n\n\nRemove test database and access to it? (Press y|Y for Yes, any other key for No) : Y<\/b>\n - Dropping test database...\nSuccess.\n\n - Removing privileges on test database...\nSuccess.\n\nReloading the privilege tables will ensure that all changes\nmade so far will take effect immediately.\n\nReload privilege tables now? (Press y|Y for Yes, any other key for No) : Y<\/b>\nSuccess.\n\nAll done!<\/pre>\n\n\n\nThe MySQL installation will be secured now and we can proceed with some useful commands in the next steps<\/p>\n\n\n\n
<\/span>4. Login to MySQL server with and without root password<\/span><\/h2>\n\n\n\nAfter fresh installation of MySQL, you can log in to MySQL prompt by only typing the “mysql<\/b>” command on the servers command line without MySQL to ask the root password that you set up in the previous step.<\/p>\n\n\n\n
After typing the “mysql<\/b>” you will be logged in the MySQL prompt.<\/p>\n\n\n\nroot@vps:~# mysql\nWelcome to the MySQL monitor. Commands end with ; or \\g.\nYour MySQL connection id is 13\nServer version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu)\n\nCopyright (c) 2000, 2021, Oracle and\/or its affiliates.\n\nOracle is a registered trademark of Oracle Corporation and\/or its\naffiliates. Other names may be trademarks of their respective\nowners.\n\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.\n\nmysql><\/pre>\n\n\n\nThe same will be if you execute the following command and hit enter without typing your password:<\/p>\n\n\n\n
mysql -u root -p<\/pre>\n\n\n\nTo configure the MySQL to ask for root password execute the following command in the MySQL prompt:<\/p>\n\n\n\n
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongRootPassword';<\/pre>\n\n\n\nYou should see the following input after executing<\/p>\n\n\n\n
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongRootPassword';\nQuery OK, 1 rows affected (0.01 sec)\n\nmysql<\/pre>\n\n\n\nNow, if you try to login with “mysql<\/b>” command you will get the message below:<\/p>\n\n\n\nroot@vps:~# mysql\nERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)<\/pre>\n\n\n\nYou need to use the command below, with your previously set root password:<\/p>\n\n\n\n
mysql -u root -p<\/pre>\n\n\n\nIt is your choice to decide if you want to use a password for root login or not. If you want to access without the password simply ignore this step and move on.<\/p>\n\n\n\n
<\/span>5. Database, User creation and Granting Privileges<\/span><\/h2>\n\n\n\nIf you list the databases in the MySQL prompt with command “show databases” you will get the default databases from MySQL:<\/p>\n\n\n\n
mysql> show databases;<\/b>\n+--------------------+\n| Database |\n+--------------------+\n| information_schema |\n| mysql |\n| performance_schema |\n| sys |\n+--------------------+\n4 rows in set (0.01 sec)<\/pre>\n\n\n\nTo create a database, execute the following command:<\/p>\n\n\n\n
Create database 'admin';<\/pre>\n\n\n\nOnce, it is created you can list again the databases:<\/p>\n\n\n\n
mysql> show databases;\n+--------------------+\n| Database |\n+--------------------+\n| admin |\n| information_schema |\n| mysql |\n| performance_schema |\n| sys |\n+--------------------+\n5 rows in set (0.00 sec)\n\nmysql>\n<\/pre>\n\n\n\nLet’s create a user named “adminuser” and grant privileges to the “admin” database with a password.<\/p>\n\n\n\n
CREATE USER 'adminuser'@'localhost' IDENTIFIED by 'YourStrongPassword';\n GRANT ALL PRIVILEGES ON admin* TO 'adminuser'@'localhost';\n FLUSH PRIVILEGES;\n EXIT;\n<\/pre>\n\n\n\nOnce, the database user is created with privileges to the “admin<\/b>” database you can easily check if everything is set up correctly:<\/p>\n\n\n\nmysql -u adminuser -p<\/pre>\n\n\n\nYou should see only the “admin<\/b>” and the default “information_schema<\/b>” databases:<\/p>\n\n\n\nmysql> show databases;\n+--------------------+\n| Database |\n+--------------------+\n| admin |\n| information_schema |\n+--------------------+\n2 rows in set (0.01 sec)\n\nmysql>\n<\/pre>\n\n\n\n