MySQL Installation and Usage Guide
Windows Installation and Usage
Step 1 — Download and Install MySQL
1. Download the MySQL Installer: Visit the MySQL website and download the MySQL
installer.
2. Run the Installer:
○ Select Custom installation if you need to choose specific components.
○ Ensure MySQL Server, MySQL Workbench, and MySQL Shell are
selected.
○ Set up MySQL Server as a Windows Service to start automatically.
3. Configure MySQL Server:
○ Set a root password during setup.
○ Choose Use Legacy Authentication Method for compatibility with older
apps.
Step 2 — Secure Installation
1. Test MySQL Connection:
○ Open the MySQL Command Line Client and enter the root password when
prompted to confirm the installation.
2. Create a New MySQL User and Grant Privileges:
○ Open MySQL Shell from the Start Menu.
Connect to MySQL as the root user:
\connect root@localhost
When prompted, enter your root password.
Switch to SQL Mode in MySQL Shell:
\sql
Create a New User and Grant Privileges:
CREATE USER 'your_username'@'localhost' IDENTIFIED BY
'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
Step 3 — Create a Simple Database and Table
Create a Database:
CREATE DATABASE testdb;
USE testdb;
Create a Table:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10, 2)
);
Insert Sample Data:
INSERT INTO employees (name, position, salary) VALUES
('Alice', 'Developer', 80000),
('Bob', 'Manager', 90000),
('Charlie', 'Analyst', 70000);
Query the Data:
SELECT * FROM employees;
Step 4 — Exit MySQL Shell
exit
Linux Installation and Usage
Step 1 — Install MySQL
Update the Package Index:
sudo apt update
Install MySQL Server:
sudo apt install mysql-server
Start MySQL Service:
sudo systemctl start mysql.service
Step 2 — Secure MySQL Installation
Configure MySQL Root Authentication:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH
mysql_native_password BY 'your_password';
exit
Run Security Script:
sudo mysql_secure_installation
Follow the prompts to set up the password policy, root password, and disable unnecessary
features.
Step 3 — Create a New MySQL User and Grant Privileges
Log in to MySQL Shell:
sudo mysql
Create a User and Grant Privileges:
CREATE USER 'your_username'@'localhost' IDENTIFIED
BY'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit
Step 4 — Create a Simple Database and Table
Log in as the New User:
mysql -u your_username -p
Create a Database and Use It:
CREATE DATABASE testdb;
USE testdb;
Create a Table:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10, 2)
);
Insert Sample Data:
INSERT INTO employees (name, position, salary) VALUES
('Alice', 'Developer', 80000),
('Bob', 'Manager', 90000),
('Charlie', 'Analyst', 70000);
Query the Data:
SELECT * FROM employees;
Exit MySQL:
exit