简介:本文详细介绍NoSQL数据库的安装步骤与核心概念,帮助开发者快速掌握MongoDB、Redis等主流NoSQL的部署方法,同时解析NoSQL数据模型、查询语法及适用场景。
NoSQL(Not Only SQL)是区别于传统关系型数据库的非结构化数据存储系统,其核心特点包括:
典型应用场景对比:
| 数据库类型 | 适用场景 | 典型案例 |
|—————-|————-|————-|
| 键值存储 | 缓存系统、会话管理 | Redis缓存集群 |
| 文档存储 | JSON数据存储、内容管理 | MongoDB产品库 |
| 列族存储 | 时序数据、日志分析 | HBase监控系统 |
| 图数据库 | 社交网络、推荐系统 | Neo4j知识图谱 |
# 导入公钥sudo apt-get install gnupg curlcurl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb.gpg# 添加仓库echo "deb [signed-by=/usr/share/keyrings/mongodb.gpg] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list# 安装服务端sudo apt-get updatesudo apt-get install -y mongodb-org# 启动服务sudo systemctl start mongodsudo systemctl enable mongod
配置文件路径:/etc/mongod.conf
storage:dbPath: /var/lib/mongodbjournal:enabled: truenet:bindIp: 0.0.0.0 # 允许远程连接port: 27017security:authorization: enabled # 启用认证
# 安装依赖sudo dnf install -y gcc make tcl# 下载稳定版wget https://download.redis.io/redis-stable.tar.gztar xzf redis-stable.tar.gzcd redis-stable# 编译安装make && sudo make install# 创建服务单元sudo vi /etc/systemd/system/redis.service
服务文件内容:
[Unit]Description=Redis In-Memory Data StoreAfter=network.target[Service]User=redisGroup=redisExecStart=/usr/local/bin/redis-server /etc/redis.confExecStop=/usr/local/bin/redis-cli shutdownRestart=always[Install]WantedBy=multi-user.target
配置文件关键项:
maxmemory 4gb # 内存限制maxmemory-policy allkeys-lru # 淘汰策略save 900 1 # 900秒内1次修改则持久化rdbcompression yes # 启用压缩
// 插入文档db.products.insertOne({name: "Laptop",price: 999.99,specs: {cpu: "i7",ram: "16GB"}})// 查询操作db.products.find({ "specs.cpu": "i7" }, { name: 1, _id: 0 })// 聚合管道db.orders.aggregate([{ $match: { status: "completed" } },{ $group: { _id: "$customer", total: { $sum: "$amount" } } }])
# 字符串操作SET user:1001:name "Alice"GET user:1001:name# 哈希表操作HSET user:1001 profile '{"age":30,"city":"NY"}'HGETALL user:1001# 有序集合ZADD leaderboard 1000 "Player1"ZRANGE leaderboard 0 -1 WITHSCORES
MongoDB索引类型:
db.users.createIndex({ email: 1 })db.orders.createIndex({ customer: 1, date: -1 })db.articles.createIndex({ content: "text" })Redis索引方案:
架构组成:
部署命令示例:
# 启动配置服务器mongod --configsvr --replSet configReplSet --dbpath /data/configdb# 启动分片服务器mongod --shardsvr --replSet shard1 --dbpath /data/shard1# 初始化分片集群mongo --host mongos_hostsh.addShard("shard1/host1:27018,host2:27018")sh.enableSharding("mydb")sh.shardCollection("mydb.users", { userid: "hashed" })
# 启动节点(每个节点不同端口)redis-server --port 7000 --cluster-enabled yes \--cluster-config-file nodes-7000.conf \--cluster-node-timeout 5000# 创建集群redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 \--cluster-replicas 1
| 评估项 | MongoDB | Redis | Cassandra | Neo4j |
|---|---|---|---|---|
| 写入性能 | ★★★☆ | ★★★★★ | ★★★★☆ | ★★☆ |
| 查询灵活性 | ★★★★☆ | ★★★☆ | ★★☆ | ★★★★★ |
| 分布式支持 | ★★★★☆ | ★★☆ | ★★★★★ | ★★★ |
| 事务支持 | ★★★☆ | ★★☆ | ★★☆ | ★★★☆ |
/etc/mongod.conf中的security.authorization设置netstat -tulnp | grep 27017df -h检查数据目录所在分区maxmemory参数后执行CONFIG REWRITEBGSAVE生成RDB文件,通过SCP传输到新服务器appendonly和rdb配置项是否冲突本文通过系统化的安装指导、操作示例和选型建议,为开发者构建了完整的NoSQL知识体系。建议初学者从MongoDB文档操作入手,逐步掌握分布式架构设计,最终根据业务需求选择最适合的NoSQL解决方案。