MySQL Database Setup In Ubuntu

Absolutely. Here is a practical MySQL setup + environment + basic DDL/DML tutorial on Ubuntu, using the Bash terminal.

1. Update Ubuntu

Open Terminal:

sudo apt update
sudo apt upgrade -y

2. Install MySQL Server

sudo apt install mysql-server -y

Check installation:

mysql --version

You should see something similar to:

mysql  Ver 8.0.x for Linux on x86_64

3. Check MySQL service

sudo systemctl status mysql

If it isn’t running:

sudo systemctl start mysql

Enable MySQL to start automatically when Ubuntu boots:

sudo systemctl enable mysql

Useful commands:

sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql
sudo systemctl status mysql

4. Secure MySQL

Run:

sudo mysql_secure_installation

For a beginner/local development machine, you can generally follow the prompts and remove anonymous users, disallow remote root login, and remove the test database.


5. Login to MySQL

On Ubuntu, you can usually enter MySQL with:

sudo mysql

You’ll see:

mysql>

This is the MySQL shell, not the normal Bash shell.

To exit:

exit;

6. Understand Bash vs MySQL

There are two different environments:

Bash:

$ sudo systemctl status mysql

MySQL shell:

mysql> SHOW DATABASES;

Don’t type the $ or mysql> prompt yourself.


7. Create a Database — DDL

genui{“learning_viz”:{“type_id”:”SQL_DDL_VS_DML”}}

Enter MySQL:

sudo mysql

Create a database:

CREATE DATABASE college;

See databases:

SHOW DATABASES;

Select the database:

USE college;

Check the current database:

SELECT DATABASE();

8. Create a Table

This is also DDL — Data Definition Language.

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT,
    email VARCHAR(150),
    course VARCHAR(100)
);

Check tables:

SHOW TABLES;

See table structure:

DESCRIBE students;

or:

DESC students;

9. DML — Insert Data

DML means Data Manipulation Language.

Insert one student:

INSERT INTO students (name, age, email, course)
VALUES ('Rahul', 21, 'rahul@example.com', 'Java');

Insert multiple students:

INSERT INTO students (name, age, email, course)
VALUES
('Amit', 22, 'amit@example.com', 'Python'),
('Priya', 20, 'priya@example.com', 'Java'),
('Neha', 21, 'neha@example.com', 'SQL');

10. Read Data — SELECT

Display everything:

SELECT * FROM students;

Select specific columns:

SELECT name, course
FROM students;

Find students studying Java:

SELECT *
FROM students
WHERE course = 'Java';

Find students older than 20:

SELECT *
FROM students
WHERE age > 20;

11. UPDATE Data

Change a student’s course:

UPDATE students
SET course = 'Spring Boot'
WHERE name = 'Rahul';

Verify:

SELECT * FROM students;

Important: Always be careful with UPDATE.

This:

UPDATE students
SET course = 'Java';

updates every row.

Usually you want:

UPDATE students
SET course = 'Java'
WHERE id = 1;

12. DELETE Data

Delete one student:

DELETE FROM students
WHERE id = 2;

Check:

SELECT * FROM students;

Again, be careful.

DELETE FROM students;

deletes all rows from the table.


13. More Important DDL Commands

ALTER TABLE — Add column

ALTER TABLE students
ADD phone VARCHAR(20);

Check:

DESC students;

Modify column

ALTER TABLE students
MODIFY age SMALLINT;

Rename column

For MySQL 8:

ALTER TABLE students
RENAME COLUMN phone TO mobile;

Drop column

ALTER TABLE students
DROP COLUMN mobile;

Rename table

RENAME TABLE students TO student;

Drop table

DROP TABLE student;

Warning: DROP TABLE removes the table itself.


14. DROP vs DELETE vs TRUNCATE

This distinction is very important:

CommandWhat happens
DELETEDeletes rows
TRUNCATERemoves all rows, keeps table
DROPRemoves table itself

Example:

DELETE FROM students WHERE id = 1;

Delete all rows:

DELETE FROM students;

or:

TRUNCATE TABLE students;

Remove the entire table:

DROP TABLE students;

15. Useful SQL Commands

Show databases:

SHOW DATABASES;

Select database:

USE college;

Show tables:

