NoSQL database case combat--MongoDB database client deployment--php, python

MongoDB database client deployment--php, python

Preface

This environment is based on the Centos 7.8 system to build mongodb-enterprise-4.2.8 learning environment For
specific construction, please refer to mongodb-enterprise-4.2.8 environment construction


php client

One, php client

1. Environment deployment

Install apache, php environment

[root@node01 ~]# yum install httpd php php-pear php-devel -y

Install MongoDB's php extension driver

# 安装c++编译环境
[root@node01 ~]# yum install gcc gcc-c++ make -y
# 安装依赖包
[root@node01 ~]# yum install openssl-devel -y
# 安装扩展
[root@node01 ~]# pecl install mongo

# php.ini文件中添加mongo配置
extension=mongo.so

# 启动httpd服务
[root@node01 ~]# systemctl start httpd
[root@node01 ~]# systemctl is-active httpd
active

2. Actual case

Create collection

[root@node01 ~]# vim /var/www/html/cleart_coll.php
<?php
  $m = new MongoClient(); // 连接
  $db = $m->test; // 获取名称为 "test" 的数据库
  $collection = $db->createCollection("student");
  echo "集合创建成功";
?>

Browser login: http://192.168.5.11/cleart_coll.php to
Insert picture description here
view the creation result and
Insert picture description here
insert the document

[root@node01 ~]# vim /var/www/html/insert.php
<?php
  $m = new MongoClient(); // 连接到mongodb
  $db = $m->test; // 选择一个数据库
  $collection = $db->student; // 选择集合
  $document = array(
    "id" => 201,
    "name" => "张三",
    "sex" => "男",
    "age" => 20
   );
  $collection->insert($document);
echo "数据插入成功";
?>

Browser login: http://192.168.5.11/insert.php to
Insert picture description here
view the insert result
Insert picture description here

Two, python client

1、安装python-redis
[root@localhost ~]# yum install python-redis -y
2、基本操作
[root@localhost ~]# vim python_redis.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#载入模块
import redis
#连接redis数据库
r = redis.Redis(host='127.0.0.1', port=6379,db=0)
#往redis中写数据
r.set('nvshen', 'hehe')
r['diaosi'] = 'yy'
r.set('xueba', 'xuexi')
r['xuezha'] = 'wan'
#查看对应的值
print 'nvshen', r.get('nvshen')
#查看数据库中有多少个key,多少条数据
print r.dbsize()
#将数据保存到硬盘中(保存时阻塞)
r.save()
#查看键值是否存在
print r.exists("doubi")
#列出所有键值
print r.keys()
#删除键值对应的数据
print r.delete('diaosi')
print r.delete('xuezha')
#删除当前数据库所有数据
r.flushdb()
[root@localhost ~]# python python_redis.py
nvshen hehe
4 F
alse
['xuezha', 'xueba', 'diaosi', 'nvshen']
1 1

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/113823143