Chạy redis trên windows server
Posted on: 7/4/2025 2:43:00 PM
I. Chả có gì mới, chỉ là hết server thôi anh em ạ...
Bạn biết đấy, Redis không được thiết kế để chạy trên Windows. Mấy bản port Redis cũ từ Microsoft đã về với đất từ lâu. Nhưng mình thì đang kẹt quá, cần Redis gấp mà server thì chưa lên. Vậy giải pháp là gì?
Dùng Redis trong WSL Ubuntu — nghe đơn giản, làm cũng đơn giản... à mà, không hẳn, nhưng vui 😆
II. Cài Redis qua WSL Ubuntu
Đầu tiên, nếu bạn chưa có WSL, bật nhanh bằng lệnh:
wsl --install
(hoặc bật thủ công qua Features nếu máy bạn không chạy Windows 11)
Tiếp theo, cài Ubuntu từ Microsoft Store. Sau khi có Ubuntu, bật terminal WSL và làm tiếp:
sudo apt update sudo apt install redis-server -y
Boom! Redis đã có trên Windows... à nhầm, trên Ubuntu trong Windows 😏
III. Chỉnh config cho Redis để hoạt động như người lớn
Redis mặc định bật protected mode, chỉ nghe local, và không có password — xấu hổ lắm 😳 Nếu bạn muốn dùng Redis như một dịch vụ thật sự (hoặc ít nhất là kết nối được từ ngoài), cần chỉnh mấy thứ sau:
Chỉnh file cấu hình:
sudo nano /etc/redis/redis.conf
1. Đặt mật khẩu:
Tìm dòng:
# requirepass foobared
Sửa lại (bỏ dấu #
):
requirepass your_super_secret_password
2. Tắt protected mode:
Tìm dòng:
protected-mode yes
Sửa thành:
protected-mode no
3. Mở port cho mọi địa chỉ:
Tìm dòng:
bind 127.0.0.1 ::1
Sửa thành:
bind 0.0.0.0
Rồi restart Redis:
sudo service redis-server restart
IV. Kết nối từ Windows ra WSL như dân chơi thực thụ
Redis trong WSL sẽ nghe ở IP nội bộ riêng, muốn dùng từ Windows (hoặc máy khác), ta phải forward port.
1. Lấy IP của WSL:
ip addr
Tìm dòng nào có eth0
, lấy cái IP trông như 172.28.234.105
(ví dụ thôi nha).
2. Forward port Redis từ Windows:
Mặc định Redis chạy ở cổng 6379 trong WSL. Để Windows truy cập được, chạy lệnh sau trong PowerShell (quyền admin):
netsh interface portproxy add v4tov4 listenport=6379 listenaddress=0.0.0.0 connectport=6379 connectaddress=172.28.234.105
(Bạn nhớ thay connectaddress
bằng IP thật từ lệnh ip addr
ở trên nha)
3. Mở firewall (nếu cần):
New-NetFirewallRule -DisplayName "Redis Port 6379" -Direction Inbound -LocalPort 6379 -Protocol TCP -Action Allow
V. Done! Test Redis nào
Bây giờ từ bất kỳ ứng dụng nào trên máy Windows hoặc từ một máy khác cùng mạng (nếu bạn làm đúng phần firewall và port forwarding), bạn có thể kết nối Redis bằng IP máy Windows và cổng 6379. Ví dụ:
redis-cli -h 127.0.0.1 -p 6379 -a your_super_secret_password
VI. Lời kết: Có thể không ngầu, nhưng đủ dùng!
Redis trong WSL không phải cách tối ưu nhất — nhưng trong lúc thiếu server, nó lại là người bạn đồng hành không tồi.
Giống như mặc đồ ngủ mà vẫn đi họp Zoom: trông hơi kỳ, nhưng vẫn hoàn thành công việc 😄
Nếu bạn thấy bài viết này hữu ích, đừng quên lưu lại vì… sớm muộn gì rồi bạn cũng sẽ thiếu server như mình thôi!
Disclaimer: The opinions expressed in this blog are solely my own and do not reflect the views or opinions of my employer or any affiliated organizations. The content provided is for informational and educational purposes only and should not be taken as professional advice. While I strive to provide accurate and up-to-date information, I make no warranties or guarantees about the completeness, reliability, or accuracy of the content. Readers are encouraged to verify the information and seek independent advice as needed. I disclaim any liability for decisions or actions taken based on the content of this blog.