SHOW TABLES;

Describe table:

DESC students;

Show table creation SQL:

SHOW CREATE TABLE students;

Current database:

SELECT DATABASE();

Exit:

EXIT;

16. Complete Practice Session

You can copy this entire block into the MySQL terminal:

CREATE DATABASE college;

USE college;

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT,
    email VARCHAR(150),
    course VARCHAR(100)
);

INSERT INTO students (name, age, email, course)
VALUES
('Rahul', 21, 'rahul@example.com', 'Java'),
('Amit', 22, 'amit@example.com', 'Python'),
('Priya', 20, 'priya@example.com', 'Java'),
('Neha', 21, 'neha@example.com', 'SQL');

SELECT * FROM students;

SELECT name, course
FROM students
WHERE course = 'Java';

UPDATE students
SET course = 'Spring Boot'
WHERE name = 'Rahul';

SELECT * FROM students;

DELETE FROM students
WHERE name = 'Amit';

SELECT * FROM students;

17. Running MySQL Directly from Bash

You don’t always have to enter the MySQL shell.

For example:

sudo mysql -e "SHOW DATABASES;"

Run a query:

sudo mysql -e "USE college; SELECT * FROM students;"

You can also execute a SQL file.

Create a file:

nano college.sql

Put SQL inside:

CREATE DATABASE IF NOT EXISTS college;

USE college;

CREATE TABLE IF NOT EXISTS students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT,
    course VARCHAR(100)
);

INSERT INTO students (name, age, course)
VALUES
('Rahul', 21, 'Java'),
('Priya', 20, 'SQL');

Save with:

Ctrl + O → Enter → Ctrl + X

Then run:

sudo mysql < college.sql

Verify:

sudo mysql -e "USE college; SELECT * FROM students;"

18. DDL vs DML Cheat Sheet

DDL — structure

CREATE
ALTER
DROP
TRUNCATE

Examples:

CREATE DATABASE college;
CREATE TABLE students (...);

ALTER TABLE students ADD phone VARCHAR(20);

TRUNCATE TABLE students;

DROP TABLE students;

DML — data

INSERT
UPDATE
DELETE

Examples:

INSERT INTO students (...) VALUES (...);

UPDATE students
SET age = 22
WHERE id = 1;

DELETE FROM students
WHERE id = 1;

DQL — querying data

SELECT

Example:

SELECT * FROM students;

The basic learning sequence

Ubuntu
  ↓
Bash
  ↓
Install MySQL
  ↓
Start MySQL service
  ↓
mysql shell
  ↓
CREATE DATABASE
  ↓
CREATE TABLE
  ↓
INSERT
  ↓
SELECT
  ↓
UPDATE
  ↓
DELETE
  ↓
ALTER / DROP

If you’re learning this alongside your Java + Maven setup, the natural next step is to build a small Java Maven application that connects to this MySQL database using JDBC, performs INSERT/SELECT/UPDATE/DELETE, and runs entirely from the Ubuntu terminal.

Yes. DCL (Data Control Language) is used to control user permissions and access in MySQL.

The two main DCL commands you should learn are:

GRANT
REVOKE

1. Login to MySQL

From Ubuntu Bash:

sudo mysql

Then:

mysql>

2. Create a MySQL User

First create a database:

CREATE DATABASE college;

Create a user:

CREATE USER 'student'@'localhost'
IDENTIFIED BY 'Student@123';

Check users:

SELECT User, Host
FROM mysql.user;

3. GRANT

GRANT gives privileges to a user.

For example, give the student user permission to read data from the college database:

GRANT SELECT
ON college.*
TO 'student'@'localhost';

Now the user can run:

SELECT * FROM students;

but cannot normally modify the data.


4. Grant Multiple Permissions

Give SELECT, INSERT, UPDATE, and DELETE:

GRANT SELECT, INSERT, UPDATE, DELETE
ON college.*
TO 'student'@'localhost';

This gives the user CRUD access to tables in the college database.


5. Grant All Privileges

For a development user:

GRANT ALL PRIVILEGES
ON college.*
TO 'student'@'localhost';

This gives the user all privileges on the college database.

Avoid giving ALL PRIVILEGES unnecessarily in production. Give users only the permissions they actually need.


