简体   繁体   English

如何与不同计算机/远处的数据库进行交互? (红宝石)

[英]How does interacting with databases on different computers/far away work? (Ruby)

So, I'm fairly new to dealing with databases, and it makes sense to me when the database is on the local machine. 所以,我对处理数据库相当新,当数据库在本地机器上时,对我来说很有意义。 But, how would I deal with a database that is far away/in a different computer? 但是,我如何处理远离/不同计算机的数据库? How is the connection set-up? 如何建立连接? How would I be able to tell Ruby to go toy with that database? 我怎么能告诉Ruby玩这个数据库玩具? I think SQLite is required to be on the local machine, but what about PostgreSQL or MySQL? 我认为SQLite需要在本地机器上,但是PostgreSQL或MySQL呢? I'm positive large projects require this sort of set-up with databases somewhere else and whatnot. 我很积极的大项目需要在其他地方和其他地方建立数据库。

Also, this means teams should be able to all interact with the same database, correct? 此外,这意味着团队应该能够与同一个数据库进行交互,对吗?

I've tried finding articles and reading about it, but I can't seem to find any information about this. 我试过找文章并阅读它,但我似乎无法找到任何有关此事的信息。

In ruby on rails, we have a config/database.yml file where we can do database connectivity. 在rails上的ruby中,我们有一个config/database.yml文件,我们可以在其中进行数据库连接。 To connect to the remote system's database do: 要连接到远程系统的数据库,请执行以下操作:

1 - Give permission to your system to access the database of remote system 1 - 授予您的系统访问远程系统数据库的权限

Grant all on databasename.* to username@ipaddress of your system identified by password 将所有数据库名。*授予您的系统的用户名@ ipaddress,用密码标识

2 - Update the database.yml file 2 - 更新database.yml文件

development: 
 adapter:   mysql  
 database:  databasename  
 username:  username  
 password:  password  
 host:      ip of remote system

Configuring database.yml for your rails app 为rails应用程序配置database.yml

development:
  adapter: mysql
  database: development_database
  username: root
  password: [password]
  host: localhost
test:
  adapter: mysql
  database: test_database
  username: root
  password: [password]
  host: localhost
production:
  adapter: mysql
  database: production_database
  username: root
  password: [password]
  host: localhost

Don't forget that these databases are not just files that the local program accesses -- they are servers in their own right, and the local program submits requests (select, insert etc) to them for the database server to process and return a result. 不要忘记这些数据库不仅仅是本地程序访问的文件 - 它们本身就是服务器,本地程序向它们提交请求(选择,插入等),以便数据库服务器处理并返回结果。

This also explains why multiple teams can access the same database -- the database server processes are just communicating with multiple programs at the same time (and the resolution of which program sees which data when they are all accessing and changing the same tables is one of the reasons why databases are so complex). 这也解释了为什么多个团队可以访问同一个数据库 - 数据库服务器进程只是同时与多个程序进行通信(以及哪个程序在访问和更改相同表时查看哪些数据的解析是其中之一数据库如此复杂的原因)。

So the location of the database is only relevant in that it can take longer to send requests to, and retrieve results from, it over the network. 因此,数据库的位置只是相关的,因为它可能需要更长的时间来通过网络向它发送请求并从中检索结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM