如何启动MongoDB的WEB界面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何启动MongoDB的WEB界面相关的知识,希望对你有一定的参考价值。
让我们构建一个简单的EmployeeManager Web服务,我们将使用它来演示与MongoDB连接的HATEOAS。为了引导我们的应用程序,我们将使用Spring Initializr。我们将使用Spring HATEOAS和Spring Data MongoDB作为依赖项。你应该看到类似下图2所示的内容。 图2 :引导应用程序配置完成后,下载zip并将其作为Maven项目导入你喜欢的IDE中。 首先,让我们配置application.properties。要获得MongoDB连接,spring.data.mongodb.host= //Mongo server host
spring.data.mongodb.port= //Mongo server port
spring.data.mongodb.username= //Login user
spring.data.mongodb.password= //Password
spring.data.mongodb.database= //Database name
一般来说,如果所有内容都是全新安装的,并且你没有更改或修改任何Mongo属性,则只需提供一个数据库名称(已经通过GUI创建了一个数据库名称)。
spring.data.mongodb.database = EmployeeManager
另外,为了启动Mongo实例,作者创建了一个.bat,它指向安装文件夹和数据文件夹。它是这样的:"C:\\Program Files\\MongoDB\\Server\\3.6\\bin\\mongod" --dbpath D:\\Inther\\EmployeeManager\\warehouse-data\\db
现在,我们来快速创建模型。这里有两个模型,员工模型和部门模型。检查它们,确保有没有参数、getter、setter、equals方法和hashCode生成的构造函数。(不用担心,所有代码都在GitHub上,你可以稍后查看它:https://github.com/theFaustus/EmployeeManager。)
public class Employee
private String employeeId;
private String firstName;
private String lastName;
private int age;
public class Department
private String department;
private String name;
private String description;
private List<Employee> employees;
现在我们已经完成了模型的制作,让我们来创建存储库,以便来测试持久性。存储库如下所示:
public interface EmployeeRepository
extends MongoRepository<Employee, String>
public interface DepartmentRepository
extends MongoRepository<Department,String>
如上所示,这里没有方法,因为大家都知道Spring Data中的中心接口被命名为Repository,在其之上是CrudRepository,它提供了处理模型的基本操作。在CrudRepository之上,我们有PagingAndSortingRepository,它为我们提供了一些扩展功能,来简化分页和排序访问。在我们的案例中,最重要的是MongoRepository,它用于严格处理我们的Mongo实例。因此,对于我们的案例来说,除了那些现成的方法外,我们不需要任何其他方法,但是仅出于学习目的,作者在这里要提到的是你可以添加其他查询方法的两种方法:
- “惰性”(查询创建):此策略将尝试通过分析查询方法的名称并推断关键字(例如findByLastnameAndFirstname)来构建查询。编写查询:这里没有什么特别的。例如,只用@Query注释你的方法,然后自己编写查询。你也可以在MongoDB中编写查询。下面是基于JSON的查询方法的示例:
@Query(" 'firstname' : ?0 ")
List<Employee> findByTheEmployeesFirstname(String firstname);
@Bean public CommandLineRunner init(EmployeeRepository employeeRepository, DepartmentRepository departmentRepository)
return (args) ->
employeeRepository.deleteAll();
departmentRepository.deleteAll();
Employee e = employeeRepository.save(new Employee("Ion", "Pascari", 23));
departmentRepository.save(new Department("Service Department", "Service Rocks!", Arrays.asList(e)));
for (Department d : departmentRepository.findAll())
LOGGER.info("Department: " + d);
;
public interface EmployeeService
Employee saveEmployee(Employee e);
Employee findByEmployeeId(String employeeId);
void deleteByEmployeeId(String employeeId);
void updateEmployee(Employee e);
boolean employeeExists(Employee e);
List<Employee> findAll();
void deleteAll();
@Service public class EmployeeServiceImpl implements EmployeeService
@Autowired
private EmployeeRepository employeeRepository;
@Override
public Employee saveEmployee(Employee e)
return employeeRepository.save(e);
@Override
public Employee findByEmployeeId(String employeeId)
return employeeRepository.findOne(employeeId);
@Override
public void deleteByEmployeeId(String employeeId)
employeeRepository.delete(employeeId);
@Override
public void updateEmployee(Employee e)
employeeRepository.save(e);
@Override
public boolean employeeExists(Employee e)
return employeeRepository.exists(Example.of(e));
@Override
public List<Employee> findAll()
return employeeRepository.findAll();
@Override
public void deleteAll()
employeeRepository.deleteAll();
@RestController
@RequestMapping("/employees")
public class EmployeeController
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/list/", method = RequestMethod.GET)
public HttpEntity<List<Employee>> getAllEmployees()
List<Employee> employees = employeeService.findAll();
if (employees.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<>(employees, HttpStatus.OK);
@RequestMapping(value = "/employee/id", method = RequestMethod.GET)
public HttpEntity<Employee> getEmployeeById(@PathVariable("id") String employeeId)
Employee byEmployeeId = employeeService.findByEmployeeId(employeeId);
if (byEmployeeId == null)
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
else
return new ResponseEntity<>(byEmployeeId, HttpStatus.OK);
@RequestMapping(value = "/employee/", method = RequestMethod.POST)
public HttpEntity<?> saveEmployee(@RequestBody Employee e)
if (employeeService.employeeExists(e))
return new ResponseEntity<>(HttpStatus.CONFLICT);
else
Employee employee = employeeService.saveEmployee(e);
URI location = ServletUriComponentsBuilder .fromCurrentRequest().path("/employees/employee/id")
.buildAndExpand(employee.getEmployeeId()).toUri();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(location);
return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
@RequestMapping(value = "/employee/id", method = RequestMethod.PUT)
public HttpEntity<?> updateEmployee(@PathVariable("id") String id, @RequestBody Employee e)
Employee byEmployeeId = employeeService.findByEmployeeId(id);
if(byEmployeeId == null)
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
else
byEmployeeId.setAge(e.getAge());
byEmployeeId.setFirstName(e.getFirstName());
byEmployeeId.setLastName(e.getLastName());
employeeService.updateEmployee(byEmployeeId);
return new ResponseEntity<>(employeeService, HttpStatus.OK);
@RequestMapping(value = "/employee/id", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteEmployee(@PathVariable("id") String employeeId)
employeeService.deleteByEmployeeId(employeeId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
@RequestMapping(value = "/employee/", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteAll()
employeeService.deleteAll();
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
httpinterface=true
mongodb.conf内容如下:
bash-4.2$ cat /etc/mongodb.conf
port=27017 #端口
dbpath= /data/mongodb #数据文件存放目录
logpath= /data/mongodb/logs/mongodb.log #日志文件存放目录
logappend=true #使用追加的方式写日志
fork=true #以守护程序的方式启用,即在后台运行
maxConns=500 #最大同时连接数
noauth=true #不启用验证
journal=true
storageEngine=wiredTiger
httpinterface=true #启动http界面,端口号为28017
手动用命令启动mongodb:
bash-4.2$ mongod --config /etc/mongodb.conf --rest
about to fork child process, waiting until server is ready for connections.
forked process: 4203
child process started successfully, parent exiting
..............................................................................
(注释: --rest代表turn on simple rest api)
查看启动日志:
bash-4.2$ tail -f /data/mongodb/logs/mongodb.log
2016-07-01T17:29:13.766+0800 I CONTROL [initandlisten]
2016-07-01T17:29:13.996+0800 I FTDC [initandlisten] Initializing
full-time diagnostic data capture with directory
'/data/mongodb/diagnostic.data'
2016-07-01T17:29:13.999+0800 I NETWORK [websvr] admin web console waiting for connections on port 28017
2016-07-01T17:29:13.999+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-07-01T17:29:14.023+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2016-07-01T17:29:16.631+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:50609 #1 (1 connection now open)
2016-07-01T17:29:34.679+0800 I NETWORK [websvr] Socket recv() errno:11 Resource temporarily unavailable 192.168.23.1:61067
2016-07-01T17:29:52.455+0800 I NETWORK [websvr] Socket recv() errno:11 Resource temporarily unavailable 192.168.23.1:61076
2016-07-01T17:30:00.455+0800 I NETWORK [websvr] Socket recv() errno:11 Resource temporarily unavailable 192.168.23.1:61077
2016-07-01T17:30:33.228+0800 I NETWORK [conn1] end connection 127.0.0.1:50609 (0 connections now open)
waiting for connections on port 27017字样代表MongoDB已成功启动。
查看本地IP地址:
bash-4.2$ ifconfig -a
eno16777736: flags=4163 mtu 1500
inet 192.168.23.131 netmask 255.255.255.0 broadcast 192.168.23.255
inet6 fe80::20c:29ff:feb2:3e46 prefixlen 64 scopeid 0x20
ether 00:0c:29:b2:3e:46 txqueuelen 1000 (Ethernet)
RX packets 2273 bytes 2027304 (1.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1398 bytes 159433 (155.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
打开本地浏览器,输入以下地址:
http://192.168.23.131:28017/
通过web页面可以看到:
1.当前Mongodb的所有连接
2.各个数据库和Collection的访问统计,包括:Reads, Writes, Queries, GetMores ,Inserts, Updates, Removes
3.写锁的状态
4.以及日志文件的最后几百行(安装的mongodb默认的日志文件位于/data/mongodb/logs/mongod.log)
5.所有的MongoDB命令本回答被提问者采纳 参考技术B 1:log一定要指定一个xxx.log文件(文件不存在也要这么写,会自动创建,写成这样是不可以的--logpath d:\mongodb\logs)
2:serviceName的N字母要大写
注意:这条命令要到MongoDB的bin目录下运行,刚开始的时候,我就直接在D:\下运行,结果服务的可执行目录为【"D:\mongod" --logpath "D:\MongoDB\logs\MongoDB.log" --logappend --dbpath "D:\MongoDB\data" --directoryperdb --service 】,肯定是不对的。
该命令行指定了日志文件:E:\APMServ5.2.6\MongoDb\logs\MongoDB.log,日志是以追加的方式输出的;
数据文件目录:E:\APMServ5.2.6\MongoDb\data,并且参数--directoryperdb说明每个DB都会新建一个目录;
Windows服务的名称:MongoDB;
以上的三个参数都是可以根据自己的情况而定的,呵呵。
最后是安装参数:--install,与之相对的是--remove
启动MongoDB:net start MongoDB
停止MongoDB:net stop MongoDB
删除MongoDB:sc delete MongoDB
或
运行→regedit→注册表编辑器→HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services 下面显示的就是所安装的服务项,删除相应的就好
mongod --logpath E:\APMServ5.2.6\MongoDb\logs\MongoDB.log --logappend --dbpath E:\APMServ5.2.6\MongoDb\data --directoryperdb --serviceName MongoDB --install
以上是关于如何启动MongoDB的WEB界面的主要内容,如果未能解决你的问题,请参考以下文章