Using MySQL on localhost
Install and connect to MySQL in a local environment, managing databases with phpMyAdmin or the command line for applications like WordPress and Drupal.
MySQL is the most commonly used relational database in the PHP ecosystem. During local development, web applications connect to the local MySQL instance via localhost or 127.0.0.1.
Default Connection Information
| Item | Common Local Default Values |
|---|---|
| Host | localhost or 127.0.0.1 |
| Port | 3306 |
| User | root |
| Password | Empty (default for XAMPP/WAMP) or set during installation |
Example application configuration (WordPress wp-config.php):
define('DB_HOST', 'localhost');
define('DB_NAME', 'myapp');
define('DB_USER', 'root');
define('DB_PASSWORD', '');Starting MySQL
- XAMPP / WAMP / MAMP: Start the MySQL service from the control panel
- Linux LAMP:
sudo systemctl start mysql - macOS Homebrew:
brew services start mysql
Management Tools
| Tool | Access URL |
|---|---|
| phpMyAdmin | http://localhost/phpmyadmin |
| Adminer | http://localhost/adminer |
| Command Line | mysql -u root -p |
Common Command Line Operations
mysql -u root -p
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'dev'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON myapp.* TO 'dev'@'localhost';
FLUSH PRIVILEGES;Frequently Asked Questions
Unable to connect to MySQL
Ensure the service is running; check if port 3306 is in use; on Windows, try using 127.0.0.1 instead of localhost.
Access denied for user ‘root’@‘localhost’
Reset the root password or verify the default credentials in the installation documentation.
Relationship with MariaDB
Stacks like XAMPP commonly use MariaDB as a replacement for MySQL, with connection methods and SQL syntax being largely compatible, so applications typically do not require modification.
Conclusion
Local MySQL listens on 3306 by default, with the hostname set to localhost; use phpMyAdmin for visual database management in the browser.