This article intends to teach you to basic MySQL in Linux and shows you how to install it on different Linux operation systems. In simple words, MySQL is a database management system. By using MySQL you can easily create and manage your databases.
How To Learn Basic MySQL In Linux?
To install MySQL in Linux, you must have access to your server as a root or non-root user with sudo privileges.
Now follow the steps below to complete this guide:
Step 1 – Install MySQL on Linux
The installation of MySQL is so easy. To do this, we have MySQL installation instructions on different operating systems, you can visit them to see how it works:
How To Install MySQL on Centos 7
How To Install MySQL 8.0 on Debian 11
Step 2 – How To Secure MySQL?
To secure your MySQL installation, you can use a MySQL security script that has been provided by MySQL. To do this, you can run the command below:
This command will ask you some questions. First, you must press enter once and enter the MySQL login password. (default password is empty).
Next, answer the other questions as your wish. It is recommended that answer them by pressing Y.
Step 3 – How To Work with MySQL?
When the installation of MySQL in Linux is finished you can see basic usage of it in the rest of the article.
Access MySQL Shell
At this point, you can access MySQL shell by using the following command:
mysql -u root –p
You should enter the root MySQL password and press enter.
Note: Remember to end your commands with “;”. MySQL commands are usually written in uppercase.
Create and Delete a MySQL Database
You can easily create a database in MySQL in Linux with the following command: Here we named it orcadb, you can choose your desired name.
mysql> CREATE DATABASE orcadb;
Then you can check available databases with the following command:
mysql> SHOW DATABASES;
You can also delete a database easily with the following command:
mysql> DROP DATABASE orcadb;
If you check your available databases there is no more a database named orcadb.
How To Access a MySQL Database?
From your MySQL database, you can put information in your new database. Open your database with the following command:
mysql> USE orcadb;
Then you can see its tables with the following command:
Because it is a new database there is nothing to show.
Let’s see how to create a MySQL table for your database.
How To Create a MySQL Table?
You can create a table with the CREATE TABLE command in MySQL shell. For example:
mysql> CREATE TABLE orca (name VARCHAR(20), age VARCHAR(10), email VARCHAR(20) );
This command will create a table named orca. And the table has some variables such as name, age, and email.
You can see tables with the SHOW TABLES command:
mysql> SHOW TABLES;
How To Add Data to a MySQL Table?
In MySQL in Linux, you can use the format below to put your information into the table:
mysql> INSERT INTO `orca` (`name`, `age`,`email`) VALUES (“user1”,’25’,”[email protected]”);
You can look at your table with the following command:
SELECT * FROM orca
How To Update MySQL Table Data?
You can use the following command to update your information in the table:
mysql> UPDATE `orca` SET `age` = '32' WHERE `orca`.`name` ='user1';
How To Add and Delete a MySQL Table Column?
You can easily add a column with the following command:
ALTER TABLE orca ADD lastname VARCHAR(40);
If you want that your column is in a specific place use the command below:
ALTER TABLE orca ADD lastname VARCHAR(40) AFTER name;
You can also delete a column with the following DROP command:
ALTER TABLE orca DROP lastname;
You can easily delete a row with the following command:
DELETE from [table name] where [column name]=[field text];
At this point, you have learned to learn a basic Mysql and you can create tables, add and delete columns, etc on Linux.
Hope you enjoy it. You may be interested in the following MySQL articles:
Find MySQL Configuration File Location on Linux
Install MySQL Workbench on Ubuntu 22.04
How To Create a New User on MySQL