Hướng dẫn xampp mongodb

Bài viết được sự cho phép của smartjob.vn

Nội dung chính

  • Tập tin info.php
  • Tập tin add.php
  • Tập tin find.php
  • Step 1. Download mongo driver
  • Step 2. Unzip downloaded mongo driver version and copy .dll file to XAMPP
  • Step 3. Register mongo .dll file within php.ini
  • Step 4. Restart XAMPP and refresh phpinfo.php page
  • Step 6. Download and install RoboMongo
  • Conclusion and additional resources
  • Can we use XAMPP for MongoDB?
  • How do I install MongoDB on Windows locally?
  • Can MongoDB be installed on Windows?
  • How use MongoDB with PHP on Windows?

MongoDB hỗ trợ rất nhiều Driver cho các ngôn ngữ lập trình khác nhau. Trong bài viết này, SmartJob trình bày về việc kết nối, truy vấn từ PHP.

Máy tính sử dụng: Windows 10, 64 bit, sử dụng bộ tích hợp XAMPP.
Cài đặt MongoDB sử dụng MEAN stack (bạn xem các bài viết trước đã hướng dẫn cách cài đặt).

Hướng dẫn xampp mongodb

Việc làm php và việc làm mongoBD hấp dẫn

Bước 1. Tải về tập tin mở rộng dll tại đường link:

Bước 2. Giải nén. Copy file php_mongodb.dll  vào thư mục ext, ví dụ trên máy của tác giả là: C:\xampp\php\ext

Bước 3. Tìm file php.ini , ví dụ, trên máy tác giả là: C:\xampp\php\php.ini .

Chèn vào cuối tập tin này:

extension=php_mongodb.dll

Bước 4. Giả sử có thư mục: C:\xampp\htdocs\vy\mongophp
Sử dụng cmd:

Gõ lệnh:

composer require "mongodb/mongodb=^1.0.0"

để tải thư viện bằng Composer về.

Composer tự động tải về và thư mục có thêm 1 thư mục và 2 tập tin mới:

Tạo 3 tập tin info.phpadd.phpfind.php để kiểm tra các tính năng mà thư viện MongoDB PHP cung cấp:

Tập tin info.php

Tập tin add.php

Tập tin find.php

Bước 5. Khởi chạy máy chủ PHP dựng sẵn

php -S localhost:1111 -t C:\xampp\htdocs\vy\mongo-php

Bước 6. Truy cập

http://localhost:1111/info.php

xem kết quả để thấy rằng extension MongoDB đã được cài đặt thành công trong PHP.

Bước 7. Thêm dữ liệu bằng cách truy cập đường dẫn

Bước 8. Tìm kiếm dữ liệu, bằng cách truy cập đường dẫn

Để thực hiện nhiều thao tác đa dạng khác (đọc, ghi, sửa, xóa), bạn hãy xem thêm tài liệu như bên dưới.

TÀI LIỆU THAM KHẢO

1. http://php.net/manual/en/mongodb.installation.windows.php
2. http://mongodb.github.io/mongo-php-library/api/class-MongoDB.Collection.html
3. https://gist.github.com/gatesvp/977676

Tải mã nguồn từ server SmartJob: mongo-php
hoặc tải về/fork từ server Github: https://github.com/SmartJobVN/mongo-php

Bài viết gốc được đăng tải tại smartjob.vn

Có thể bạn quan tâm:

  • Node.js và Mongodb hướng dẫn kết nối
  • MongoDB là gì? Định nghĩa đầy đủ và chi tiết nhất về MongoDB
  • Lập trình viên MongoDB các vị trí

Xem thêm tuyển dụng nhân viên it hấp dẫn trên TopDev

Learn how to install MongoDB on Windows OS and configure with PHP on XAMPP.

Nội dung chính

  • Step 1. Download mongo driver
  • Step 2. Unzip downloaded mongo driver version and copy .dll file to XAMPP
  • Step 3. Register mongo .dll file within php.ini
  • Step 4. Restart XAMPP and refresh phpinfo.php page
  • Step 6. Download and install RoboMongo
  • Conclusion and additional resources
  • Can we use XAMPP for MongoDB?
  • How do I install MongoDB on Windows locally?
  • Can MongoDB be installed on Windows?
  • How use MongoDB with PHP on Windows?

