ubuntu 安装mongodb创建账号和库及添加坐标索引
一 安装
1.1 安装php-mongodb
sudo apt-get install php-mongodb
1.2 安装mongodb-org
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
如果是ubuntu14则
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
如果是ubuntu16或者18则
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
接下来
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo service mongod start # 启动
二 常见启动等命令
sudo service mongod start # 启动
sudo service mongod stop # 关闭
ps aux | grep mongod # 查看进程
三 创建表,角色与权限
3.1 管理员账号
mongo --host 127.0.0.1
use admin
db.createUser(
... {
... user: "test",
... pwd: "test@1111",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
db.getUser('test')
exit
3.2 对象库管理员账号及权限
mongo --host 127.0.0.1 -u 'test' --authenticationDatabase 'admin' -p 'test@1111' use admin db.auth('test','test@1111') use lbs db.createUser( ... { ... user: "testlbs", ... pwd: "testlbs1111", ... roles: [ { role: "readWrite", db: "lbs" }, ... { role: "read", db: "lbs" } ] ... } ... ) exit
3.3 对象库索引
mongo --host 127.0.0.1 -u 'testlbs' --authenticationDatabase 'lbs' -p 'testlbs1111' db use lbs db db.createCollection('lbs',{capped:false}) db.lbs.createIndex({'location':'2dsphere'}) db.lbs.createIndex({'uniqid':1},{unique:true})
四 卸载
4.1 关闭守护进程mongod
sudo service mongod stop
4.2 卸载安装的软件包
sudo apt-get purge mongodb-org*
4.3 移除数据库和日志文件(数据库和日志文件的路径取决于/etc/mongod.conf文件中的配置)
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb