二进制安装
下载安装包
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.5.tgz
解压
tar axf mongodb-linux-x86_64-2.6.5.tgz -C /opt/
ln -s /opt/mongodb-linux-x86_64-2.6.5/ /opt/mongodb
创建日志、数据存放目录
mkdir -pv /opt/mongodb/{data,etc,logs}
创建配置文件
vim /opt/mongodb/etc/mongodb.conf
# 日志文件位置
logpath=/opt/mongodb/logs/mongodb.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
# 默认27017
port = 27017
# 数据库文件位置
dbpath=/opt/mongodb/data/
# 启用定期记录CPU利用率和 I/O 等待
#cpu = true
# 是否以安全认证方式运行,默认是不认证的非安全方式
#noauth = true
auth = true
# 详细记录输出
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)用于开发驱动程序时验证客户端请求
#objcheck = true
# Enable db quota management
# 启用数据库配额管理
#quota = true
# 设置oplog记录等级
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 3=both
# 7=W+some reads
#diaglog=0
# Diagnostic/debugging option 动态调试项
#nocursors = true
# Ignore query hints 忽略查询提示
#nohints = true
# 禁用http界面,默认为localhost:28017
#nohttpinterface = true
# 关闭服务器端脚本,这将极大的限制功能
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# 关闭扫描表,任何查询将会是扫描失败
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# 关闭数据文件预分配
# Disable data file preallocation.
#noprealloc = true
# 为新数据库指定.ns文件的大小,单位:MB
# Specify .ns file size for new databases.
# nssize =
# Replication Options 复制选项
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#指定存储身份验证信息的密钥文件的路径
#keyFile=/path/to/keyfile
添加mongodb用户
useradd mongodb
echo '123456! '|passwd mongodb --stdin
编写管理脚本
vim /etc/init.d/mongodb
#!/bin/sh
#
# mongodb - this script starts and stops the mongodb daemon
#
# chkconfig: - 85 15
# description: MongoDB is a non-relational database storage system.
# processname: mongodb
# config: /opt/mongodb/etc/mongodb.conf
# pidfile: /opt/mongodb/mongodb.pid
PATH=/opt/mongodb/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=mongodb
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting MongoDB... "
#/opt/mongodb/bin/mongod --port 27017 --fork --dbpath=/opt/mongodb/data/ --config=/opt/mongodb/etc/m
ongodb.conf --logpath=/opt/mongodb/logs/mongodb.log --logappend su - mongodb -c "/opt/mongodb/bin/mongod --config=/opt/mongodb/etc/mongodb.conf"
;;
stop)
echo -n "Stopping MongoDB... "
/opt/mongodb/bin/mongod --shutdown --dbpath=/opt/mongodb/data/
;;
restart)
$0 stop
$0 start
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop}" >&2
exit 1
;;
esac
exit 0
修改权限
chown -R mongodb.mongodb /opt/mongodb-linux-x86_64-2.6.5
启动
chmod a+x /etc/init.d/mongodb
/etc/init.d/mongodb start
修改环境变量
vim /etc/profile
#mongodb
export PATH=$PATH:/opt/mongodb/bin
mongodb常用命令
连接mongodb
mongo 127.0.0.1:27017/admin
该连接方式是连接127.0.0.1上27017端口mongodb中的admin库
帮助信息
help
\help
db.mycoll.help()
库表基本操作
查看所有数据库
show dbs
切换数据库
use DBNAME
查看所有表
show tables
查看当前使用的数据库
db.getName()
db
显示当前db状态
db.stats()
查看当前db的链接机器地址
db.getMongo()
用户管理
查看当前库所有用户
show users
查看权限列表
show roles
添加管理员用户
db.createUser(
{
user:"python",
pwd:"python",
roles:
[
{
role:"userAdminAnyDatabase",
db:"admin"
}
]
}
)
创建读写权限普通用户
use 库名
db.createUser(
{
user:"name",
pwd:"password",
roles:[{ role:" readWrite",db:”库名”}]
}
)
用户认证,密码设置
db.auth("userName", "123123")
删除用户
db.removeUser("userName")
启动关闭
用脚本
service mongodb start
service mongodb stop
使用命令
/opt/mongodb/bin/mongod --config=/opt/mongodb/etc/mongodb.conf
/opt/mongodb/bin/mongod --shutdown --dbpath=/opt/mongodb/data/
聚合(表)管理
查看帮助信息
db.help
得到当前db的所有聚集集合
db.getCollectionNames()
创建一个聚集集合(table)
show tables;
db.createCollection('test',{size:20,capped:5,max:100})
得到指定名称的聚集集合(table)
db.getCollection("test");
显示当前db所有聚集索引的状态
db.printCollectionStats()
查询数据
db.聚合名(表名).find()
db.tb.find()
db.聚合名(表名).find({条件},{键})
db.tb.find({},{_id:0,_class:1})
_id:0中_id:是键,0表示不显示,非零表示显示
按条件查询
比较符:$gt----大于,$gte----大于等于,$lt----小于,$lte----小于等于,$eq----等于,$ne----不等于
db.tb.find({num:{$eq:4}},{_id:0,_class:1,num:4})
db.tb.find({num:{$gt:3,$lt:7}},{_id:0,_class:1,num:4})
正则表达式
测试失败。。。。。。
开放防火墙
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
service iptables reload
yum源安装
yum install -y mongodb-server mongodb
配置
vim /etc/mongod.conf
# Comma separated list of ip addresses to listen on (all local ips by default)
bind_ip =192.168.100.76
# Specify port number (27017 by default)
port = 27017
# Fork server process (false by default)
fork = true
# Full path to pidfile (if not set, no pidfile is created)
pidfilepath = /var/run/mongodb/mongod.pid
# Log file to send write to instead of stdout - has to be a file, not directory
logpath = /var/log/mongodb/mongod.log
# Alternative directory for UNIX domain sockets (defaults to /tmp)
unixSocketPrefix = /var/run/mongodb
# Directory for datafiles (defaults to /data/db/)
dbpath = /data/mongodb
# Use a smaller default file size (false by default)
smallfiles = true
mkdir -pv /data/mongodb
chown -R mongodb.mongodb /data/mongodb
启动
systemctl start mongod
systemctl enable mongod