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

ItemCommon Local Default Values
Hostlocalhost or 127.0.0.1
Port3306
Userroot
PasswordEmpty (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

ToolAccess URL
phpMyAdminhttp://localhost/phpmyadmin
Adminerhttp://localhost/adminer
Command Linemysql -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.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run