How to Use mysqladmin: Basic Commands and Examples
Use
mysqladmin to perform administrative tasks on a MySQL server from the command line, such as checking server status, creating or dropping databases, and shutting down the server. Run commands like mysqladmin -u root -p status to check server status or mysqladmin -u root -p create dbname to create a database.Syntax
The basic syntax of mysqladmin is:
mysqladmin [options] command [command-arg]
Where:
- options: Specify user credentials and connection details, e.g.,
-u usernameand-pto prompt for password. - command: The administrative action to perform, like
status,create,drop, orshutdown. - command-arg: Additional argument for the command, such as the database name when creating or dropping.
bash
mysqladmin -u root -p status mysqladmin -u root -p create mydatabase mysqladmin -u root -p drop mydatabase mysqladmin -u root -p shutdown
Example
This example shows how to check the MySQL server status and create a new database named testdb using mysqladmin.
bash
mysqladmin -u root -p status mysqladmin -u root -p create testdb
Output
Uptime: 3600 Threads: 2 Questions: 100 Slow queries: 0 Opens: 20 Flush tables: 1 Open tables: 15 Queries per second avg: 0.027
Common Pitfalls
Common mistakes when using mysqladmin include:
- Not specifying the correct user with
-uor forgetting-pto prompt for a password, causing authentication errors. - Trying to create a database that already exists, which will cause an error.
- Using
dropwithout caution, as it permanently deletes the database. - Running commands without sufficient privileges.
Always double-check the command and user permissions before running administrative commands.
bash
mysqladmin -u root -p create testdb # Error if testdb exists mysqladmin -u root -p drop testdb # Use with caution: this deletes the database permanently
Quick Reference
| Command | Description | Example |
|---|---|---|
| status | Show server status | mysqladmin -u root -p status |
| create dbname | Create a new database | mysqladmin -u root -p create mydb |
| drop dbname | Delete a database | mysqladmin -u root -p drop mydb |
| shutdown | Stop the MySQL server | mysqladmin -u root -p shutdown |
| processlist | Show active MySQL processes | mysqladmin -u root -p processlist |
Key Takeaways
Use mysqladmin with -u and -p options to specify user and prompt for password.
Common commands include status, create, drop, and shutdown for managing MySQL server.
Be careful with drop command as it permanently deletes databases.
Check user privileges before running administrative commands to avoid permission errors.
mysqladmin is a quick way to perform server tasks without logging into the MySQL shell.