macos 如何初始化 MySQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7300653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I initialize a MySQL database?
提问by Matt Bettinson
I've tried the commands in terminal. MySQL is installed, but I don't know how to initialize a database for the rails app I'm working on
我已经尝试过终端中的命令。MySQL 已安装,但我不知道如何为我正在开发的 Rails 应用程序初始化数据库
回答by Max
I'm assuming you are using Linux. If you are not, please say so. I have only followed these steps on Ubuntu, and I know for a fact that the process of setting up MySQL on OS X is much more difficult. As an aside, please provide more detail next time.
我假设您使用的是 Linux。如果你不是,请说出来。我只在 Ubuntu 上遵循了这些步骤,我知道在 OS X 上设置 MySQL 的过程要困难得多。顺便说一句,下次请提供更多详细信息。
First, you must install MySQL. You need to install both MySQL, as well as the Ruby connector. Rails might ship with a Ruby connector for MySQL. I don't believe installing this one has any negative effects though. To install MySQL and the Ruby connector, execute the following commands in a terminal window:
首先,您必须安装 MySQL。您需要安装 MySQL 和 Ruby 连接器。Rails 可能附带用于 MySQL 的 Ruby 连接器。我不相信安装这个有任何负面影响。要安装 MySQL 和 Ruby 连接器,请在终端窗口中执行以下命令:
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
During this process, you should be asked to set a root password. This will come in handy later. The next step is to add the mysql gem to your dependency list. To do this, add the following line to your Gemfile:
在此过程中,您应该被要求设置 root 密码。这将在以后派上用场。下一步是将 mysql gem 添加到您的依赖项列表中。为此,请将以下行添加到您的 Gemfile 中:
gem 'mysql'
宝石'mysql'
And then run
然后运行
bundle install
捆绑安装
To install the mysql gem. Next, you must change your database adapter to mysql. You can do this by opening config/database.yml and under each environment there should be a line listing adapter: XXXX, which you should change to
安装 mysql gem。接下来,您必须将数据库适配器更改为 mysql。您可以通过打开 config/database.yml 来完成此操作,在每个环境下都应该有一行列出适配器:XXXX,您应该将其更改为
adapter: mysql
适配器:mysql
You also need to configure your database access here. Here is a sample:
您还需要在此处配置您的数据库访问权限。这是一个示例:
development:
adapter: mysql
database: YourApp_development
username: root
password: root_password
host: localhost
pool: 5
timeout: 5000
Make sure to replace root_password with the password you configured mysql to use. You can also create other MySQL users to use, but that is outside the scope of this question. Now, make sure that MySQL server is running by typing the following in a terminal window:
确保将 root_password 替换为您配置 mysql 使用的密码。您还可以创建其他 MySQL 用户来使用,但这超出了本问题的范围。现在,通过在终端窗口中键入以下内容来确保 MySQL 服务器正在运行:
/etc/init.d/mysql start
/etc/init.d/mysql 启动
and then run rake db:create
然后运行 rake db:create
Hope that helps.
希望有帮助。
回答by Smar
Assuming you are in development environment, otherwise you need to add environment variable with value RAILS_ENV=production
.
假设您在开发环境中,否则您需要添加值为 value 的环境变量RAILS_ENV=production
。
rake db:create # creates the databases (if privileged)
rake db:migrate # runs migrations from db/migrate