6. Check User Privileges

Use:

SHOW GRANTS FOR 'student'@'localhost';

You might see something like:

GRANT SELECT, INSERT, UPDATE, DELETE
ON `college`.* TO `student`@`localhost`

7. REVOKE

REVOKE removes privileges.

Suppose we previously granted:

GRANT SELECT, INSERT, UPDATE, DELETE
ON college.*
TO 'student'@'localhost';

Remove DELETE:

REVOKE DELETE
ON college.*
FROM 'student'@'localhost';

Check:

SHOW GRANTS FOR 'student'@'localhost';

8. Remove All Privileges

You can revoke all privileges on the database:

REVOKE ALL PRIVILEGES
ON college.*
FROM 'student'@'localhost';

Then:

SHOW GRANTS FOR 'student'@'localhost';

9. Test the User

Exit MySQL:

EXIT;

From Bash, login as the new user:

mysql -u student -p

Enter:

Student@123

Then:

USE college;

Try:

SELECT * FROM students;

If the user has SELECT permission, it works.

Try:

INSERT INTO students (name, age, course)
VALUES ('Test', 22, 'Java');

If INSERT wasn’t granted, MySQL will reject it with a permission error.


10. Database-Level vs Table-Level Permissions

You can give access to the entire database:

GRANT SELECT
ON college.*
TO 'student'@'localhost';

Or only one table:

GRANT SELECT
ON college.students
TO 'student'@'localhost';

This is useful when a user should access only specific tables.

For example:

GRANT SELECT, INSERT
ON college.students
TO 'student'@'localhost';

The user can insert and read from students, but doesn’t automatically get access to every table in college.


11. Column-Level Permissions

MySQL also allows privileges on specific columns.

For example:

GRANT SELECT (name, course)
ON college.students
TO 'student'@'localhost';

Now the user can query:

SELECT name, course
FROM college.students;

but doesn’t have general SELECT access to every column.


12. Create a Read-Only User

This is a very common real-world setup.

CREATE USER 'report_user'@'localhost'
IDENTIFIED BY 'Report@123';

GRANT SELECT
ON college.*
TO 'report_user'@'localhost';

Check:

SHOW GRANTS FOR 'report_user'@'localhost';

The user can read:

SELECT * FROM students;

but shouldn’t be able to:

INSERT ...
UPDATE ...
DELETE ...

13. Create an Application User

For a Java application, you might create a dedicated user:

CREATE USER 'college_app'@'localhost'
IDENTIFIED BY 'CollegeApp@123';

Then:

GRANT SELECT, INSERT, UPDATE, DELETE
ON college.*
TO 'college_app'@'localhost';

Your Java application can then connect using:

username = college_app
password = CollegeApp@123
database = college

This is better than having your Java application connect as root.


14. DCL Cheat Sheet

CommandPurpose
CREATE USERCreate database user
GRANTGive permissions
REVOKERemove permissions
SHOW GRANTSView permissions

Common privileges:

SELECT
INSERT
UPDATE
DELETE
CREATE
ALTER
DROP
INDEX
EXECUTE
ALL PRIVILEGES

Example:

CREATE USER 'student'@'localhost'
IDENTIFIED BY 'Student@123';

GRANT SELECT, INSERT, UPDATE, DELETE
ON college.*
TO 'student'@'localhost';

SHOW GRANTS FOR 'student'@'localhost';

REVOKE DELETE
ON college.*
FROM 'student'@'localhost';

The complete SQL learning picture

SQL
│
├── DDL → Database/Table structure
│   ├── CREATE
│   ├── ALTER
│   ├── TRUNCATE
│   └── DROP
│
├── DML → Data modification
│   ├── INSERT
│   ├── UPDATE
│   └── DELETE
│
├── DQL → Data retrieval
│   └── SELECT
│
├── DCL → Access control
│   ├── GRANT
│   └── REVOKE
│
└── TCL → Transactions
    ├── COMMIT
    ├── ROLLBACK
    └── SAVEPOINT

The next important topic after DDL + DML + DCL is TCL (COMMIT, ROLLBACK, SAVEPOINT), especially because you’ll need it when working with Java/JDBC and Maven.

Leave a Reply

Your email address will not be published. Required fields are marked *