虚拟机与开发环境配置踩坑记录
经常需要搭建各种开发环境,VMware虚拟机用得比较多。记录一下Windows虚拟机安装、Redis配置等踩过的坑。
VMware虚拟机配置
Windows 11虚拟机
Windows 11对硬件有最低要求,虚拟机配置要注意。
最低配置要求:
- 内存:4GB(建议8GB+)
- 处理器:2核(建议4核)
- 磁盘:64GB(建议100GB+)
- 显卡:支持DirectX 12
VMware配置步骤:
- 创建新虚拟机,选择Windows 11 x64
- 处理器:2个处理器,每个2核
- 内存:8GB (8192 MB)
- 硬盘:100GB,单个文件
- 网络:NAT模式
坑1:Windows 11安装卡网络
安装Windows 11时,如果没有网络连接,会卡在”糟糕,你已断开Internet连接”。
绕过方法:
坑2:虚拟机文件锁定
错误信息:
1 2
| 另一个程序已锁定文件的一部分,进程无法访问 Cannot open the disk. The file is locked.
|
原因:虚拟机未正常关闭,锁文件未清除。
解决方法:
1 2 3 4 5 6 7 8
|
cd "虚拟机目录" del /s /q *.lck rmdir /s /q *.lck
|
性能优化
内存优化:
- 分配物理内存的50-75%
- 优先级:抓取的输入内容设”高”
- 高级设置:禁用内存页面修整
磁盘优化:
- 定期进行碎片整理
- 压缩磁盘
- 删除不必要的快照
- 虚拟机文件放SSD
Windows开发环境
Windows安装Redis
下载:https://github.com/tporadowski/redis/releases
下载 .msi 安装包,运行安装程序。
配置文件:
Windows版本包含两个配置文件:
redis.windows.conf - 手动启动配置
redis.windows-service.conf - 服务启动配置
常用命令:
1 2 3 4 5 6 7 8 9
| redis-server.exe redis.windows.conf
redis-cli.exe
PING
|
配置为Windows服务:
1 2 3 4 5 6 7
| redis-server --service-install redis.windows-service.conf --loglevel verbose
redis-server --service-start redis-server --service-stop redis-server --service-uninstall
|
常用配置项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| # 绑定IP bind 127.0.0.1
# 端口 port 6379
# 密码认证(生产环境必需) requirepass your_password
# 持久化 save 900 1 save 300 10 save 60 10000
# 内存限制 maxmemory 256mb maxmemory-policy allkeys-lru
|
Node.js版本管理
Windows上推荐用nvm-windows管理Node.js版本。
安装:https://github.com/coreybutler/nvm-windows/releases
常用命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| nvm list
nvm install 14.21.3 nvm install 16.13.1 nvm install 18.12.1
nvm use 16.13.1
node -v npm -v
|
配置国内镜像:
编辑 C:\Users\用户名\AppData\Roaming\nvm\settings.txt:
1 2 3 4
| arch: 64 proxy: none node_mirror: http://npm.taobao.org/mirrors/node/ npm_mirror: https://npm.taobao.org/mirrors/npm/
|
Git代理配置
查看当前配置:
1 2
| git config --global --get-all http.proxy git config --global --get-all https.proxy
|
配置代理:
1 2 3 4 5 6 7
| git config --global http.proxy http://代理IP:端口 git config --global https.proxy http://代理IP:端口
git config --global --unset http.proxy git config --global --unset https.proxy
|
SSH代理配置:
编辑 ~/.ssh/config:
1 2 3 4
| Host github.com Hostname github.com User git ProxyCommand connect -S 代理IP:端口 %h %p
|
坑3:dl.google.com无法访问
Android开发中经常遇到dl.google.com无法访问。
解决方法:
- 访问 https://site.ip138.com/dl.google.com/ 查询可用IP
- 修改Hosts文件
C:\Windows\System32\drivers\etc\hosts:
1 2
| 108.160.166.137 dl.google.com 108.160.166.137 dl-ssl.google.com
|
- 刷新DNS缓存:
Linux开发环境
查看CentOS版本
1 2 3 4 5 6 7 8 9
| cat /etc/redhat-release
uname -a
cat /etc/os-release
|
坑4:CentOS 8源配置错误
错误信息:
1 2
| Error: Failed to download metadata for repo 'AppStream' Cannot prepare internal mirrorlist: No URLs in mirrorlist
|
原因:CentOS 8已于2021年12月31日停止维护,官方源已不可用。
解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| cd /etc/yum.repos.d/
sudo mkdir backup sudo mv *.repo backup/
sudo curl -o CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
sudo yum clean all sudo yum makecache
sudo yum update -y
|
进程内存监控
1 2 3 4 5 6 7 8 9 10 11 12 13
| ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' --sort=-rsz | head -20
netstat -nat | grep ESTABLISHED | grep -i "端口号" | wc -l
|
防火墙配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| sudo systemctl start firewalld sudo systemctl enable firewalld
sudo systemctl status firewalld sudo firewall-cmd --list-all
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent sudo firewall-cmd --reload
sudo systemctl stop firewalld
|
Docker容器环境
Docker安装
1 2 3 4 5 6 7 8 9
| sudo yum install -y docker
sudo systemctl start docker sudo systemctl enable docker
sudo docker --version
|
Redis容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| docker pull redis:latest
docker run -d \ --name redis \ -p 6379:6379 \ -v /data/redis:/data \ redis:latest \ redis-server --appendonly yes
docker ps
docker exec -it redis redis-cli
|
MongoDB容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| docker pull mongo:6.0
docker run -d \ --name mongodb \ -p 27017:27017 \ -v /data/mongo:/data/db \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=password \ mongo:6.0
docker logs mongodb
|
虚拟机备份策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #!/bin/bash
VM_DIR="/vmware" BACKUP_DIR="/backup/vmware" DATE=$(date +%Y%m%d)
vmrun stop $VM_DIR/开发环境/开发环境.vmx
tar -czf $BACKUP_DIR/dev-env-$DATE.tar.gz $VM_DIR/开发环境/
ls -t $BACKUP_DIR/*.tar.gz | tail -n +6 | xargs rm -f
echo "Backup completed: dev-env-$DATE.tar.gz"
|
总结
开发环境配置经常遇到各种坑,记录一下最常见的:
- Windows 11安装要绕过网络检查,用
oobe\bypassnro
- 虚拟机文件锁定就删除.lck文件
- CentOS 8官方源已挂,要换阿里云vault源
- dl.google.com被墙,改hosts解决
- Redis Windows版配置服务时注意区分conf文件
- nvm-windows配置国内镜像加速下载
建议虚拟机做好快照管理,关键节点备份,免得环境崩了从头再来。