本教程适合 Mac 及 linux 用户的二进制版安装。
Mac, Linux 用户的源码版安装,参见这里。
Windows 用户的二进制版安装,参见这里。
Windows 用户的源码版安装,参见这里。
安装步骤:
下载 leanote 二进制版。
安装 mongodb。
导入初始数据。
配置 leanote。
运行 leanote。
由此处下载 leanote 最新二进制版。
假设将文件下载到 /home/user1 目录下, 解压文件从而在 /home/user1 目录下生成 leanote目录:
$> cd /home/user1
$> tar -xzvf leanote-darwin-amd64.v2.0.bin.tar.gz
到 mongodb 官网 下载相应系统的最新版安装包,或者从以下链接下载旧版本:
64位 linux mongodb 3.0.1 下载链接: https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.1.tgz
下载到 /home/user1
下, 直接解压即可:
$> tar -xzvf mongodb-linux-x86_64-3.0.1.tgz/
$> mv mongodb-linux-x86_64-3.0.6/ /home/mongodb # 将解压包拷贝到指定目录
$> mkdir -p /data/db # 创建数据库目录
为了快速使用mongodb命令, 可以配置环境变量。编辑 ~/.profile
或/etc/profile
文件, 将mongodb/bin路径加入即可:
$> sudo vim /etc/profile
此处实例使用了vim文本编辑器,你可以使用自己熟悉的编辑器。
在/etc/profile中添加以下行,注意把用户名(u
1.查找
db.system.users.find().pretty()
show users
db.table.find()
db.table.find().pretty()
db.col.getIndexes()
db.col.totalIndexSize()
db.col.dropIndexes()
db.col.dropIndex("索引名称")
// LIKE模糊查询userName包含A字母的数据(%A%)
// SQL:SELECT * FROM UserInfo WHERE userName LIKE "%A%"
db.UserInfo.find({userName :/A/})
// LIKE模糊查询userName以字母A开头的数据(A%)。
// SQL:SELECT * FROM UserInfo WHERE userName LIKE "A%"
db.UserInfo.find({userName :/^A/})
//查询商品名称长度大于25个字符的商品
db.item.find({item_name:{$exists:true},$where:"(this.item_name.length > 25)"}).limit(5)
db.item.find({"item_name": {"$exists": true, "$regex": /^.{25,}$/}}).limit(5)
//查询商品名称长度小于5个字符的商品
db.item.find({$where:"this.item_name.length < 5"}).limit(5)
db.item.find({"item_name": {"$regex": /^.{0,5}$/}}).limit(5)
2.创建
MongoDB的访问控制能够有效保证数据库的安全,访问控制是指绑定Application监听的IP地址,设置监听端口,使用账户和密码登录
mongod 参数:--bind_ip
默认值是所有的IP地址都能访问,该参数指定MongoDB对外提供服务的绑定IP地址,用于监听客户端 Application的连接,客户端只能使用绑定的IP地址才能访问mongod,其他IP地址是无法访问的。
mongod 参数:--port
MongoDB 默认监听的端口是27017,该参数显式指定MongoDB实例监听的TCP 端口,只有当客户端Application连接的端口和MongoDB实例监听的端口一致时,才能连接到MongoDB实例。
mongod 参数:--auth
默认值是不需要验证,即 --noauth,该参数启用用户访问权限控制;当mongod 使用该参数启动时,MongoDB会验证客户端连接的账户和密码,以确定其是否有访问的权限。如果认证不通过,那么客户端不能访问MongoDB的数据库。
Enables authorization to control user’s access to database resources and operations. When authorization is enabled, MongoDB requires all clients to authenticate themselves first in order to determine the access for the client.
mongo 参数:--username <username>, -u <username>
mongo 参数:--password <password>, -p <password>
mongo 参数:--authenticationDatabase <dbname>
mongo -u workerman_rw -p DQP5cK7! --authenticationDatabase
mongodump命令脚本语法如下:
>mongodump -h dbhost -d dbname -o dbdirectory --port 27017 -u username -p password