替换数据库

2021-06-08  本文已影响0人  RealAnalysis

看到资料表明可能是数据库问题 我就查了如何正确从sqlite3 切换成Postgres

找了一篇参考资料

https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25

其中不熟悉的一个步骤是把 config/database.yml里的内容从


# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

替换成

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: MyTrip_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: MyTrip_test

production:
  <<: *default
  database: MyTrip_production
  username: deploy
  password:

注解掉的说明文字内容还是sqlite3的相关文字没改 先这样吧

以及查资料到后面发现 username: deploy用替换 貌似不是 deploy

因为要改好几处而且看起来string更合理 所以先跳过 报错了 但我不死心

在改 t.string over to t.text前 我想先尝试rake三兄弟

够狠的!三兄弟都执行不了...强啊

第一个文档改动存照 方便改回去

第二个文档改了

t.string :title

第三个文档

t.string  :address
t.string  :price
t.string  :best_time

改了居然还是报错 果然问题出在数据库设置啊啊啊!

尝试解决数据库问题过程中运行了

brew upgrade

看到一些提示 记录以备后用

==> Pouring libxml2--2.9.12.big_sur.bottle.tar.gz
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

隔一段居然还有报错

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink lib/libImath.dylib
Target /usr/local/lib/libImath.dylib
is a symlink belonging to ilmbase. You can unlink it:
  brew unlink ilmbase

To force the link and overwrite all conflicting files:
  brew link --overwrite imath

To list all files that would be deleted:
  brew link --overwrite --dry-run imath

Possible conflicting files are:
/usr/local/lib/libImath.dylib -> /usr/local/Cellar/ilmbase/2.5.5_1/lib/libImath.dylib

以及

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/OpenEXR/Iex.h
Target /usr/local/include/OpenEXR/Iex.h
is a symlink belonging to ilmbase. You can unlink it:
  brew unlink ilmbase

To force the link and overwrite all conflicting files:
  brew link --overwrite openexr

To list all files that would be deleted:
  brew link --overwrite --dry-run openexr

Possible conflicting files are:
一堆的文档...就没有复制...

我估计是系统老是自己升级导致的...不要乱升级系统真是老师当时的良心建议啊...

然后发现自己两个数据库都安装了 果然...

==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/13/app-initdb.html

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

最后还有这个眼熟的朋友

==> redis
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

一通报错折腾之后 找到个能运行起来的解法 https://stackoverflow.com/questions/27700596/homebrew-postgres-broken

rm -rf /usr/local/var/postgres  
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
initdb -D /usr/local/var/postgres

记录有用的信息内容 注意其中我的 user不是云服务器上的那个

The files belonging to this database system will be owned by user "RealAnalysis".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  C
  CTYPE:    UTF-8
  MESSAGES: C
  MONETARY: C
  NUMERIC:  C
  TIME:     C
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start

注意到user的不同 所以先跑去 config/database.yml把数据库的 user改了

然后就能运行

rake db:create

psql -l

就可以查看数据库情况 比如 owner具体为何

虽然过程没办法完全顺利的按照 https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25 走下来 但是貌似也算顺利替换成pg数据库了?

直接用PG数据库 不用换

真是良心建议 建立以后会部署的项目时 直接执行

rails new my_app_name --database=postgresql

记得my_app_name替换成你的新项目名称哈 这样直接用 pg 数据库就不会有部署的时候要换数据库的吐血了

最后 给你解释一下全程疑惑的一点

解惑一下

还有一篇不错的参考

https://dev.to/forksofpower/make-the-move-from-sqlite3-to-postgres-in-rails-6-34m2

里面有解释为啥换数据库的时候 建议把 t.string 改成t.text

以及查到一个看着挺靠谱的 database.yml 模板当参考

default: &default
  adapter: postgresql
  encoding: unicode
  password: xxxxx #<-- which you have defiend while installing postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: project_development

test:
  <<: *default
  database: project_test

production:
  <<: *default
  database: project_production
  username: username (我不确定要不要改 是否要跟云端的一致)
  password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>

又一堆连环报错 一个个修过去 然后执行了

bundle install --path vendor/bundle

后 提示记录一下 说不定后面可以用

==== Release notes for capistrano-passenger ====
passenger once had only one way to restart: `touch tmp/restart.txt`
Beginning with passenger v4.0.33, a new way was introduced: `passenger-config restart-app`

The new way to restart was not initially practical for everyone,
since for versions of passenger prior to v5.0.10,
it required your deployment user to have sudo access for some server configurations.

capistrano-passenger gives you the flexibility to choose your restart approach, or to rely on reasonable defaults.

If you want to restart using `touch tmp/restart.txt`, add this to your config/deploy.rb:

    set :passenger_restart_with_touch, true

If you want to restart using `passenger-config restart-app`, add this to your config/deploy.rb:

    set :passenger_restart_with_touch, false # Note that `nil` is NOT the same as `false` here

If you don't set `:passenger_restart_with_touch`, capistrano-passenger will check what version of passenger you are running
and use `passenger-config restart-app` if it is available in that version.

If you are running passenger in standalone mode, it is possible for you to put passenger in your
Gemfile and rely on capistrano-bundler to install it with the rest of your bundle.
If you are installing passenger during your deployment AND you want to restart using `passenger-config restart-app`,
you need to set `:passenger_in_gemfile` to `true` in your `config/deploy.rb`.

好奇心起来 执行了一下

 rake db:migrate RAILS_ENV=production

报错提示为

Caused by:
PG::ConnectionBad: FATAL:  database "MyTrip_production" does not exist

解法

rake db:create:all

就能补上创建不存在的 database "MyTrip_production"

然后就能顺利的跑

 rake db:migrate RAILS_ENV=production

还找到个官网的参考资料 如何设置 config/database.yml

https://guides.rubyonrails.org/configuring.html#configuring-a-postgresql-database

上一篇下一篇

猜你喜欢

热点阅读