-
Notifications
You must be signed in to change notification settings - Fork 56
Building Python MongoDB Driver
Below versions of Python MongoDB Driver are available in respective distributions at the time of creation of these build instructions:
- Ubuntu 16.04 has
3.2
- Ubuntu (17.10, 18.04) has
3.4.0
The instructions provided below specify the steps to build Python MongoDB Driver (PyMongo) version 3.6.1 on Linux on IBM Z for the following distributions:
- RHEL (6.9, 7.3, 7.4)
- SLES (11 SP4, 12 SP2, 12 SP3)
- Ubuntu (16.04, 17.10, 18.04)
General Notes:
-
When following the steps below please use a standard permission user unless otherwise specified.
-
A directory
/<source_root>/
will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.
-
RHEL 6.9
sudo yum install -y git python-devel python-setuptools python-setuptools-devel python-virtualenv curl gcc python-argparse.noarch
-
RHEL (7.3, 7.4)
sudo yum install -y git openssl openssl-devel pyOpenSSL python-devel python-setuptools python-setuptools-devel python-virtualenv libffi-devel gcc
-
SLES 11 SP4
sudo zypper install -y git curl python-xml python-openssl python-setuptools openssl-devel openssl tar python-devel gcc wget-openssl1 curl-openssl1 libtool make awk automake autoconf
- Build OpenSSL
cd /source_root/ git clone git://github.com/openssl/openssl.git cd openssl git checkout OpenSSL_1_0_2l ./config --prefix=/usr --openssldir=/usr/local/openssl shared make sudo make install
- Build curl
cd /source_root/ git clone git://github.com/curl/curl.git cd curl git checkout curl-7_52_1 ./buildconf ./configure --prefix=/usr/local --with-ssl --disable-shared make && sudo make install export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64 export PATH=/usr/local/bin:$PATH
- Generate ca-bundle.crt for curl
echo insecure >> $HOME/.curlrc wget https://raw.githubusercontent.com/curl/curl/curl-7_53_0/lib/mk-ca-bundle.pl perl mk-ca-bundle.pl -k export SSL_CERT_FILE=`pwd`/ca-bundle.crt rm $HOME/.curlrc
- Set the Environment variables
export LDFLAGS="-L/usr/local/lib/" export LD_LIBRARY_PATH="/usr/local/lib/" export CPPFLAGS="-I/usr/local/include -I/usr/local/include/openssl"
- Build OpenSSL
-
SLES (12 SP2, 12 SP3)
sudo zypper install -y git wget python-xml python-devel python-cffi openssl-devel libffi-devel gcc
-
Ubuntu (16.04, 17.10)
sudo apt-get update sudo apt-get install -y git openssl libssh-dev python python-openssl python-setuptools python-dev gcc
-
Ubuntu 18.04
sudo apt-get update sudo apt-get install -y git openssl libssh-dev python python-openssl python-setuptools python-dev gcc python-pip
-
Install Python 2.7.14 (Only for RHEL 6.9, SLES 11 SP4)
Instructions for building Python can be found here. After installing Python in /usr/local, set the newly installed Python as default.
export PATH=/usr/local/bin:$PATH sudo /usr/sbin/update-alternatives --install /usr/bin/python python /usr/local/bin/python 10 sudo /usr/sbin/update-alternatives --display python python -V
Note: When sudo is used for RHEL 6.9, you may need to use
sudo env PATH=$PATH /<command>/
in order to use higher version Python 2.7.14
- RHEL 6.9
sudo python -m ensurepip
- RHEL (7.3, 7.4) and Ubuntu (16.04, 17.10)
sudo easy_install pip
- SLES 11 SP4
sudo python -m ensurepip
- SLES (12 SP2, 12 SP3)
wget https://bootstrap.pypa.io/ez_setup.py && sudo python ez_setup.py
sudo easy_install pip==1.2.1
cd /<source_root>/
git clone git://github.com/mongodb/mongo-python-driver.git pymongo
cd pymongo
git checkout 3.6.1
sudo python setup.py install
Access to MongoDB Server is required to execute test cases.
- If MongoDB Server is running on same machine then execute the following:
sudo python setup.py test
- If MongoDB Server is running on remote machine, then execute the following:
export DB_IP=<MongoDB_Server_IP>
sudo -E python setup.py test
The example code section given below is used to perform a basic test to ensure that the Python MongoDB Driver is working as expected, and can connect to, modify and query a MongoDB server.
Python MongoDB Driver needs access to a running MongoDB server, either on your local server or a remote system. The following commands are an example of how to start up a MongoDB server and then connect to it with the client shell, but note that MongoDB has not been installed as part of these instructions, and typically you would be running MongoDB on a remote server.
mongod > /tmp/mongodb.log &
mongo --host localhost
Which would typically give a command prompt such as
MongoDB shell version: 3.6.3 connecting to: localhost:27017/test >
The example code below will need to be modified to use your remote server hostname or IP address instead of "localhost", if you are attempting to connect to your own (remote) server.
Create a file named test.py with the content shown below. This code connects to a MongoDB server, inserts some documents and then queries the database to read them back and display them.
Remember, if you are connecting to a remote server then you need to substitute the localhost
with the hostname or IP address of the MongoDB server.
import pprint
import pymongo
from pymongo import MongoClient
server="localhost";
database="ibm_test_db";
collection="mongodb_python_driver";
serverdb="mongodb://" + server + ":27017/";
client = MongoClient(serverdb)
db = client[database];
db[collection].drop();
header = {"company": "IBM",
"project": "MongoDB Driver",
"language": "python",
"version": "3.6.1"};
db[collection].insert_one(header);
for i in range (0, 3):
doc = {"line": i};
db[collection].insert_one (doc);
for gotdoc in db[collection].find():
pprint.pprint(gotdoc);
python test.py
The output from the above should look similar to:
{u'_id': ObjectId('560eb1ff051ba90001d927a0'),
u'company': u'IBM',
u'language': u'python',
u'project': u'MongoDB Driver',
u'version': u'3.6.1'}
{u'_id': ObjectId('560eb1ff051ba90001d927a1'), u'line': 0}
{u'_id': ObjectId('560eb1ff051ba90001d927a2'), u'line': 1}
{u'_id': ObjectId('560eb1ff051ba90001d927a3'), u'line': 2}
The information provided in this article is accurate at the time of writing, but on-going development in the open-source projects involved may make the information incorrect or obsolete. Please open issue or contact us on IBM Z Community if you have any questions or feedback.