We had a chance to install MongoDB on one of our Cpanel-based servers. So, we just wanted to write down how to install, create and import MongoDB. Also, we will show you the steps required for the installation and how to create and import MongoDB.
MongoDB is an open-source document database written in C++. Moreover, it provides high performance, high availability, and automatic scaling.
MongoDB was developed by 10gen, a New York-based organization. It initially developed as a PAAS (Platform As A Service). Later in 2009, it was introduced as an Open source database server, maintained and supported by MongoDB Inc.
How to install MongoDB in the Cpanel.
STEP 1. Installing repo [root/]# cd/etc/yum.repos.d [root/]# nano -w mongodb.repo Copy and paste the below given code and save the file. [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc STEP 2. Installing the package MongoDB [root / ]# sudo yum install -y mongodb-org STEP 3. Configure it to automatically start after reboots [root / ]# chkconfig mongod on Start MongoDB: [root / ]# service mongod start Starting mongod: [OK] STEP 4. Installing MongoDB PHP Extension [root / ]# pecl install mongo Let the process complete and at the end you should see the following text. ….. Build complete. Don’t forget to run ‘make test’. running: make INSTALL_ROOT=”/root/tmp/pear-build-rootv6pAQg/install-mong-1.6.13” install Installing shared extensions: /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr/local/lib/php/extensions/no-debug-non-zts-20121212/ running: find “/root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13” | xargs ls -dils 6292373 4 drwxr-xr-x 3 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13 6292524 4 drwxr-xr-x 3 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr 6292525 4 drwxr-xr-x 3 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr/local 6292526 4 drwxr-xr-x 3 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr/local/lib 6292527 4 drwxr-xr-x 3 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo- 1.6.13/usr/local/lib/php 6292528 4 drwxr-xr-x 3 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr/local/lib/php/extensions 6292529 4 drwxr-xr-x 2 root root 4096 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr/local/lib/php/extensions/no-debug-non-zts-20121212 6292523 1836 -rwxr-xr-x 1 root root 1878374 Apr 9 02:58 /root/tmp/pear/pear-build-rootv6pAQg/install-mongo-1.6.13/usr/local/lib/php/extensions/no-debug-non-zts-20121212/mongo.so Build process completed successfully Installing ‘/usr/local/lib/php/extensions/no-debug-non-zts-20121212/mongo.so’ install ok: channel://pecl.php.net/mongo-1.6.13 Extension mongo enabled in php.ini Restart Apache to apply changes: [root / ]# service httpd restart Verify installation with this command: [root / ]# php -i | grep mongodb -i MongoDB Support => enabled MONGODB-CR => enabled MONGODB-X509 => enabled mongo.allow_empty_keys => 0 => 0 mongo.chunk_size => 261120 => 261120 mongo.cmd => $ => $ mongo.default_host => localhost => localhost mongo.default_port => 27017 => 27017 mongo.is_master_interval => 15 => 15 mongo.long_as_object => 0 => 0 mongo.native_long => 1 => 1 mongo.ping_interval => 5 => 5
If you see the MongoDB extension in the output, That’s it you are all set.
How to create MongoDB:
- Run the command: mongosh
- Create DB using the command: use DATABASE_NAME
- Create collections or tables: db.createCollection(“COLLECTION_NAME”)
- List all databases: show dbs
- List all collections in a particular db: show collections
- View data in a collection: db.COLLECTION_NAME.find()
- Create User
Creates a new user for the database on which the method is run. db.createUser() returns a duplicate user error if the user already exists on the database.
db.createUser( { user: “USER_NAME", pwd: “PASSWORD", roles: [ { role: "readWrite", db: "DATABASE_NAME" }, ] } )
8. Returns user information for a specified user: db.getUser()
db.getUser(“USER_NAME”)
9. Drop user: db.dropUser(“USER_NAME”, {w: “majority”, wtimeout: 5000})
10. Returns information for all the users in the database: db.getUsers()
How to import MongoDB:
- Uploads the full db zip file into the ‘dump’ folder in the root
- Unzip the file
- Then run the command: mongorestore –db DATABASE_NAME dump/folder_name
MongoDB is a popular NoSQL database solution that suits modern development requirements. MongoDB is a powerful, flexible, and secure database management system that can be the right choice of a database in certain use cases, but it may not always be the best choice.
If you have any trouble with the database, Let us know! We can help you with the development.