标签归档:mongodb

PyMongo基本使用

引用PyMongo

import pymongo

创建连接Connection

import pymongo

conn = pymongo.Connection('localhost',27017)

from pymongo import Connection

conn = Connection('localhost',27017)

创建Connection时,指定host及port参数

import pymongo
conn = pymongo.Connection(host='127.0.0.1',port=27017)

连接数据库

db = conn.ChatRoom

db = conn['ChatRoom']

连接聚集

account = db.Account

或 account = db[“Account”]

查看全部聚集名称

db.collection_names()

查看聚集的一条记录

db.Account.find_one()
db.Account.find_one({"UserName":"keyword"})

查看聚集的字段

db.Account.find_one({},{"UserName":1,"Email":1})
// {u'UserName': u'libing', u'_id': ObjectId('4ded95c3b7780a774a099b7c'), u'Email': u'libing@35.cn'}
db.Account.find_one({},{"UserName":1,"Email":1,"_id":0})
// {u'UserName': u'libing', u'Email': u'libing@35.cn'}

查看聚集的多条记录

for item in db.Account.find():
    item

for item in db.Account.find({"UserName":"libing"}):
    item["UserName"]

查看聚集的记录统计

db.Account.find().count()
db.Account.find({"UserName":"keyword"}).count()

聚集查询结果排序

db.Account.find().sort("UserName")  --默认为升序
db.Account.find().sort("UserName",pymongo.ASCENDING)   --升序
db.Account.find().sort("UserName",pymongo.DESCENDING)  --降序

聚集查询结果多列排序

db.Account.find().sort([("UserName",pymongo.ASCENDING),("Email",pymongo.DESCENDING)])

添加记录

db.Account.insert({"AccountID":21,"UserName":"libing"})

修改记录

db.Account.update({"UserName":"libing"},{"$set":{"Email":"libing@126.com","Password":"123"}})

删除记录

db.Account.remove()   -- 全部删除
db.Test.remove({"UserName":"keyword"})

amazon aws linux下python环境安装

允许远程通过密码登录

给root设置一个密码,允许远程通过密码登录

chmod 400 xxx.pem
ssh -i xxx.pem ec2-user@host-ip    
sudo passwd root
sudo su -
vim /etc/ssh/sshd_config 

/etc/ssh/sshd_config内容如下

PermitRootLogin yes
PasswordAuthentication yes
UsePAM yes

然后

service sshd reload

开启epel

amazon aws如果采用redhat需要额外收授权费,目前aws官方2014.3基于centos 6,许多软件版本较旧,可以通过epel软件仓库安装一些新软件

yum-config-manager --enable epel
yum update

安装基本环境

yum groupinstall "Development tools" -y
yum install openssl-devel libxslt-devel libxml2-devel libffi-devel -y

安装mysql客户端

vim /etc/yum.repos.d/MariaDB.repo

# MariaDB 5.5 CentOS repository list - created 2014-09-13 05:43 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安装MariaDB客户端

yum install MariaDB-client mysql-devel -y

安装redis客户端

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum-config-manager --enable remi
yum install redis -y

安装mongodb客户端

vim /etc/yum.repos.d/mongodb.repo

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

安装mongodb客户端

yum update
yum install mongodb-org-shell -y

安装python环境

# yum install centos-release-SCL (aliyun only)
yum install python27*
yum install freetype-devel libjpeg-devel libpng-devel
# scl enable python27 bash (aliyun only)
virtualenv --no-site-packages /data/pyenv
source /data/pyenv/bin/activate
pip install redis cryptography sqlalchemy flask simplejson mongoengine python-amazon-product-api scrapy mysql-python gunicorn gevent

安装glusterfs客户端

#install glusterfs repo
wget -P /etc/yum.repos.d http://download.gluster.org/pub/gluster/glusterfs/LATEST/EPEL.repo/glusterfs-epel.repo
#fix it for amazon linux
sed -i 's/$releasever/6/g' /etc/yum.repos.d/glusterfs-epel.repo 

#install glusterfs
yum install -y glusterfs-fuse

#setup fstab
echo "172.31.42.77:/pcvol /data_pcvol glusterfs defaults,noatime 0 0" >> /etc/fstab

#mount
mkdir /data_pcvol
mount -a
ls /data_pcvol

挂载日志磁盘

lsblk
mkfs -t ext4 /dev/xvdf
echo '/dev/xvdf       /data_log   ext4    defaults,nofail        0       2' >> /etc/fstab
mkdir /data_log
mount -a