Administering MySQL from the Command Line


A database is a structured set of data, and the most common type you'll encounter in web hosting environments is a relational database. In this module, we’ll cover practical administration of MySQL/MariaDB using the command line on cPanel-powered servers—everything from structure to recovery and tuning.
🔄 Understanding Relational Databases
A relational database organizes data into tables, where relationships are established via fields. Think of a blog platform:
[users] ←── [posts] ←── [comments]
↑
└────── [sessions]
Each record in posts
and sessions
maps back to a single user. This normalized structure makes data storage efficient and powerful.
📌 Tip: MySQL 8 and MariaDB 10.3+ offer native support for JSON columns, blurring lines between SQL and NoSQL.
⚙️ MySQL Storage Engines
MySQL/MariaDB supports multiple storage engines, but on cPanel systems, you’ll mostly work with:
MyISAM – Fast reads, no transactions, table-level locking
InnoDB – ACID compliant, row-level locking, transactions supported (recommended default)
🧰 cPanel-Integrated MySQL Utilities
Utility | Purpose |
dbindex | Builds dbindex.db from /etc/dbowners ; runs via cron every 2 hours |
dbmaptool | CLI version of WHM’s DB Map Tool; modifies JSON but doesn’t run SQL |
dbstoregrants | Generates grants_cpuser.yaml during DB user operations |
mysqluserstore | Updates users.db with host/user data; auto-runs via cron |
restoregrants | Reads grants_cpuser.yaml to apply DB grants |
setupdbmap | Triggered during upcp ; sets initial DB mapping |
🧪 Real-Time Monitoring with mytop
Run mytop
to view live query activity and performance, just like top
for Linux processes.
mytop -u root -p yourpassword
⚠️ Dealing With Common Errors
🔒 ACCESS DENIED
Check /etc/my.cnf
or your user’s ~/.my.cnf
for authentication/permission issues.
❌ mysql: unknown variable '...'
This means you've specified a variable not recognized by your current MySQL/MariaDB version. Clean up deprecated or unsupported entries in my.cnf
.
🧱 Backup and Restore Techniques
🔄 Full Server Backup (Compressed)
mysqldump --opt -AER | gzip > /root/alldatabases.sql.gz
-A
: All databases-E
: Include events-R
: Include routinesgzip
: Compress to save space
🔁 Directory-Level Restore
Stop MySQL/MariaDB and monitoring
Rename existing data dir:
mv /var/lib/mysql /var/lib/mysql.bak
- Restore backup:
cp /backup/date/system/dirs/_var_lib_mysql_.tar.gz /
cd /
tar -xzf _var_lib_mysql_.tar.gz
🐌 MySQL Slow Query Log – Performance Goldmine
The slow query log identifies underperforming queries.
Enable it in /etc/my.cnf
:
slow_query_log = 1
log-slow-queries = /var/log/mysql-slow.log
long_query_time = 2
Why It Matters
Find unoptimized queries
Improve indexing
Reduce load time
🧠 Pro Insights
Monitor
$(hostname).err
files for startup issues:less /var/lib/mysql/$(hostname).err
Use
mysqladmin
andmysqlshow
for quick server checksAlways validate restored directories with
mysql_upgrade
✅ Key Takeaways
Use InnoDB for most modern applications
Monitor your MySQL instance using tools like
mytop
and error logsOptimize using the slow query log
Automate backups, but always verify your restore strategy
✳️ If you found this helpful, follow me on LinkedIn for more technical insights and MySQL deep dives.
Subscribe to my newsletter
Read articles from Mihir Savla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
