← Back to Chapters

Installing MySQL with XAMPP

? Installing MySQL with XAMPP

MySQL XAMPP Localhost Setup

⚡ Quick Overview

In this topic, you will learn how to install and start MySQL using XAMPP. XAMPP is an easy-to-install package that bundles Apache (web server), MySQL/MariaDB (database), PHP, and other tools. After completing this page, you will be able to:

  • Install XAMPP on your system.
  • Start MySQL server using the XAMPP Control Panel.
  • Access MySQL via phpMyAdmin and the MySQL command line.
  • Create a simple PHP script to test the MySQL connection.

? Key Concepts

  • XAMPP: A local server stack that includes Apache, MySQL, PHP, and more.
  • MySQL Server: The database engine used to store and manage data.
  • phpMyAdmin: A web-based GUI tool to manage MySQL databases.
  • localhost: Your own computer acting as a server (usually http://localhost or http://localhost/xampp).
  • Port 3306: The default port used by MySQL server.

? Installation Steps (XAMPP + MySQL)

  1. Download XAMPP from the official website for your operating system (Windows / macOS / Linux).
  2. Run the installer and follow the setup wizard.
  3. During installation, keep Apache and MySQL selected (default).
  4. Finish installation and open the XAMPP Control Panel.
  5. Start the Apache and MySQL modules.
  6. Verify installation by visiting http://localhost or http://localhost/xampp in your browser.
  7. Open phpMyAdmin from the XAMPP dashboard or via http://localhost/phpmyadmin.
? View Code Example
// Steps to start MySQL from XAMPP Control Panel (summary)
1. Open XAMPP Control Panel.
2. Click the "Start" button next to Apache.
3. Click the "Start" button next to MySQL.
4. Wait until both modules are highlighted in green.
5. Open your browser and visit: http://localhost/phpmyadmin

? Accessing MySQL via phpMyAdmin

Once MySQL is running, you can access and manage your databases using phpMyAdmin:

  • Open your browser and go to http://localhost/phpmyadmin.
  • You will see a dashboard where you can create, modify, and delete databases and tables.
  • By default, the root user has no password in a fresh XAMPP installation (for local use only).
? View Code Example
// Example SQL query to create a database in phpMyAdmin
CREATE DATABASE xampp_demo_db;
USE xampp_demo_db;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
course VARCHAR(100)
);

? Accessing MySQL via Command Line

XAMPP also allows you to use the MySQL command line client. On Windows, you can use the "Shell" button in XAMPP Control Panel to open a terminal window.

? View Code Example
// Log in to MySQL server from XAMPP shell using root user
mysql -u root

// After logging in, you can run SQL commands
SHOW DATABASES;
CREATE DATABASE cli_demo;
USE cli_demo;
SHOW TABLES;

? Testing MySQL with a PHP Script (htdocs)

To verify that PHP can connect to MySQL inside XAMPP, create a simple PHP file in the htdocs folder (for example: C:\xampp\htdocs\mysql_test.php on Windows).

? View Code Example
// mysql_test.php - Simple script to test MySQL connection via XAMPP
<?php
$host = "localhost";          // MySQL server is running on localhost
$user = "root";               // Default XAMPP user
$pass = "";                   // Default XAMPP password is empty
$db   = "test";               // Existing database name (e.g. 'test')

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "✅ Connected successfully to MySQL using XAMPP!";
$conn->close();
?>

? Expected Output (Browser)

If everything is configured correctly, opening http://localhost/mysql_test.php in your browser should display:

✅ Connected successfully to MySQL using XAMPP!

If you see an error message instead, carefully read the error; it usually tells you whether the problem is with the database name, username, password, or connection settings.

? Tips & Best Practices

  • Use XAMPP only for local development, not for production servers.
  • Keep the default root user password empty only on your personal machine; set a password if others can access your system.
  • Always stop Apache and MySQL from the XAMPP Control Panel before shutting down your PC.
  • If MySQL fails to start, check if another service (like MySQL from another software) is already using port 3306.
  • Organize your PHP projects in separate folders inside htdocs (e.g. C:\xampp\htdocs\mysite).

? Try It Yourself / Practice Tasks

  1. Install XAMPP on your machine and start the Apache and MySQL services from the XAMPP Control Panel.
  2. Open http://localhost/phpmyadmin and create a new database named practice_db.
  3. Inside practice_db, create a table users with fields id, name, and email.
  4. Create a new PHP file db_connect.php inside htdocs that connects to practice_db and prints a success message.
  5. Modify your PHP script to insert one sample record into the users table and verify it inside phpMyAdmin.