Chun Lin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # redis & cashing ## 🔧 1. What Is **Redis**? **Redis** stands for **REmote DIctionary Server**. It is an **in-memory data store** often used as: * A **cache** * A **database** * A **message broker** --- ## ✅ Key Characteristics of Redis: * **In-memory**: Stores data in RAM, making it extremely fast. * **Key-Value store**: Data is stored as key-value pairs (e.g., `"user:123" -> "John"`). * **Supports data structures**: Strings, hashes, lists, sets, sorted sets, bitmaps, streams, etc. * **Persistence options**: Can write to disk periodically (RDB) or in real-time (AOF). * **Pub/Sub & Streams**: Used for messaging and real-time event processing. --- ## 💡 2. What Is **Caching**? **Caching** is the practice of **storing frequently accessed data** in a **temporary storage (cache)** so that future requests for that data can be served **faster**. ### 🔁 Typical Use Case: Instead of querying a **slow database** or API repeatedly, you **store the result** of the first query in memory: ```plaintext 1st request: Query DB -> Store in cache 2nd request: Check cache -> Return cached data (much faster) ``` --- ## 🔗 3. How Redis Is Used for **Caching** Redis is ideal for caching because of its: * **Speed**: Memory access is much faster than disk or database. * **TTL support**: You can set keys to automatically expire (`EXPIRE key 60`). * **Atomic operations**: Supports atomic reads/writes, preventing race conditions. * **Eviction policies**: Redis can auto-remove old keys when memory is full. ### 🧠 Common Redis Caching Strategies: | Strategy | Description | | ----------------------- | ------------------------------------------------------------------- | | **Read-through cache** | App checks cache first → if miss → loads from DB → stores in cache. | | **Write-through cache** | App writes to both DB and cache at the same time. | | **Cache-aside** | App manually manages what gets stored in cache. | | **TTL-based caching** | Cached data auto-expires after a set time. | --- ### ⚙️ 4. Real-World Example: Redis as Cache #### Scenario: Caching User Profile Data ```python # Pseudocode def get_user_profile(user_id): cache_key = f"user:{user_id}" # Check cache first if redis.exists(cache_key): return redis.get(cache_key) # If not in cache, get from DB profile = db.query("SELECT * FROM users WHERE id = ?", user_id) # Store in cache for next time redis.setex(cache_key, 3600, profile) # 1 hour TTL return profile ``` This saves your database from repeated queries and **improves response time**. --- ### ⚠️ 5. When Not to Use Redis for Caching Redis is powerful, but not always the right choice: * ❌ **Large datasets** that don’t fit in memory * ❌ **Critical persistence requirements** (though Redis has persistence modes, it’s RAM-first) * ❌ **Complex relational queries** (Redis is NoSQL, not SQL) --- ## 🧾 redis Summary | Concept | Redis | Caching | | ----------- | -------------------------------------------- | -------------------------------------- | | What is it? | In-memory key-value datastore | Strategy to store frequently used data | | Used for | DB, cache, queue, pub/sub | Speeding up data access | | Key trait | Fast, simple, versatile | Reduces latency and DB load | | Relation | Redis is commonly used **as** a cache engine | | --- # 🔍 Redis Testing Checklist: ## ✅ Container Level Tests: ```bash! # 1. Container running docker-compose ps | grep redis # Should show: redis ... Up ... 6379/tcp # 2. Redis service responding docker-compose exec redis redis-cli ping # Should return: PONG # 3. Redis server info docker-compose exec redis redis-cli info server # Should show Redis version and server info # 4. Redis process running docker-compose exec redis ps aux | grep redis # Should show redis-server process # 5. Redis configuration docker-compose exec redis redis-cli config get "*" # Should show Redis configuration parameters ``` ## ✅ Network Level Tests: ```bash! # 6. Test Redis from other containers docker-compose exec wordpress redis-cli -h redis ping # Should return: PONG # 7. Test Redis from nginx docker-compose exec nginx redis-cli -h redis ping # Should return: PONG (if redis-cli installed) # 8. Test Redis connectivity docker-compose exec redis netstat -tlnp | grep 6379 # Should show Redis listening on port 6379 # 9. Test Redis internal network docker-compose exec redis redis-cli -h localhost ping # Should return: PONG ``` ## ✅ Functional Tests: ```bash! # 10. Test Redis SET/GET operations docker-compose exec redis redis-cli set test_key "test_value" # Should return: OK docker-compose exec redis redis-cli get test_key # Should return: "test_value" # 11. Test Redis data types docker-compose exec redis redis-cli lpush test_list "item1" "item2" # Should return: (integer) 2 docker-compose exec redis redis-cli lrange test_list 0 -1 # Should return: 1) "item2" 2) "item1" # 12. Test Redis hash operations docker-compose exec redis redis-cli hset test_hash field1 "value1" # Should return: (integer) 1 docker-compose exec redis redis-cli hget test_hash field1 # Should return: "value1" ``` ## 🎯 Performance Tests: ```bash! # 13. Redis benchmark docker-compose exec redis redis-benchmark -q -n 1000 -c 10 # Should show performance metrics # 14. Test Redis memory usage docker-compose exec redis redis-cli info memory # Should show memory usage statistics # 15. Test Redis statistics docker-compose exec redis redis-cli info stats # Should show connection and command statistics # 16. Test Redis keyspace docker-compose exec redis redis-cli info keyspace # Should show database and key statistics ``` ## 🔐 Security Tests: ```bash! # 17. Test Redis authentication (if configured) docker-compose exec redis redis-cli auth your_password # Should return: OK (if password is set) # 18. Test Redis ACL (if configured) docker-compose exec redis redis-cli acl list # Should show ACL rules # 19. Check Redis bind configuration docker-compose exec redis redis-cli config get bind # Should show bind configuration # 20. Test Redis protected mode docker-compose exec redis redis-cli config get protected-mode # Should show protected mode status ``` ## 🚀 WordPress Integration Tests: ```bash! # 21. Test WordPress Redis cache (if configured) docker-compose exec wordpress wp cache flush --allow-root # Should flush WordPress cache # 22. Test Redis with WordPress docker-compose exec redis redis-cli keys "*wordpress*" # Should show WordPress-related keys (if caching is enabled) # 23. Test session storage docker-compose exec redis redis-cli keys "*session*" # Should show session keys (if Redis is used for sessions) # 24. Monitor Redis commands docker-compose exec redis redis-cli monitor # Should show real-time Redis commands (Ctrl+C to stop) ``` ## 📊 Advanced Tests: ```bash! # 25. Test Redis pub/sub # Terminal 1: docker-compose exec redis redis-cli subscribe test_channel # Terminal 2: docker-compose exec redis redis-cli publish test_channel "Hello Redis" # Should show message in Terminal 1 # 26. Test Redis persistence docker-compose exec redis redis-cli bgsave # Should return: Background saving started # 27. Test Redis replication (if configured) docker-compose exec redis redis-cli info replication # Should show replication status # 28. Test Redis cluster (if configured) docker-compose exec redis redis-cli cluster nodes # Should show cluster information ``` ## 🔧 Debugging Tests: ```bash! # 29. Redis logs docker-compose logs redis # Should show Redis startup and operation logs # 30. Redis configuration dump docker-compose exec redis redis-cli config get "*" > redis_config.txt # Should create configuration file # 31. Test Redis slowlog docker-compose exec redis redis-cli slowlog get 10 # Should show slow queries # 32. Test Redis client list docker-compose exec redis redis-cli client list # Should show connected clients ``` --- --- # adminer ## **Architecture:** ```bash! Browser → nginx (port 443) → Adminer (port 80) → WordPress + MariaDB ``` ## Apache & php ### 🌐 Apache - The Web Server #### What is Apache? - Full name: Apache HTTP Server - Purpose: Serves web pages over HTTP/HTTPS - Role: Listens for web requests and sends back responses #### What Apache Does: 1. Listens on port 80 (HTTP) for incoming requests 2. Receives requests like GET /index.php 3. Finds the file in /var/www/html/ 4. Processes the file (if it's PHP, hands it to PHP) 5. Sends response back to the browser ### 🐘 PHP - The Programming Language #### What is PHP? - Full name: PHP: Hypertext Preprocessor - Purpose: Server-side scripting language - Role: Executes code to generate dynamic web content #### What PHP Does: 1. Executes PHP code in .php files 2. Connects to databases (like your MariaDB) 3. Generates HTML dynamically 4. Handles forms and user input ### 🤝 How Apache + PHP Work Together #### The Request Flow: ```bash! Browser → Apache → PHP → Database → PHP → Apache → Browser ``` #### Step-by-Step Example: 1. User visits: http://yilin.42.fr 2. Apache receives: Request for /index.php 3. Apache sees: This is a PHP file, not static HTML 4. Apache calls: PHP interpreter to execute the code 5. PHP runs: Adminer code (connects to database, generates interface) 6. PHP returns: Generated HTML to Apache 7. Apache sends: Final HTML to user's browser ### 🔧 In Your Adminer Container: #### Apache's Job: - Web server: Serves the Adminer interface - Port 80: Listens for web requests - File serving: Serves /var/www/html/index.php #### PHP's Job: - Runs Adminer: Executes the PHP code in index.php - Database connection: Connects to your MariaDB container - Interface generation: Creates the database management interface #### 🎯 Why need both for adminer Dockerfile? ```bash! # Install web server + scripting language apache2 # Web server (serves pages) php # PHP interpreter (runs code) php-mysql # Connects PHP to MySQL/MariaDB php-mysqli # Alternative MySQL connection method php-pdo # Database abstraction layer php-pdo-mysql # PDO driver for MySQL/MariaDB ```` #### Apache Alone: ❌ Can only serve static files (HTML, CSS, images) ❌ Cannot execute dynamic code ❌ Cannot connect to databases #### PHP Alone: ❌ Cannot listen for web requests ❌ Cannot serve over HTTP ❌ Needs a web server to handle network requests #### Apache + PHP Together: ✅ Apache handles web requests and HTTP protocol ✅ PHP executes dynamic code and database connections ✅ Perfect combination for web applications like Adminer ### 🔄 Real Example - Adminer: #### When you access Adminer: 1. Apache receives your browser request 2. Apache sees you want index.php (the Adminer file) 3. Apache passes the PHP file to PHP interpreter 4. PHP executes Adminer code: - Shows login form - Connects to MariaDB when you login - Displays database tables and data 5. PHP generates HTML and sends it back to Apache 6. Apache sends the final webpage to your browser ## launch adminer ### Open any browser in terminal ```bash chromium https://yilin.42.fr/adminer/ ``` ### Or use curl to see HTML: ``` # Get Adminer HTML curl -k https://yilin.42.fr/adminer/ # Should return Adminer login form HTML ``` ### 🎯 Expected Browser Result: When you visit ```https://yilin.42.fr/adminer/```, you should see: ``` ┌─────────────────────────────────┐ │ Adminer │ │ Database management tool │ │ │ │ System: [MySQL ▼] │ │ Server: [mariadb ] │ │ Username: [yilin ] │ │ Password: [*********** ] │ │ Database: [wordpress ] │ │ │ │ [Login] │ └─────────────────────────────────┘ ``` #### 🔐 Login Credentials: ``` System: MySQL / mariaDB Server: mariadb Username: yilin Password: XXXXX Database: wordpress ``` --- ## 🌐 Complete Adminer Testing Checklist: ```bash! # 1. Container running docker-compose ps | grep adminer # Should show: adminer ... Up ... 80/tcp # 2. Apache responding docker-compose exec adminer curl -I localhost:80 # Should return: HTTP/1.1 200 OK # 3. PHP working docker-compose exec adminer php -v # Should show: PHP 7.4.33 # 4. Apache processes docker-compose exec adminer ps aux | grep apache # Should show apache2 processes # 5. PHP extensions loaded docker-compose exec adminer php -m | grep -i mysql # Should show: mysql, mysqli, pdo_mysql ``` --- ## ✅ Network Level Tests: ```bash! # 6. nginx proxy working curl -k https://localhost/adminer/ # Should return HTML with login form # 7. Database connectivity (if ping installed) docker-compose exec adminer ping -c 3 mariadb # Should show successful pings # 8. Test internal web server docker-compose exec adminer curl -I http://localhost:80 # Should return: HTTP/1.1 200 OK # 9. Test from nginx to adminer docker-compose exec nginx curl -I http://adminer:80 # Should return: HTTP/1.1 200 OK ``` --- ## ✅ Application Level Tests: ```bash! # 10. Login form loads curl -k https://localhost/adminer/ | grep -i login # Should return: <title>Login - Adminer</title> # 11. Test MySQL connectivity via PHP docker-compose exec adminer php -r " try { \$pdo = new PDO('mysql:host=mariadb;dbname=wordpress', 'yilin', 'happybirthday'); echo 'SUCCESS: Database connection working!\n'; } catch (Exception \$e) { echo 'ERROR: ' . \$e->getMessage() . '\n'; }" # 12. Check Adminer version curl -k https://localhost/adminer/ | grep -i version # Should show Adminer version info ``` --- ## 🌐 Browser Testing Checklist: ✅ Access and Interface: ``` 1. Visit: https://localhost/adminer/ 2. Accept SSL warning (self-signed certificate) 3. Should see clean, modern Adminer interface 4. Login form should be visible and functional ``` ✅ Login Form Testing: ``` System: [MySQL / MariaDB ▼] (should be pre-selected) Server: mariadb Username: yilin Password: happybirthday Database: wordpress [✓] Permanent login (optional) ``` ✅ Database Connection Testing: ``` 1. Click "Login" button 2. Should successfully connect to WordPress database 3. Should see database structure in left sidebar 4. Should see WordPress tables listed ``` ✅ WordPress Database Exploration: ``` Tables to check: - wp_posts (blog posts and pages) - wp_users (user accounts) - wp_options (WordPress settings) - wp_comments (comments) - wp_postmeta (post metadata) - wp_usermeta (user metadata) ``` ✅ Functionality Testing: ``` 1. Browse table data (click on table names) 2. Execute SQL queries (SQL command tab) 3. View table structure (Structure tab) 4. Check table indexes (Indexes tab) 5. Test data editing (Edit button on rows) ``` --- ## 🔍 Key Security Checks: ✅ Access Control: ```bash! # 13. Should only be accessible via nginx proxy curl -k https://localhost/adminer/ # ✅ Should work curl http://localhost:8080/adminer/ # ❌ Should fail (no direct access) # 14. Check SSL/TLS encryption curl -k -I https://localhost/adminer/ | grep -i "strict-transport" # Should show security headers ``` ✅ Database Security: ```bash! # 15. No credentials in Dockerfile grep -i password /path/to/Dockerfile # Should return nothing # 16. Test wrong credentials # Try logging in with wrong password in browser # Should show: "Invalid credentials" or similar ``` ✅ File Permissions: ```bash! # 17. Check web files ownership docker-compose exec adminer ls -la /var/www/html/ # Should show: www-data:www-data index.php # 18. Check file permissions docker-compose exec adminer stat /var/www/html/index.php # Should show: Access: (0755/-rwxr-xr-x) ``` --- ## 🎯 Advanced Testing: ✅ Performance Testing: ```bash! # 19. Test response time time curl -k https://localhost/adminer/ # Should be fast (under 1 second) # 20. Test concurrent connections for i in {1..5}; do curl -k https://localhost/adminer/ & done # Should handle multiple requests ``` ✅ SQL Query Testing: ``` -- Test queries in Adminer interface: -- 1. Check WordPress installation SELECT option_value FROM wp_options WHERE option_name = 'home'; -- 2. List all users SELECT user_login, user_email FROM wp_users; -- 3. Count posts SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'; -- 4. Check database size SELECT table_name, round(((data_length + index_length) / 1024 / 1024), 2) as 'Size (MB)' FROM information_schema.tables WHERE table_schema = 'wordpress'; ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully