简体   繁体   English

如何将mysql数据库文件连接到rails应用程序上的本地ruby

[英]How do I connect a mysql database file to a local ruby on rails application

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. 我有一个数据库文件(name.sql)发送给我,我应该连接到我的Mac本地托管的rails应用程序,我从github下载(分叉?)。 How do I set up my database.yml file to connect with the sql files. 如何设置我的database.yml文件以连接sql文件。

You can't connect a Rails application directly to a SQL file. 您无法将Rails应用程序直接连接到SQL文件。 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. Rails应用程序从数据库服务器获取其数据,并将SQL文件的内容导入到服务器托管的数据库中。

You can download a DMG archive which will install MySQL Community Server on your Mac from http://dev.mysql.com/downloads/mysql/#downloads 您可以从http://dev.mysql.com/downloads/mysql/#downloads下载将在Mac上安装MySQL社区服务器的DMG存档。

That download also includes a handy Preference Pane for starting and stopping the server. 该下载还包括一个方便的Preference窗格,用于启动和停止服务器。

Once you have MySQL up and running then you should set a password for the root user (ie the database system administrator) using 启动并运行MySQL后,您应该使用root用户(即数据库系统管理员)设置密码

mysqladmin -u root password "secret"

—Obviously replace secret with the real password you want to use. - 显然用你想要使用的真实密码替换secret

Then you can set up the database.yml file for the Rails application. 然后,您可以为Rails应用程序设置database.yml文件。 For an application named app it would look like this: 对于名为app的应用程序,它看起来像这样:

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

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应用程序创建一个单独的有限权限数据库用户帐户以连接到MySQL,但是对于本地计算机上的开发,root帐户很好。

After this step you can run rake db:create from the root of the Rails application within the Terminal. 完成此步骤后,您可以从终端内的Rails应用程序的根目录运行rake db:create This command will create the app_development database in MySQL ( rake db:create:all creates the test and production databases too). 此命令将在MySQL中创建app_development数据库( rake db:create:all创建测试和生产数据库)。 Finally, you can import your SQL file by entering the following command in the Terminal: 最后,您可以通过在终端中输入以下命令来导入SQL文件:

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

You will be prompted for the MySQL root password. 系统将提示您输入MySQL root密码。 Replace path/to/file with the full path to the SQL file if it's not within the Terminal's current directory. path/to/file替换为SQL文件的完整路径,如果它不在Terminal的当前目录中。 For example, use ~/Desktop/name.sql if it's on your desktop. 例如,如果~/Desktop/name.sql ,请使用它。

Probably easiest: you need to run a database server on your mac. 可能最简单:您需要在Mac上运行数据库服务器。 Then import your data into your database server. 然后将数据导入数据库服务器。

Tutorials on installing rails on a mac will also talk about how to install the local database server and setting up the database.yml file 在mac上安装rails的教程还将讨论如何安装本地数据库服务器和设置database.yml文件

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

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