rails s でローカルホストを開くとこのエラーが出てしまいます
ジェムファイルの内容を消して
bundle installたり、何種類かコマンドを試したのですがだめでした。
docker-compose run web bundle exec rake db:create
しても同じようなエラーが出ます
Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'db' (115)
dockercompose
1version: '3' 2services: 3 db: 4 image: mysql:5.6 5 environment: 6 MYSQL_ROOT_PASSWORD: example 7 MYSQL_DATABASE: root 8 ports: 9 - "4306:3306" 10 volumes: 11 - mysql-data:/var/lib/mysql 12 web: 13 build: . 14 environment: 15 DATABASE_PASSWORD: example 16 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 17 volumes: 18 - .:/Books 19 ports: 20 - 3000:3000 21 depends_on: 22 - db 23 tty: true 24 stdin_open: true 25volumes: 26 mysql-data:
dockerfile
1FROM ruby:2.6.5 2 3 4RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ 5 && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list 6 7RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs yarn 8 9WORKDIR /Books/ 10COPY Gemfile ./Gemfile 11COPY Gemfile.lock ./Gemfile.lock 12RUN gem install bundler 13RUN bundle install 14COPY . /Books/ 15 16# Add a script to be executed every time the container starts. 17COPY entrypoint.sh /usr/bin/ 18RUN chmod +x /usr/bin/entrypoint.sh 19ENTRYPOINT ["entrypoint.sh"] 20EXPOSE 3000 21 22# Start the main process. 23CMD ["rails", "server", "-b", "0.0.0.0"]
database
1 2default: &default 3 adapter: mysql2 4 encoding: utf8 5 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 6 username: root 7 password: example 8 # socket: /tmp/mysql.sock 9 10 host: db 11 12development: 13 <<: *default 14 database: Books_development 15 16test: 17 <<: *default 18 database: Books_test 19 20production: 21 <<: *default 22 database: Books_production 23 24 username: root 25 password: <%= ENV['DATABASE_PASSWORD'] %> 26 # socket: /var/lib/mysql/mysql.sock
brew install mysql brew reinstall mysql sudo chown -R user /usr/local/var/mysql mysql.server restart Shutting down MySQL .. ERROR! The server quit without updating PID file (/usr/local/var/mysql/fukusyun-2.local.pid). ERROR! Failed to stop running server, so refusing to try to start.
docker-compose down --rmi all --volumes Books % docker-compose build Books % docker-compose run web bundle exec rake db:create Creating books_db_1 ... done Creating books_web_run ... done Can't connect to MySQL server on 'fukusyun-2.local' (22) Couldn't create 'Books_development' database. Please check your configuration. rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'fukusyun-2.local' (22) /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' mysql> show variables like 'hostname'; +---------------+------------------+ | Variable_name | Value | +---------------+------------------+ | hostname | fukusyun-2.local | +---------------+------------------+ 1 row in set (0.00 sec)
修正後エラー Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Couldn't create 'Books_development' database. Please check your configuration. rake aborted! Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
database
1 2default: &default 3 adapter: mysql2 4 encoding: utf8 5 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 6 username: root 7 password: <%= ENV['DATABASE_PASSWORD'] %> 8 # socket: /tmp/mysql.sock 9 10 host: localhost 11 12development: 13 <<: *default 14 database: Books_development 15 16test: 17 <<: *default 18 database: Books_test 19 20production: 21 <<: *default 22 database: Books_production 23 24 username: root 25 password: <%= ENV['DATABASE_PASSWORD'] %> 26 # socket: /var/lib/mysql/mysql.sock
mysqlリスタート後 % docker-compose run web bundle exec rake db:create Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Couldn't create 'Books_development' database. Please check your configuration. rake aborted! Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) ホストを替えた場合 Can't connect to MySQL server on 'fukusyun-2.local' (22) Couldn't create 'Books_development' database. Please check your configuration. rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'fukusyun-2.local' (22)
回答2件
あなたの回答
tips
プレビュー