文章目录
  1. 1. MongoDB数据库
    1. 1.1. 基础操作

MongoDB数据库

基础操作

注意自动创建了openshift,密码为为配置文件的

1
2
#set a password for db. example:dbpassword
A_DBPASSWORD=Chinaskills


mongo在/usr/bin/mongo

  1. 运行mongo
    mongo
  1. 切换数据库

    1
    > use admin
  2. 在admin数据库认证

    1
    2
    > db.auth('openshift','Chinaskills');
    1
  3. 切换指定数据库

    1
    > use xdcloud;
  4. 创建集合

    1
    2
    > db.createCollection('skill_competitor');
    { "ok" : 1 }
  5. 插入数据

    1
    2
    3
    > db.skill_competitor.insert(
    ... {year:2016,title:'2016Cloud Skill Competitor',Tip:'Name changed'}
    ... )

一条,一条的插入

1
2
> db.skill_competitor.insert(
... {year:2015,title:'2015Cloud Security Skill Competitor'})

  1. 查询集合记录
  • pretty()方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    > db.skill_competitor.find().pretty()
    {
    "_id" : ObjectId("573d6434c0d88cd36856d706"),
    "year" : 2016,
    "title" : "2016Cloud Skill Competitor",
    "Tip" : "Name changed"
    }
    {
    "_id" : ObjectId("573d6468c0d88cd36856d707"),
    "year" : 2015,
    "title" : "2015Cloud Security Skill Competitor"
    }
  • 常规查看:

    1
    2
    3
    > db.skill_competitor.find()
    { "_id" : ObjectId("573d6434c0d88cd36856d706"), "year" : 2016, "title" : "2016Cloud Skill Competitor", "Tip" : "Name changed" }
    { "_id" : ObjectId("573d6468c0d88cd36856d707"), "year" : 2015, "title" : "2015Cloud Security Skill Competitor" }
  1. 查看集合
    1
    > show collections;
  • 查看当前选择的数据库
    1
    2
    > show dbs;
    xdcloud



查看mongod状态:

  • 看服务

    1
    [root@broker ~]# service mongod status
  • 看是否在运行

    1
    2
    3
    4
    [root@broker ~]# netstat -lanp | grep "27017"
    tcp 0 0 192.168.200.160:27017 0.0.0.0:* LISTEN 1462/mongod
    tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1462/mongod
    unix 2 [ ACC ] STREAM LISTENING 12548 1462/mongod /tmp/mongodb-27017.sock

2.编辑iptables(脚本默认添加了,可以跳过)
[root@broker ~]# vi /etc/sysconfig/iptables

1
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
文章目录
  1. 1. MongoDB数据库
    1. 1.1. 基础操作