MySQL is the world’s most popular open-source relational database, used by WordPress, many web apps and countless companies. This tutorial gets you from installation to creating a database and running your first queries, and links to our step-by-step Python MySQL series.
What You Will Learn
- Install MySQL and connect
- Create a database, user and tables
- MySQL data types
- Common MySQL-specific features
- Connect from Python
Prerequisites
Install
- Windows/macOS: download MySQL Community Server and MySQL Workbench from the official site, or install XAMPP.
- Ubuntu:
sudo apt install mysql-server. - Docker:
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret mysql:8.
mysql -u root -p
Create a Database, User and Table
CREATE DATABASE institute;
CREATE USER 'student_app'@'localhost' IDENTIFIED BY 'StrongPass#2026';
GRANT ALL PRIVILEGES ON institute.* TO 'student_app'@'localhost';
USE institute;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
city VARCHAR(50),
joined_on DATE DEFAULT (CURRENT_DATE)
);
INSERT INTO students (name, city) VALUES ('Aman', 'Ludhiana'), ('Riya', 'Jalandhar');
SELECT * FROM students;
Explanation: AUTO_INCREMENT fills the id automatically. Give each app its own user with only the privileges it needs instead of using root.
+----+------+-----------+------------+ | id | name | city | joined_on | +----+------+-----------+------------+ | 1 | Aman | Ludhiana | (today) | | 2 | Riya | Jalandhar | (today) | +----+------+-----------+------------+
Common Data Types
| Type | Use |
|---|---|
| INT, BIGINT | Whole numbers, ids |
| DECIMAL(10,2) | Money |
| VARCHAR(n) | Short text |
| TEXT | Long text |
| DATE, DATETIME, TIMESTAMP | Dates and times |
| BOOLEAN | True/false (stored as TINYINT) |
| JSON | JSON documents |
Useful MySQL Commands
SHOW DATABASES;
SHOW TABLES;
DESCRIBE students;
SELECT DATE_FORMAT(joined_on, '%d-%m-%Y') FROM students;
SELECT * FROM students LIMIT 10 OFFSET 20; -- page 3 of 10 rows
mysqldump -u root -p institute > backup.sql -- (terminal) backup
Connect from Python
# pip install mysql-connector-python
import mysql.connector
con = mysql.connector.connect(host="localhost", user="student_app",
password="StrongPass#2026", database="institute")
cur = con.cursor()
cur.execute("SELECT name FROM students WHERE city = %s", ("Ludhiana",))
print(cur.fetchall())
con.close()
Explanation: Always pass values with %s placeholders, never by joining strings – this prevents SQL injection. Full details are in Python MySQL Database.
[('Aman',)]
Common Errors
| Problem | Cause and fix |
|---|---|
Access denied for user |
Wrong password or the user lacks privileges on that database. |
Can't connect to MySQL server |
The server is not running, or the port (3306) is blocked. |
| Hindi/emoji text shows ??? | Use CHARACTER SET utf8mb4 for the database and tables. |
Series
SQL series: SQL Basics · MySQL · PostgreSQL · MongoDB · Database Design · SQL for Data Analytics · Advanced SQL Queries · Python MySQL series
Interview Questions
Q: What is AUTO_INCREMENT?
A column option that automatically gives each new row the next number.
Q: MySQL vs PostgreSQL?
Both are excellent; MySQL is very common in web hosting, PostgreSQL has richer SQL features and extensions such as pgvector.
Practice Exercises
- Create a courses table with DECIMAL fees and insert 3 rows.
- Write a query that shows students who joined in the current month.
Summary
MySQL is a popular, free relational database. Create a database and a limited user, choose proper data types, and connect from Python with parameterised queries.
Next: PostgreSQL · Python MySQL series
Machine Learning Tutorials, Courses and Certifications