Memo

メモ > 技術 > プログラミング言語: Ruby > MySQLの導入

■MySQLの導入
■Ruby+MySQLインストール 以下のコマンドでmysql2をインストール
>gem install mysql2 Fetching: mysql2-0.5.2-x64-mingw32.gem (100%) Successfully installed mysql2-0.5.2-x64-mingw32 Parsing documentation for mysql2-0.5.2-x64-mingw32 Installing ri documentation for mysql2-0.5.2-x64-mingw32 Done installing documentation for mysql2 after 2 seconds 1 gem installed
これでMySQLを使える ■Ruby+MySQL動作確認 以下のテーブルとデータがあるとする
CREATE TABLE address( no INT, name VARCHAR(80), tel VARCHAR(80) ) DEFAULT CHARSET=utf8; INSERT INTO address(no, name, tel) VALUES(1, '山田太郎', '090-1234-5678'); INSERT INTO address(no, name, tel) VALUES(2, "山田花子", "090-2345-6789");
以下のプログラムでテーブルの内容を表示できた
#!/ruby/bin/ruby require 'mysql2' print "Content-type: text/html\n\n" print "<html>\n" print "<head>\n" print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" print "<title>Hello world !</title>\n" print "</head>\n" print "<body>\n" print "<pre>\n" print "MySQLのテスト。<br>\n" client = Mysql2::Client.new(:host => 'localhost', :user => 'root', :password => '1234') query = %q{select no, name from test.address} results = client.query(query) results.each do |row| puts "--------------------" row.each do |key, value| puts "#{key} => #{value}\n" end end print "</pre>\n" print "</body>\n" print "</html>\n" exit
参考 RubyでMySQLを操作する - Qiita https://qiita.com/toshiro3/items/b65b2ad744d8f3ecc734 ■Ruby+MySQL(駄目だったときの試行錯誤メモ) http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-6.1.6-win32.zip から libmysql.dll を入手して、C:\ruby\bin\libmysql.dll に配置する これだけで駄目なら、さらに Index of /pub/mysql/Downloads/MySQL-5.5 http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/ から mysql-5.5.51-winx64.zip をダウンロードし、展開して C:\ruby\mysql-5.5.51-winx64 に配置 以下のコマンドでmysql2をインストール
>gem install mysql2 -- '--with-mysql-lib="C:\ruby\mysql-5.5.51-winx64\lib" --with-mysql-include="C:\ruby\mysql-5.5.51-winx64\include"' Temporarily enhancing PATH to include DevKit... Building native extensions with: '--with-mysql-lib="C:\ruby\mysql-5.5.51-winx64\lib" --with-mysql-include="C:\ruby\mysql-5.5.51-winx64\include"' This could take a while... Successfully installed mysql2-0.4.4 Parsing documentation for mysql2-0.4.4 Installing ri documentation for mysql2-0.4.4 Done installing documentation for mysql2 after 1 seconds 1 gem installed
これでMySQLを使えるはず 以前は以下の手順でインストールできたが、今のXAMPPにはlibフォルダ自体が無かった
プログラム内に require 'mysql' と書くと LIBMYSQL.dll が無いというエラーになる C:\xampp\mysql\lib\libmysql.dll を C:\ruby\bin\libmysql.dll にコピー
gemでインストールしようとしてもエラーになった
>gem install mysql2 Fetching: mysql2-0.4.4.gem (100%) Temporarily enhancing PATH to include DevKit... Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. 〜略〜 extconf failed, exit code 1 Gem files will remain installed in C:/ruby/lib/ruby/gems/2.1.0/gems/mysql2-0.4.4 for inspection. Results logged to C:/ruby/lib/ruby/gems/2.1.0/extensions/x64-mingw32/2.1.0/mysql2-0.4.4/gem_make.out
以下のページを参考にインストールできた…が、今は消えている http://halfdome.biz/redmine%E3%82%92windows10%E3%81%AB%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%... キャッシュから探してインストールできた…が、今はキャッシュも消えている http://webcache.googleusercontent.com/search?q=cache:ShD6SA1YPXIJ:halfdome.biz/redmine%25E3%2582%259... さらに別の方法 http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-6.1.6-win32.zip から libmysql.dll を入手して、C:\ruby\bin\libmysql.dll に配置する これだけで駄目なら、さらに Index of /pub/mysql/Downloads/MySQL-5.5 http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/ から mysql-5.5.51-winx64.zip をダウンロードし、展開して C:\ruby\mysql-5.5.51-winx64 に配置 以下のコマンドでmysql2をインストール
>gem install mysql2 -- '--with-mysql-lib="C:\ruby\mysql-5.5.51-winx64\lib" --with-mysql-include="C:\ruby\mysql-5.5.51-winx64\include"' Temporarily enhancing PATH to include DevKit... Building native extensions with: '--with-mysql-lib="C:\ruby\mysql-5.5.51-winx64\lib" --with-mysql-include="C:\ruby\mysql-5.5.51-winx64\include"' This could take a while... Successfully installed mysql2-0.4.4 Parsing documentation for mysql2-0.4.4 Installing ri documentation for mysql2-0.4.4 Done installing documentation for mysql2 after 1 seconds 1 gem installed
これでMySQLを使えるはず

Advertisement