Rails가 DB에 연결할 수 없을 때의 사고 방식
13048 단어 RailshomebrewPostgreSQLCoderDojo디버깅
각자의 환경이나 상황에 따라 취해야 할 액션은 다르다고 생각합니다만, 나는 「가능한 한 공식 정보를 쫓아 해결한다」라고 하는 방법을 채택하는 것이 많습니다. 여러가지 방법이 있어 좋을까라고 생각합니다만, 그 1개의 방법으로서 참고가 되면 다행입니다 (>사람< )✨
「뭔가 에러가 나왔다!」로부터의 원인 조사 → 대책 예
이번에는 제가 실제로 만난 예를 바탕으로 에러가 발생하는 곳에서 원인을 하나씩 조사해 나가고, 최종적으로 대처하는 곳까지의 흐름을 차례로 정리해 나갑니다. 덧붙여서 소재로 하는 Rails 어플리케이션은 이쪽.
CoderDojo Japan - 아이들을위한 프로그래밍 도장
h tps : // 여기에서 r. jp/
GitHub - coderdojo-japan/coderdojo.jp (OSS)
htps : // 기주 b. 코 m/코데 r 도조쟈판/코데 r 도조. jp
우선은 에러를 만나는 곳으로부터.
오류와의 만남
╭─○ yasulab ‹2.5.3› ~/coderdojo.jp
╰─○ rails s
WARNING: Nokogiri was built against LibXML version 2.9.7, but has dynamically loaded 2.9.4
=> Booting WEBrick
=> Rails 5.1.6.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[2018-12-27 11:55:12] INFO WEBrick 1.4.2
[2018-12-27 11:55:12] INFO ruby 2.5.3 (2018-10-18) [x86_64-darwin17]
[2018-12-27 11:55:12] INFO WEBrick::HTTPServer#start: pid=12570 port=3000
Started GET "/" for ::1 at 2018-12-27 11:55:15 +0900
PG::ConnectionBad (could not connect to server: No such file or directory # <= コレっぽい 👀
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
):
오류 메시지를 보면 PG (PostgreSQL)가 원인임을 알 수 있습니다. 단지 위의 정보만으로 데이터베이스에 연결할 수 없을 때까지는 알 수 없으므로 먼저 PostgreSQL이 원래 작동하는지 확인하십시오.
Postgre SQL의 현재 상태 확인
╭─○ yasulab ‹2.5.3› ~/coderdojo.jp
╰─○ pg_ctl -D /usr/local/var/postgres start # パスは環境によって違う
waiting for server to start....2018-12-28 19:53:07.847 EST [5097] FATAL: database files are incompatible with server
2018-12-28 19:53:07.847 EST [5097] DETAIL: The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.1. # <= コレが原因っぽい 👀
stopped waiting
pg_ctl: could not start server
Examine the log output.
상태를 조사해 보면, 이번의 경우는 「DB 의 버젼이 다르면(자) 잘 동작하지 않게 된다 (incompatible 가 된다)로부터」라고 하는 것을 알았습니다. 과연! 그러고 보니 최근
brew update
이나 brew upgrade
를 한 기억이 있으므로 아마 그것이 요인일 것입니다 버전을 올릴 때 Homebrew를 사용하고 있었기 때문에 Homebrew를 통해 공식 정보에 액세스해 보면 뭔가 좋은 정보를 찾을 수 있습니다 (공식 정보에는 좋은 느낌의 간단한 답변이 실리는 경우가 많은 인상입니다 ) 🤔💭
Homebrew를 통해 공식 정보에 액세스하는 방법
╭─○ yasulab ‹2.5.3› ~/coderdojo.jp
╰─○ brew info postgresql
postgresql: stable 11.1 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
Conflicts with:
postgres-xc (because postgresql and postgres-xc install the same binaries.)
/usr/local/Cellar/postgresql/10.4 (3,389 files, 39.2MB)
Poured from bottle on 2018-07-25 at 23:24:07
/usr/local/Cellar/postgresql/11.1 (3,548 files, 40.3MB) *
Poured from bottle on 2018-12-23 at 12:30:47
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/postgresql.rb
==> Dependencies
Build: pkg-config ✔
Required: icu4c ✔, openssl ✔, readline ✔
Optional: python ✘
==> Options
--with-python
Enable PL/Python3
--HEAD
Install HEAD version
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database # <= いい感じの回答を見つけた! 👀✨
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
==> Analytics
install: 88,258 (30 days), 185,674 (90 days), 701,483 (365 days)
install_on_request: 75,475 (30 days), 163,418 (90 days), 594,559 (365 days)
build_error: 0 (30 days)
좋은 느낌의 대답이 발견되었습니다!
NOTE: 공식 정보를 따라 대응한다면 고치는 것이 거의 있을까 생각합니다만, 보다 복잡한 환경이라면 원인이 하나만이 아닐지도 모릅니다. 그 경우도 위와 같이 하나씩 공식 정보를 보면서 진행하면 좋을 것 같습니다.
공식 정보에 따라 대응
╭─○ yasulab ‹2.5.3› ~/coderdojo.jp
╰─○ brew postgresql-upgrade-database
==> brew install postgresql@10
==> Downloading https://homebrew.bintray.com/bottles/[email protected]
######################################################################## 100.0%
==> Pouring [email protected]
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
postgresql@10 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have postgresql@10 first in your PATH run:
echo 'export PATH="/usr/local/opt/postgresql@10/bin:$PATH"' >> ~/.zshrc
For compilers to find postgresql@10 you may need to set:
export LDFLAGS="-L/usr/local/opt/postgresql@10/lib"
export CPPFLAGS="-I/usr/local/opt/postgresql@10/include"
For pkg-config to find postgresql@10 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/postgresql@10/lib/pkgconfig"
To have launchd start postgresql@10 now and restart at login:
brew services start postgresql@10
Or, if you don't want/need a background service you can just run:
pg_ctl -D /usr/local/var/postgres@10 start
==> Summary
🍺 /usr/local/Cellar/postgresql@10/10.6: 1,706 files, 20.8MB
==> Upgrading postgresql data from 10 to 11...
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Moving postgresql data from /usr/local/var/postgres to /usr/local/var/postgres.old...
The files belonging to this database system will be owned by user "yasulab".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
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:
/usr/local/opt/postgresql/bin/pg_ctl -D /usr/local/var/postgres -l logfile start
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Creating dump of global objects ok
Creating dump of database schemas
ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.
Performing Upgrade
------------------
Analyzing all rows in the new cluster ok
Freezing all rows in the new cluster ok
Deleting files from new pg_xact ok
Copying old pg_xact to new server ok
Setting next transaction ID and epoch for new cluster ok
Deleting files from new pg_multixact/offsets ok
Copying old pg_multixact/offsets to new server ok
Deleting files from new pg_multixact/members ok
Copying old pg_multixact/members to new server ok
Setting next multixact ID and offset for new cluster ok
Resetting WAL archives ok
Setting frozenxid and minmxid counters in new cluster ok
Restoring global objects in the new cluster ok
Restoring database schemas in the new cluster
ok
Copying user relation files
ok
Setting next OID for new cluster ok
Sync data directory to disk ok
Creating script to analyze new cluster ok
Creating script to delete old cluster ok
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
./analyze_new_cluster.sh
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
==> Upgraded postgresql data from 10 to 11!
==> Your postgresql 10 data remains at /usr/local/var/postgres.old
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
다시 Rails 서버를 다시 시작하면 이번에는 잘 작동했습니다 ;)
어땠나요?
Reference
이 문제에 관하여(Rails가 DB에 연결할 수 없을 때의 사고 방식), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/yasulab/items/ed387a3c54e8d9c6a4b5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)