Linux安装MongoDB-unijhql
这里介绍Centos版本的linux使用yum安装。采用yum安装方便些其他操作系统安装方式请参考:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/#data-directories-and-permissions
1.先要配置yum源:
#这是配置3.6版本的yum源
# vim /etc/yum.repos.d/mongodb.repo
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
#如果需要配置3.6版本之前的yum源,例如3.4
# /etc/yum.repos.d/mongodb.repo
name=MongoDB 3.4 Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=0
enabled=1
2. 重新加载yum源
# yum clean all
# yum repolist
#yum makecache
3. 安装MongoDB
3.1安装最新版本
#yum -y install mongodb-org
3.2指定版本安装
#yum install -y mongodb-org-3.6.5 mongodb-org-server-3.6.5 mongodb-org-shell-3.6.5 mongodb-org-mongos-3.6.5 mongodb-org-tools-3.6.5
注意:指定版本安装的时候还需要配置写其配置文件,在配置文件中/etc/yum.conf中写入:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
4.安装后:
4.1配置selinux
#semanage port -a -t mongod_port_t -p tcp 27017
或者直接关闭selinux(下面是临时关闭)
#setenforce 0
永久关闭,编辑配置文件/etc/selinux/config
将SELINUX=enforcing改成SELINUX=disable
5.启动MongoDB
# service mongod start
Starting mongod (via systemctl):
#查看mongo是否启动
# ps -fe | grep mongod
mongod 26231 13 10:39 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf
加入开机自启
# sudo chkconfig mongod on
6关闭MongoDB
# service mongod stop
Stopping mongod (via systemctl):
7.删除安装包
#sudo yum erase $(rpm -qa | grep mongodb-org)
8.删除产生的数据
#sudo rm -r /var/log/mongodb
#sudo rm -r /var/lib/mongo
9通过windows连接mongodb数据库
下载图形化界面的MongoDB管理工具,去这里:
https://robomongo.org/
然后需要关闭selinux,关闭firewalld。修改linux上的mongodb数据库的配置文件
# vim /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1# Listen to local interface only, comment to listen on all interfaces.
将上面的127.0.0.1修改为====》0.0.0.0
重启服务
# /etc/init.d/mongod restart
#然后就可以连接了
10.测试python连接MongoDB
先在linux上创建一个测试检验的数据
# mongo
use local
switched to db local
db.local.insert({'a':'b'})
WriteResult({ "nInserted" : 1 })
在python中写入以下程序
from pymongo import MongoClient#导入模块,这里不说怎么安装了,百度
#建立数据库连接(先是要连接的IP,再是端口号)
client = MongoClient('192.168.10.15',27017)
#连接所需数据库,local为数据库名
db=client.local
#连接所用集合,也就是我们通常所说的表,local为表名
collection=db.local
#查找集合中所有数据
for item in collection.find():
print(item)
页:
[1]