Python2.7.9+或Python3.4+版本,内置了pip
安装pip
# unzip setuptools-38.5.1.zip
# cd setuptools-38.5.1
# python setup.py install
# tar -zxvf pip-9.0.1.tar.gz
# cd pip-9.0.1
# python setup.py install
# unzip setuptools-38.5.1.zip
# cd setuptools-38.5.1
# python setup.py install
# tar -zxvf pip-9.0.1.tar.gz
# cd pip-9.0.1
# python setup.py install
yum -y install git gcc zlib zlib-devel openssl openssl-devel
pyenv的安装
# mkdir .pyenv
# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
# echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
# echo 'export PATH="$PYENV_ROOT/bin/:$PATH"' >> ~/.bash_profile
初始化
# echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# source .bash_profile
使用pyenv安装不同的Python版本
# pyenv install --list
# pyenv install -v 2.7.13
# pyenv install -v 3.6.8
# pyenv versions
# pyenv global 2.7.13
# pyenv global 3.6.8
# mkdir .pyenv
# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
# echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
# echo 'export PATH="$PYENV_ROOT/bin/:$PATH"' >> ~/.bash_profile
初始化
# echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# source .bash_profile
使用pyenv安装不同的Python版本
# pyenv install --list
# pyenv install -v 2.7.13
# pyenv install -v 3.6.8
# pyenv versions
# pyenv global 2.7.13
# pyenv global 3.6.8
# pip install pymongo
>use mydb
>db.createUser(
{
user: "mydbuser",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "mydb" },
{ role: "read", db: "test" } ]
}
)
>db.createUser(
{
user: "mydbuser",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "mydb" },
{ role: "read", db: "test" } ]
}
)
普通连接
import re
from pymongo import MongoClient
client = MongoClient('192.168.1.101',27017)
# 获取数据库对象
db = client.mydb
db.authenticate("mydbuser","xyz123")
db = client.mydb
db.authenticate("mydbuser","xyz123")
# 获取集合对象
student = db.student
student = db.student
# 插入一条数据,并获取返回结果
res = student.insert_one({"name":"老王"})
# 获取插入之后该条数据的id
object_id = res.inserted_id
print(object_id)
res = student.insert_one({"name":"老王"})
# 获取插入之后该条数据的id
object_id = res.inserted_id
print(object_id)
# 插入9条数据
res = student.insert_many([{"name":"name%d"%index} for index in range(1,10)])
# 获取插入之后该9条数据的ids,object_ids为一个list
object_ids = res.inserted_ids
print(object_ids)
res = student.insert_many([{"name":"name%d"%index} for index in range(1,10)])
# 获取插入之后该9条数据的ids,object_ids为一个list
object_ids = res.inserted_ids
print(object_ids)
# 查询满足条件的所有数据,res为一个pymongo.cursor.Cursor对象
res = student.find({"name":"老王"})
# 获取数据个数
print(res.count())
for index in res:
# index为一个dict。注意:这个循环只能进行一次,如需再次操作返回结果,需要在find一次,或将list(res),将这个返回结果保存起来
print(index)
res = student.find({"name":"老王"})
# 获取数据个数
print(res.count())
for index in res:
# index为一个dict。注意:这个循环只能进行一次,如需再次操作返回结果,需要在find一次,或将list(res),将这个返回结果保存起来
print(index)
# 查询并更新。{"name":"老王"}为查询条件;{"$set":{"addr":"家住隔壁"}}更新数据;upsert=False找不到不插入数据,upsert=True找不到则插入数据
# res为返回结果,res为一个字典对象,是之前数据的字典
res = student.find_one_and_update({"name":"老王"},{"$set":{"addr":"家住隔壁"}},upsert=False)
# 查询并更新。{"name":"老王"}为查询条件;{"$set":{"addr":"家住隔壁"}}更新数据;multi:true更改所有匹配的数据,默认false,只匹配第一条
res = student.update({"name":"老王"},{"$set":{"addr":"家住隔壁"}},{multi:True})
# res为返回结果,res为一个字典对象,是之前数据的字典
res = student.find_one_and_update({"name":"老王"},{"$set":{"addr":"家住隔壁"}},upsert=False)
# 查询并更新。{"name":"老王"}为查询条件;{"$set":{"addr":"家住隔壁"}}更新数据;multi:true更改所有匹配的数据,默认false,只匹配第一条
res = student.update({"name":"老王"},{"$set":{"addr":"家住隔壁"}},{multi:True})
student.delete_one({"name":"老王"})
student.delete_many({"name":"老王"})
student.remove({"name":"老王"})
student.remove({"name":re.compile("name")})
复制集连接
import time
import random
import string
from datetime import datetime
from pymongo import MongoClient
student.delete_many({"name":"老王"})
student.remove({"name":"老王"})
student.remove({"name":re.compile("name")})
复制集连接
import time
import random
import string
from datetime import datetime
from pymongo import MongoClient
#client = MongoClient('192.168.1.101,192.168.1.102',27017)
client = MongoClient('mongodb://192.168.1.101:27017,192.168.1.102:27017')
db = client.mydb
db.authenticate("mydbuser","xyz123")
zs_user = db.zs_user
tt = db.tt
client = MongoClient('mongodb://192.168.1.101:27017,192.168.1.102:27017')
db = client.mydb
db.authenticate("mydbuser","xyz123")
zs_user = db.zs_user
tt = db.tt
'''
create table zs_user(uid int auto_increment,name varchar(32),age tinyint,add_time datetime,primary key(uid),key(name));
create table tt(uid int auto_increment,name varchar(32),primary key(uid),key(name));
'''
create table zs_user(uid int auto_increment,name varchar(32),age tinyint,add_time datetime,primary key(uid),key(name));
create table tt(uid int auto_increment,name varchar(32),primary key(uid),key(name));
'''
def insert():
i = 1
while True:
r_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(10,30)))
r_age = random.randint(15,70)
print("r_name = %s " %r_name)
print("r_age = %d " %r_age)
zs_user.insert_one({"name":r_name,"age":r_age,"add_time":datetime.now()})
tt.insert_one({"name":r_name})
i = i + 1
time.sleep(0.1)
print(i)
i = 1
while True:
r_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(10,30)))
r_age = random.randint(15,70)
print("r_name = %s " %r_name)
print("r_age = %d " %r_age)
zs_user.insert_one({"name":r_name,"age":r_age,"add_time":datetime.now()})
tt.insert_one({"name":r_name})
i = i + 1
time.sleep(0.1)
print(i)
insert()