ookamiinc / twitter-analytics-acquirer

0 stars 0 forks source link

HerokuにてDB(mysql2)を作成。実際にアカウントで動かす。 #6

Closed yusuko closed 6 years ago

yusuko commented 6 years ago

What

masterブランチをHerokuにpush。 DBを作成して、実際に運用する。

Why

Herokuにて正常にシステムを運用するため。

Summary

yusuko commented 6 years ago

To do

yusuko commented 6 years ago

Heroku config

CLEARDB_DATABASE_URL:     mysql://....
DATABASE_URL:             mysql2://....

ClEARDB__DATABASE_URLを変更する必要があるのかの確認をする。

yusuko commented 6 years ago

sudo mysql --defaults-extra-file=./my.conf

でDB自体には接続可能。

yusuko commented 6 years ago

How to use mysql

Command Line

bundle install ・・・ install 'mysql2' and 'activerecord'

sudo mysql ・・・ log in to MySQL monitor

After login to MySQL monitor

CREATE DATABASE twitter_analytics_acquirer; ・・・create DB

USE twitter_analytics_acquirer; ・・・ select DB

CREATE TABLE twitter_analytics_acquirer.twitter_accounts
(id int not null auto_increment primary key,
 cookies text,
 name text not null,
 password text not null,
 worksheet_name text not null,
 created_at datetime  default current_timestamp,
 updated_at timestamp default current_timestamp on update current_timestamp);

show columns from twitter_accounts; ・・・ see table

MySQL setup is finished.

Add data

Notice: You should change PASSWORD into the real password.

INSERT INTO  twitter_analytics_acquirer.twitter_accounts (name, password, worksheet_name)
 VALUES ("Playerapp_vb",PASSWORD,"player");

・・・ Add data

SELECT * FROM twitter_accounts; ・・・ Check data

You can also create data using Active record.

TwitterAccount.create
  (name: YOUR_TWITTER_ID,
   password: YOUR_TWITTER_PASSWORD,
   worksheet_name: YOUR_WORKSHEET_NAME)

Spread sheet

https://docs.google.com/spreadsheets/d/1bt_D2fNj9jlsNgP41GjsfJ2OjPkqDk1EtTRDTv7R1Us/edit#gid=1754749292

yusuko commented 6 years ago

Access denied for user 'bb30d5d2687002'@'%' to database 'twitter_analytics_acquirer'

ユーザーの権限の問題でDBが作成できない。そのため、ユーザーに権限を渡す方法を調べる。 https://teratail.com/questions/45324

現在の問題点

yusuko commented 6 years ago

やったこと

mysql > CREATE TABLE twitter_analytics_acquirer; --> Access denied for user 'bb30d5d2687002'@'%'to database 'twitter_analytics_acquirer'

yusuko commented 6 years ago

2018-09-27 21 16 17

DB作成成功

yusuko commented 6 years ago
~ $ bundle exec ruby script.rb
/app/twitter_analytics_client.rb:56:in `initialize': Permission denied (Errno::EACCES)
    from /app/twitter_analytics_client.rb:56:in `new'
    from /app/twitter_analytics_client.rb:56:in `cookies_to_yaml_string'
    from /app/twitter_analytics_client.rb:62:in `save_cookies'
    from /app/twitter_analytics_client.rb:27:in `get_analytics_data_with_login'
    from script.rb:12:in `block in <main>'
    from script.rb:9:in `each'
    from script.rb:9:in `<main>'
  def cookies_to_yaml_string
    cookies_io_write = StringIO.new('', 'r+')
    @agent.cookie_jar.save(cookies_io_write, session: true)
    cookies_io_write.string
  end

1行目でエラーが発生。。。 なぜだ。調査必要

https://docs.ruby-lang.org/ja/latest/class/Errno=3a=3aEXXX.html Rubyのライブラリ内部でシステムコールや一部のC言語関数が失敗したときに発生します。

herokuのbashで gem update を実行。

下記のエラーが発生したが、それ以外は正常実行。 が、いつまでたっても処理が終わらないので、途中でinterrputed.

Fetching: bigdecimal-1.3.5.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing bigdecimal:
    ERROR: Failed to build gem native extension.

    current directory: /app/vendor/ruby-2.4.4/lib/ruby/gems/2.4.0/gems/bigdecimal-1.3.5/ext/bigdecimal
