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

时间:2022-06-20 01:44:40

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.

我有一个数据库文件(name.sql),它被发送给我,我应该连接到我的mac上的一个rails应用程序,我从github上下载了(forked?)如何设置数据库。连接到sql文件的yml文件。

2 个解决方案

#1


5  

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.

不能将Rails应用程序直接连接到SQL文件。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/#下载一个DMG存档,它将在您的Mac上安装MySQL社区服务器

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

该下载还包括一个用于启动和停止服务器的方便的首选项窗格。

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

一旦安装并运行了MySQL,就应该为使用它的根用户(即数据库系统管理员)设置密码

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. For an application named app it would look like this:

然后可以设置数据库。用于Rails应用程序的yml文件。对于一个名为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连接,但是对于本地机器上的开发,根帐户是可以的。

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:

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

mysql -u root -p app_development < path/to/file/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根密码。如果SQL文件不在终端的当前目录中,则将路径/to/file替换为SQL文件的完整路径。例如,使用~ /桌面/名称。如果它在你的桌面上。

#2


0  

Probably easiest: you need to run a database server on your mac. Then import your data into your database server.

可能最简单:需要在mac上运行数据库服务器,然后将数据导入数据库服务器。

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的教程还将讨论如何安装本地数据库服务器和设置数据库。yml文件

#1


5  

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.

不能将Rails应用程序直接连接到SQL文件。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/#下载一个DMG存档,它将在您的Mac上安装MySQL社区服务器

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

该下载还包括一个用于启动和停止服务器的方便的首选项窗格。

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

一旦安装并运行了MySQL,就应该为使用它的根用户(即数据库系统管理员)设置密码

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. For an application named app it would look like this:

然后可以设置数据库。用于Rails应用程序的yml文件。对于一个名为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连接,但是对于本地机器上的开发,根帐户是可以的。

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:

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

mysql -u root -p app_development < path/to/file/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根密码。如果SQL文件不在终端的当前目录中,则将路径/to/file替换为SQL文件的完整路径。例如,使用~ /桌面/名称。如果它在你的桌面上。

#2


0  

Probably easiest: you need to run a database server on your mac. Then import your data into your database server.

可能最简单:需要在mac上运行数据库服务器,然后将数据导入数据库服务器。

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的教程还将讨论如何安装本地数据库服务器和设置数据库。yml文件