MongoDB 是一个基于分布式文件存储的数据库. 它比MySQL更适合于WEB应用,在单机或群集应用中,有更高的扩展性及更好的性能表现.
在生产环境中,有2个选择:1是 MongoDB 官方版本,2是兼容版本 Percona Server for MongoDB . 两者在使用上基本上是完全一样,但后者比前者的性能更好.所以一般会推荐使用Percona的产品.
注意,软件随时在升级,所以在使用过程中,尽量参考官网的安装命令. 不要同时安装2套数据库
Percona Server for MongoDB
官网地址产品: https://www.percona.com/software/mongo-database/percona-server-for-mongodb
Debian下安装说明:https://www.percona.com/doc/percona-server-for-mongodb/3.4/install/apt.html
这里说一下apt方式安装软件的流程:
Debian中安装软件,使用的命令是apt-get或者apt,正常情况下,会从官方的网络源中下载并安装. 这些源在 /etc/apt/sources.list 文本文件中配置,一行一个.
如果使用第三方源,则还需要对应的数字签名认证.否则会出现 这样的错误:
gpgv: Signature made Wed Feb 13 10:30:20 2013 UTC using RSA key ID 9F1B8B32
gpgv: Can't check signature: public key not found
很多人不会手动的设置源和签名,所以一般流行的第三方软件会把这个过程做成另一个软件包,你只要下载下来执行一下就帮你设置好环境了.
1,先下载并配置安装源:
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
解读一下: wget 是下载命令,下载URL中的 $(lsb_release -sc)
是一个动态变量,取的是你的操作系统名称.比如说在Debian 8下面运行 lsb_release -sc
命令,显示的结果是: jessie
那么,这句命令如果运行失败(常会发生在便宜的vps中),其实你可以直接手动使用命令:
wget https://repo.percona.com/apt/percona-release_0.1-4.jessie_all.deb
下载下来的文件名子就叫: percona-release_0.1-4.jessie_all.deb
,这是一个安装包,相当于win下面的一个cab安装文件,运行它就可以了.
sudo dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
同样的道理,你可以直接手工运行:
dpkg -i percona-release_0.1-4.jessie_all.deb
那么问题来了, sudo
哪去了?
sudo命令是以其它用户身份执行命令,默认是root. 所以,一条命令加上 sudo,表示它是以root权限执行的. 正常情况下,推荐以root身份登录ssh,那么你就可以省掉这个命令.因为:
标准安装的debian系统中,sudo命令是不存在的,需要手工用 apt-get install sudo
安装.
那么源添加进去了,需要更新一下本地CACHE数据
sudo apt-get update
然后,你只要一条命令即可安装 Percona Server for MongoDB
sudo apt-get install percona-server-mongodb-34
安装完成以后,你可以检查一下运行状态:
service mongod status
应该是这样子显示的:
官网文档:https://docs.mongodb.com/master/tutorial/install-mongodb-on-debian/
这个步骤就是手动了,建议先 apt-get install sudo
补上这个命令,否则CTRL+C/V的过程中可能会出现问题.
先添加签名key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
再添加源
echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
如果运行失败,你也可以手工新建一个 /etc/apt/sources.list.d/mongodb-org-3.4.list
文件,文件内容只有1行:deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.4 main
同样,需要更新本地CACHE数据
apt-get update
再运行安装命令
apt-get install -y mongodb-org
安装完成以后,同样可以检查一下运行状态:
service mongod status
备份和恢复
Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。
mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。但是存在的问题时使用mongodump产生的备份不一定是数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和Mongodb实时数据相等。另外在备份时可能会对其它客户端性能产生不利的影响。
mongodump用法如下:
[[email protected] mongodb]# ./bin/mongodump --help
Export MongoDB data to BSON files.
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-o [ --out ] arg (=dump) output directory or "-" for stdout
-q [ --query ] arg json query
--oplog Use oplog for point-in-time snapshotting
--repair try to recover a crashed database
--forceTableScan force a table scan (do not use $snapshot)
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
如果不带参数,会在当前目录下新建一个 dump 目录 ,并备份所有的数据库.
mongorestore是Mongodb从备份中恢复数据的工具,它主要用来获取mongodump的输出结果,并将备份的数据插入到运行的Mongodb中。
mongorestore命令使用方法如下:
[[email protected] mongodb]# ./bin/mongorestore --help
usage: ./bin/mongorestore [options] [directory or filename to restore from]
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
--objcheck validate object before inserting
--filter arg filter to apply before inserting
--drop drop each collection before import
--oplogReplay replay oplog for point-in-time restore
--keepIndexVersion don't upgrade indexes to newest version
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
其它还有监视工具 mongostat
,显示当前数据库的负载统计每秒读写多少次什么的.
可视化管理
还有一个第3方的可视化管理工具 robomongo
数据库安装好以后,默认是只限本机访问的.所以你下载了可视化管理工具可能也无法直接连到你的数据库上去操作.
要解决这个问题,一般有几种做法:
1,直接修改 /etc/mongod.conf, 找到 bindIp那一项,把127.0.0.1改成0.0.0.0 并重启mongodb(service mongod restart) .这个会有安全问题,所以不推荐.
2,设置帐号密码,然后再进行第1步. 这个有些复杂,搞得定的自行搜索资料,搞不定的直接放弃吧.别折腾出新的问题来.
3,使用Robomongo,新建数据库连接,并设置SSH和数据库地址为127.0.0.1