如何将mysql数据库文件连接到Rails应用程序上的本地ruby [英] How do I connect a mysql database file to a local ruby on rails application

查看:85
本文介绍了如何将mysql数据库文件连接到Rails应用程序上的本地ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库文件(name.sql)发送给我,我应该连接到本地托管在Mac上的Rails应用程序,该应用程序是我从github下载的(分叉的?).如何设置我的database.yml文件以与sql文件连接.

I have a database file (name.sql) that was sent to me that I am supposed to connect to a rails app locally hosted on my mac, that I downloaded (forked?) from github. How do I set up my database.yml file to connect with the sql files.

推荐答案

您不能将Rails应用程序直接连接到SQL文件. Rails应用程序从数据库服务器获取其数据,然后将SQL文件的内容导入到服务器托管的数据库中.

You can't connect a Rails application directly to a SQL file. The Rails application gets its data from a database server and you import the contents of the SQL file into a database hosted by the server.

您可以下载DMG归档文件,该归档文件将从 http: //dev.mysql.com/downloads/mysql/#downloads

You can download a DMG archive which will install MySQL Community Server on your Mac from http://dev.mysql.com/downloads/mysql/#downloads

该下载文件还包含一个方便的首选项窗格",用于启动和停止服务器.

That download also includes a handy Preference Pane for starting and stopping the server.

一旦启动并运行MySQL,则应使用以下方式为root用户(即数据库系统管理员)设置密码:

Once you have MySQL up and running then you should set a password for the root user (i.e. the database system administrator) using

mysqladmin -u root password "secret"

—显然用您要使用的真实密码替换secret.

—Obviously replace secret with the real password you want to use.

然后,您可以为Rails应用程序设置database.yml文件.对于名为 app 的应用程序,它看起来像这样:

Then you can set up the database.yml file for the Rails application. For an application named app it would look like this:

development:
  adapter: mysql
  database: app_development
  username: root
  password: secret
  host: localhost

test:
  adapter: mysql
  database: app_test
  username: root
  password: secret
  host: localhost

production:
  adapter: mysql
  database: app_production
  username: root
  password: secret
  host: localhost

请注意,通常在生产环境中,您将为Rails应用程序创建一个单独的受限特权数据库用户帐户,以用于与MySQL连接,但对于在本地计算机上进行开发,root帐户就可以了.

Note that typically in production you'd create a separate limited privilege database user account for the Rails application to connect to MySQL with, but for development on your local machine the root account is fine.

完成此步骤后,您可以从终端机内的Rails应用程序的根目录运行rake db:create.此命令将在MySQL中创建app_development数据库(rake db:create:all也会创建测试数据库和生产数据库).最后,您可以通过在终端中输入以下命令来导入SQL文件:

After this step you can run rake db:create from the root of the Rails application within the Terminal. This command will create the app_development database in MySQL (rake db:create:all creates the test and production databases too). Finally, you can import your SQL file by entering the following command in the Terminal:

mysql -u root -p app_development < path/to/file/name.sql

系统将提示您输入MySQL根密码.如果path/to/file不在终端的当前目录中,请用SQL文件的完整路径替换.例如,如果~/Desktop/name.sql在您的桌面上,则使用它.

You will be prompted for the MySQL root password. Replace path/to/file with the full path to the SQL file if it's not within the Terminal's current directory. For example, use ~/Desktop/name.sql if it's on your desktop.

这篇关于如何将mysql数据库文件连接到Rails应用程序上的本地ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