/app/vendor/ruby-2.4.4/bin/ruby -r ./siteconf20180928-5-aqn90a.rb extconf.rb
checking for labs() in stdlib.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/app/vendor/ruby-2.4.4/bin/$(RUBY_BASE_NAME)
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:457:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:542:in `try_link0'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:557:in `try_link'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:768:in `try_func'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:1055:in `block in have_func'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:945:in `block in checking_for'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:351:in `block (2 levels) in postpone'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:321:in `open'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:351:in `block in postpone'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:321:in `open'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:347:in `postpone'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:944:in `checking_for'
    from /app/vendor/ruby-2.4.4/lib/ruby/2.4.0/mkmf.rb:1054:in `have_func'
    from extconf.rb:20:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /app/vendor/ruby-2.4.4/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0/bigdecimal-1.3.5/mkmf.log

extconf failed, exit code 1
yusuko commented 6 years ago

@nafu @ladnack

Do you know something about this error? https://github.com/ookamiinc/twitter-analytics-acquirer/issues/6#issuecomment-425074771 Permission denied (Errno::EACCES)

Fact

Please tell me if you have any ideas.

nafu commented 6 years ago

Did you configure the authentication for MySQL correctly? e.x. host, username, password

yusuko commented 6 years ago

Yes. I checked it twice...

yusuko commented 6 years ago

I try to create another puroduction if you don't know about it..

nafu commented 6 years ago

I try to create another puroduction if you don't know about it..

[Q] What do you mean?

yusuko commented 6 years ago

[A] もう一つherokuのproductionを作って、再push再設定すれば、上記のエラーが起きずに実行できるのかなと思っているので、やり直しも視野に入れています。。。

nafu commented 6 years ago
  • I can operate this system successfully in the localhost
  • I've already checked the environmental variables and database data
  • I face this error for the first time. And I assume this error is about heroku server.

OK, then what should you next to solve this problems? You should know how to think and debug for solving these kind of problems. The way of thinking can apply for everything so that your basic skill is going to be great.

[Q] If heroku is the cause of this error, how could you make sure that?

nafu commented 6 years ago

[A] もう一つherokuで、productionを作って再設定し直せば、上記のエラーが起きずに実行できるのかなと思っているので、やり直しも視野に入れています。。。

OK, if that is the only solution for this problems you should do that 👍

nafu commented 6 years ago

[A] もう一つherokuで、productionを作って再設定し直せば、上記のエラーが起きずに実行できるのかなと思っているので、やり直しも視野に入れています。。。

But you should think what you can solve by doing this 😇 What is the root problems 🤔

yusuko commented 6 years ago

You should know how to think and debug for solving these kind of problems. The way of thinking can apply for everything so that your basic skill is going to be great.

Thank you. I hold on!

[Q] If heroku is the cause of this error, how could you make sure that?

I am sure the cause is heroku by confirming that I can operate this system in local .

yusuko commented 6 years ago

It may be that gem version is the cause... Because this error is caused by gem in my search...

https://qiita.com/cutenkey/items/2505710b4c494bac152b https://mimikun.hatenablog.jp/entry/2018/03/22/221950

So I try to run bundle update in the local and then push it heroku.

yusuko commented 6 years ago

もう一つのproductionにpushしたところ同様のエラーが。。。

しっかり調べます。。。

yusuko commented 6 years ago
  def cookies_to_yaml_string
    cookies_io_write = StringIO.new('', 'r+')

  def cookies_to_yaml_string
    cookies_io_write = StringIO.new(+'', 'r+')

に変えた結果、エラーは発生しなくなった。。。 詳しい原因はわかっていないので、調べる。

yusuko commented 6 years ago

Rubocopによって、frozen_literal:trueとなり、自動的にstringがfreezeされていた。 そのため、errorがでた。

https://docs.ruby-lang.org/ja/latest/method/StringIO/s/new.html

[EXCEPTION] Errno::EACCES: string がフリーズされていて、mode が書き込み可能に設定されている場合に発生します。

よって、+を加えることでstringをunfrozenなものにした。

yusuko commented 6 years ago

そもそも、localでもしっかりと動いていなかったのに、動いていると勘違いしていた。(DBにすでにcookieが保持されている状態で動かしていた。)

また、今回は結局人(teratail)に聞いてしまったので、自分で解決できるように問題解決能力を高める。

debugは冷静にやる。

yusuko commented 6 years ago

Summary