There are a couple of tutorials out there already, but neither of them helped me set everything up 100%. Some of them are missing key explanations, some of them are a bit old. My tutorial presumes you know your way around XAMPP on Windows and you have it installed already.

Here’s how to install MongoDB on your local Windows machine with XAMPP in 5 easy steps:

Step 1. Download mongo driver

To download mongo driver for Windows visit pecl.php.net

Download the latest stable release by clicking on the DLL link. I used the latest stable release at the time, 2017-05-04, version 1.2.9.

On the next page there is a DLL list. It is vital to pick the right version. You need to pick depending on three things:

  • the PHP version that is installed with your XAMPP
  • Architecture x86 or x64
  • Whether Thread Safety is enabled or disabled

Here’s where to find out that information:

  1. Start XAMPP
  2. Navigate to the XAMPP dashboard via Internet browser (tipically at http://localhost)
  3. Click the PHPinfo in the top menu
  4. There, in the new tab you will see the table with the necessary information. At the top, you will see PHP version, in my case it was 7.0.13

Below that, in the table, you will see list of other information. You need to find the value for the Architecture (x86 or x64) and Thread Safety (enabled or disabled).

Finally, you can choose a proper DLL to download.
In my case, I had:

  • PHP version 7.0.13
  • Architecture x86
  • Thread Safety enabled

When Thread Safety is enabled, you need to download Thread Safe (TS) version. So I downloaded file: 7.0 Thread Safe (TS) x86

Step 2. Unzip downloaded mongo driver version and copy .dll file to XAMPP

After successful download, unzip and find the .dll file. Copy it to ext directory of your XAMPP installation. If you installed XAMPP to C drive, full path to the ext folder would be C:\xampp\php\ext

Step 3. Register mongo .dll file within php.ini

Add following line into php.ini file:

extension=php_mongodb.dll

If your .dll file name is different, it’s ok, just make sure you call it like that within php.ini file. For example, if you added a file with a name of php_mongo.dll in the ext folder, you need to put that in the php.ini file.

Step 4. Restart XAMPP and refresh phpinfo.php page

When you restart XAMPP and refresh the phpinfo page, something like this should appear when you CTRL+F and type mongo:

If you don’t see that on your phpinfo page, you did something wrong so try to go back through previous steps.

a) Download latest MongoDB server version.
b) Choose custom setup type and install in the C:\mongodb folder.
c) Then create db folder within data folder on the C drive – C:\data\db
d) Start MongoDB by executing the following command in the CMD: C:\mongodb\bin\mongod.exe

That is it, you successfully installed MongoDB on Windows and configured it in XAMPP!
Just in case you need extra help with step 5, here is the entire process explained with images.

Step 6. Download and install RoboMongo

As an extra step I would recommend installing MongoDB management tool, such as RoboMongo, (it’s called Robo 3T now)  to easily manage MongoDB on your machine. Just create connection and you are set to go.

Conclusion and additional resources

Hope you found this tutorial useful in the process of installing and configuring MongoDB on your Windows machine with XAMPP.

For the next step, I recommend checking out the following MongoDB online courses on Pluralsight:

Introduction to MongoDB

MongoDB Administration

These fantastic courses by Nuri Halperin helped me to learn a lot more about MongoDB.

Can we use XAMPP for MongoDB?

By default, PHP in XAMPP has no mongodb support. So we have to download mongodb extension from PECL (PHP extension repository) and configure it in xampp.

How do I install MongoDB on Windows locally?

Step 1 — Download the MongoDB MSI Installer Package. Head over here and download the current version of MongoDB. ... .

Step 2 — Install MongoDB with the Installation Wizard. A. ... .

Step 3— Create the Data Folders to Store our Databases. A. ... .

Step 4 — Setup Alias Shortcuts for Mongo and Mongod. ... .

Step 5 — Verify That Setup was Successful..

Can MongoDB be installed on Windows?

Starting in version 4.0, you can install and configure MongoDB as a Windows Service during installation. The MongoDB service starts upon successful installation. Configure the MongoDB instance with the configuration file \bin\mongod.

How use MongoDB with PHP on Windows?

Get started with MongoDB and PHP with XAMPP on Windows.

Copy the extracted php_mongodb. dll file into :\xampp\php\ext folder..

Open XAMPP php. ini and add the line: extension=php_mongodb. dll..

Restart the Apache server in XAMPP. You're good to go..