温馨提示×

ubuntu lamp数据库管理指南

小樊
54
2025-10-07 18:12:50
栏目: 智能运维

Ubuntu LAMP Database Management Guide

The LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) is a foundational setup for hosting dynamic web applications. This guide focuses on managing databases (MySQL/MariaDB) in an Ubuntu-based LAMP environment, covering installation, configuration, operations, security, and optimization.

1. Installing the LAMP Stack

Before managing databases, ensure the LAMP components are installed:

  • Update System: Run sudo apt update && sudo apt upgrade -y to sync package lists and fix bugs.
  • Install Apache: Execute sudo apt install apache2 and verify with http://your_server_ip (should show the Apache default page).
  • Install MySQL/MariaDB: For MySQL, use sudo apt install mysql-server; for MariaDB (a MySQL-compatible alternative), use sudo apt install mariadb-server. During installation, set a strong root password.
  • Install PHP: Run sudo apt install php libapache2-mod-php php-mysql to enable PHP support for Apache. Restart Apache with sudo systemctl restart apache2 to apply changes.

For PHPMyAdmin (a web-based database tool), install it via sudo apt install phpmyadmin, selecting Apache during setup and following the prompts.

2. Database Management Basics

Accessing the Database

Use the mysql command-line tool (for MySQL) or mariadb (for MariaDB) to connect to the server:

sudo mysql -u root -p  # Enter root password
# For MariaDB:
sudo mariadb -u root -p

To connect as a specific user to a database:

mysql -u username -p -D database_name

Exit the prompt with \q.

Common SQL Commands

  • Create a Database:
    CREATE DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    
  • Use a Database:
    USE database_name;
    
  • Create a Table:
    CREATE TABLE users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) NOT NULL,
        email VARCHAR(100) NOT NULL UNIQUE,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    
  • Insert Data:
    INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com');
    
  • Query Data:
    SELECT * FROM users;  -- All columns
    SELECT username, email FROM users WHERE id = 1;  -- Specific columns with condition
    
  • Update Data:
    UPDATE users SET email = 'john.doe@example.com' WHERE id = 1;
    
  • Delete Data:
    DELETE FROM users WHERE id = 1;
    
  • Drop a Table/Database:
    DROP TABLE users;  -- Deletes the table and its data
    DROP DATABASE database_name;  -- Deletes the entire database
    ```.
    
    
    

3. User and Permission Management

Creating Users

Create a user with a strong password:

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'SecurePassword123!';

For remote access, replace 'localhost' with '%' (allows connections from any IP, but restrict in production).

Granting Permissions

Grant specific privileges to a user on a database:

GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'app_user'@'localhost';

Common privileges include SELECT (read), INSERT (add), UPDATE (modify), DELETE (remove), and ALL PRIVILEGES (full access).
Flush privileges to apply changes immediately:

FLUSH PRIVILEGES;
```.  

### Revoking Permissions  
Revoke privileges using:  
```sql
REVOKE INSERT, UPDATE ON database_name.* FROM 'app_user'@'localhost';
FLUSH PRIVILEGES;
```.


## 4. Security Best Practices  
### Initial Hardening  
Run the `mysql_secure_installation` script (for MySQL) or `mariadb-secure-installation` (for MariaDB) after installation. It will:  
- Set the root password (if not already set).  
- Remove anonymous users.  
- Disallow root login from remote hosts.  
- Delete the test database.  
- Reload privilege tables.  

### Remote Access Configuration  
To allow remote connections, edit the MariaDB/MySQL configuration file:  
```bash
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Find the bind-address line and change it from 127.0.0.1 (local only) to 0.0.0.0 (all interfaces). Save and restart the service:

sudo systemctl restart mariadb

Then, grant remote access to a user:

GRANT ALL PRIVILEGES ON database_name.* TO 'app_user'@'%' IDENTIFIED BY 'SecurePassword123!';
FLUSH PRIVILEGES;

Restrict access by configuring your firewall (e.g., UFW) to allow only specific IPs:

sudo ufw allow from trusted_ip to any port 3306
```.  

### Encryption  
Enable SSL/TLS for encrypted connections. Generate certificates (or use Let’s Encrypt) and configure the database server to require SSL for remote connections. Update user permissions to enforce SSL:  
```sql
GRANT ALL PRIVILEGES ON database_name.* TO 'app_user'@'%' IDENTIFIED BY 'SecurePassword123!' REQUIRE SSL;
```.


## 5. Backup and Recovery  
### Database Backups  
Use `mysqldump` to export databases to SQL files:  
```bash
mysqldump -u root -p database_name > database_name_backup.sql

For multiple databases:

mysqldump -u root -p --databases db1 db2 > backup.sql

Include the --single-transaction flag for InnoDB tables to avoid locking.

Restoring Databases

Restore from a backup file:

mysql -u root -p database_name < database_name_backup.sql

For a full server restore, restore the mysql system database first, then user databases.

6. Performance Optimization

Query Optimization

Use EXPLAIN to analyze query performance:

EXPLAIN SELECT * FROM users WHERE email = 'john@example.com';

Optimize queries by adding indexes to frequently queried columns:

CREATE INDEX idx_email ON users(email);

Avoid SELECT *—fetch only necessary columns to reduce data transfer.

Server Configuration

Tune the MariaDB/MySQL configuration file (/etc/mysql/mariadb.conf.d/50-server.cnf) for better performance:

  • InnoDB Buffer Pool: Set to 50-70% of available RAM (e.g., innodb_buffer_pool_size = 2G).
  • Query Cache: Enable if your workload benefits from it (e.g., query_cache_size = 64M).
  • Max Connections: Adjust based on expected traffic (e.g., max_connections = 200).

Restart the service after changes:

sudo systemctl restart mariadb
```.  

### Regular Maintenance  
Optimize tables to defragment and reclaim space:  
```sql
OPTIMIZE TABLE users;

Automate maintenance with mysqlcheck:

sudo mysqlcheck --all-databases --optimize --auto-repair
```.


## 7. Monitoring and Logging  
### Logs  
Check error logs for troubleshooting:  
```bash
sudo tail -f /var/log/mysql/error.log

Enable slow query logging to identify performance bottlenecks:
Edit /etc/mysql/mariadb.conf.d/50-server.cnf and add:

slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow-query.log
long_query_time = 2  # Log queries taking longer than 2 seconds

Restart the service to apply changes.

Tools

Use tools like htop (monitor CPU/memory), iotop (monitor disk I/O), or vnStat (monitor network usage) to track system performance. For database-specific monitoring, consider tools like Prometheus + Grafana or Percona Monitoring and Management (PMM).

This guide provides a comprehensive foundation for managing databases in an Ubuntu LAMP environment. Adapt the steps to your specific use case (e.g., production vs. development) and always prioritize security (e.g., strong passwords, SSL, least privilege).

0