<< back to Guides

๐Ÿ›ข๏ธ MySQL Essentials: Client, Configuration, Users, and SQL

This guide covers all essential aspects of working with MySQL in a server environment โ€” including the MySQL CLI client, configuration settings, user and privilege management, and SQL best practices.


๐Ÿ“Ÿ 1. Using the MySQL Client

๐Ÿ”น Connecting to a MySQL Server

mysql -u root -p
mysql -h 127.0.0.1 -P 3306 -u myuser -p

Use --protocol=tcp to avoid socket-based connections if needed.

๐Ÿ”น Basic Client Commands

SHOW DATABASES;
USE mydb;
SHOW TABLES;
DESCRIBE users;
EXIT;

๐Ÿ”น Running SQL Scripts

mysql -u root -p mydb < ./setup.sql

โš™๏ธ 2. Configuration and Server Settings

๐Ÿ”น Key Configuration File Locations

Distro Default Config File
Debian /etc/mysql/my.cnf
Alpine /etc/my.cnf
Docker Custom volume or ENV configs

๐Ÿ”น Important Configuration Directives

[mysqld]
bind-address = 0.0.0.0       # Allow external connections
port = 3306
max_connections = 200
sql_mode = STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
innodb_buffer_pool_size = 1G
log_error = /var/log/mysql/error.log

Use SHOW VARIABLES; to inspect live settings.

๐Ÿ”น Service Management

# Debian/Ubuntu
sudo systemctl restart mysql
sudo systemctl status mysql

# Alpine/OpenRC
sudo rc-service mariadb start

๐Ÿ‘ค 3. Managing MySQL Users and Permissions

๐Ÿ”น Creating Users

CREATE USER 'appuser'@'%' IDENTIFIED BY 's3cret';

๐Ÿ”น Granting Permissions

GRANT SELECT, INSERT, UPDATE ON mydb.* TO 'appuser'@'%';
FLUSH PRIVILEGES;

๐Ÿ”น Checking Permissions

SHOW GRANTS FOR 'appuser'@'%';

๐Ÿ”น Deleting a User

DROP USER 'appuser'@'%';

๐Ÿง  4. MySQL SQL Features & Tips

๐Ÿ”น Data Types

๐Ÿ”น Table Creation

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100),
  email VARCHAR(100) UNIQUE,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

๐Ÿ”น Indexing for Performance

CREATE INDEX idx_email ON users(email);
SHOW INDEX FROM users;

๐Ÿ”น Query Basics

SELECT * FROM users WHERE email = 'john@example.com';
INSERT INTO users (name, email) VALUES ('Jane', 'jane@example.com');
UPDATE users SET name = 'Janet' WHERE id = 1;
DELETE FROM users WHERE id = 1;

๐Ÿ”น Transactions

START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

Use ROLLBACK; if something goes wrong.


๐Ÿงช 5. Useful Utilities & Maintenance

๐Ÿ”น Backup and Restore

# Dump
mysqldump -u root -p mydb > backup.sql

# Restore
mysql -u root -p mydb < backup.sql
</codexample>

### ๐Ÿ”น Check Slow Queries

Edit config:

```ini
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
# Analyze slow queries
mysqldumpslow /var/log/mysql/slow.log

๐Ÿ”น Monitoring


๐Ÿ›ก๏ธ 6. Security Tips


๐Ÿ“š Resources


โœ… Summary

Topic Command/Tool
Connect mysql -u root -p
Config /etc/mysql/my.cnf, SHOW VARIABLES;
Users CREATE USER, GRANT, SHOW GRANTS
SQL SELECT, JOIN, INDEX, TRANSACTION
Admin mysqldump, slow_query_log, systemctl
<< back to Guides