MySql Connect Lost in Crystal Language with Kemal - mysql

I try connection Kemal Framework with crystal-mysql.
This my shard:
mysql:
github: crystal-lang/crystal-mysql
db:
github: crystal-lang/crystal-db
And this my connection:
db = DB.open "mysql://root:password#localhost:787878/book"
But, if I'm running crystal src/book.cr I have this error:
Unhandled exception: (DB::ConnectionLost)
from lib/mysql/src/mysql/read_packet.cr:16:7 in 'initialize'
from lib/mysql/src/mysql/read_packet.cr:6:3 in 'new'
from lib/mysql/src/mysql/connection.cr:77:5 in 'build_read_packet'
from lib/mysql/src/mysql/connection.cr:59:5 in 'read_packet'
from lib/mysql/src/mysql/connection.cr:22:19 in 'initialize'
from lib/mysql/src/mysql/connection.cr:4:3 in 'new'
from lib/mysql/src/mysql/driver.cr:3:5 in 'build_connection'
from lib/db/src/db/database.cr:51:9 in '->'
from lib/db/src/db/pool.cr:255:3 in 'build_resource'
from lib/db/src/db/pool.cr:17:34 in 'initialize'
from lib/db/src/db/pool.cr:15:5 in 'new:initial_pool_size:max_pool_size:max_idle_pool_size:checkout_timeout:retry_attempts:retry_delay'
from lib/db/src/db/database.cr:50:7 in 'initialize'
from lib/db/src/db/database.cr:43:5 in 'new'
from lib/db/src/db.cr:139:5 in 'build_database'
from lib/db/src/db.cr:135:5 in 'build_database'
from lib/db/src/db.cr:103:5 in 'open'
from src/news_monitoring.cr:13:1 in '__crystal_main'
from /usr/share/crystal/src/crystal/main.cr:97:5 in 'main_user_code'
from /usr/share/crystal/src/crystal/main.cr:86:7 in 'main'
from /usr/share/crystal/src/crystal/main.cr:106:3 in 'main'
from __libc_start_main
from _start
from ???
How I can fix it.

Related

Ruby: Catching Sequel::DatabaseConnectionError

I'm using Docker to run my Ruby app, with MySQL as a database. I need for my Ruby app to wait until MySQL has finished loading and a connection can be made.
I'm using the following code:
def connect_to_db
begin
puts "Trying to connect to Mysql"
Sequel::Model.db = Sequel.connect( // Connection stuff in here )
rescue Sequel::Error => e
puts "Mysql connection failed #{e.message}: Retrying."
retry
end
end
connect_to_db()
This runs once, then I get an error - Sequel::DatabaseConnectionError: Mysql2::Error: Unknown MySQL server host (25) - It doesn't go into the rescue block and doesn't retry.
I've tried rescue Sequel::DatabaseConnectionError but this gives the same result.
What do I need to rescue here?
Got it working with db.test_connection:
def connect_to_db_with_mysql_driver
begin
puts "Trying to connect to Mysql"
db = Sequel.connect(
// Connection stuff
)
if db.test_connection == true
Sequel::Model.db = db
else
raise "Mysql connection failed."
end
rescue => ex
puts ex
puts "Retrying..."
retry
end
end

mysql dbi connection: works in irb, not in script?

I am trying to use the rubygem DBI to connect to MYSQL on Windows 7; I can connect using irb but not from within a ruby script, which has stumped me for a few days now...
Here is my gem env output:
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.28
- RUBY VERSION: 1.9.3 (2014-02-24 patchlevel 545) [i386-mingw32]
- INSTALLATION DIRECTORY: C:/Ruby193/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: C:/Ruby193/bin/ruby.exe
- EXECUTABLE DIRECTORY: C:/Ruby193/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-mingw32
- GEM PATHS:
- C:/Ruby193/lib/ruby/gems/1.9.1
- C:/Users/rick/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Here is my irb connection:
irb(main):001:0> require 'dbi'
=> true
irb(main):004:0> dbh=DBI.connect("DBI:Mysql:host=127.0.0.1;port=3306", "rick",>
=> #<DBI::DatabaseHandle:0x2c30bb0 #handle=#<DBI::DBD::Mysql::Database:0x2c30dd8
#handle=#<Mysql:0x2c30f28>, #attr={"AutoCommit"=>true}, #have_transactions=true
, #mutex=#<Mutex:0x2c30bf8>>, #trace_output=nil, #trace_mode=nil, #convert_types
=true, #driver_name="Mysql">
irb(main):005:0> row = dbh.select_one("SELECT VERSION()")
=> ["5.5.25a"]
irb(main):006:0> puts row
5.5.25a
=> nil
irb(main):007:0>
And here is my script:
require 'DBI'
require 'mysql'
# connect to the MySQL server
begin
dbh = DBI.connect("DBI:Mysql:wikifracdb:127.0.0.1;port=3306", "rick", "rick")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
But when I run my script, I get:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:93: warning: already in
itialized constant VERSION
C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:96: warning: already in
itialized constant API_VERSION
C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:100: warning: already i
nitialized constant DEFAULT_TRACE_MODE
C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:101: warning: already i
nitialized constant DEFAULT_TRACE_OUTPUT
An error occurred
Error code: 2003
Error message: Can't connect to MySQL server on 'localhost' (10061)
I have Windows Firewall turned off.
Any suggestions will be much appreciated...this has been driving me crazy!

OpenShift domain status failing

So I created an account at open shift, created an app, and installed the command line tool. when I do the command rhc domain status it fails:
Loaded suite /usr/bin/rhc-chk
Started
.E
===============================================================================
Error: test_connectivity(Test1_Connectivity)
ArgumentError: too few arguments
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:204:in `sprintf'
201: message = sprintf(get_message(:errors,name),*(args.shift || ''))
202: solution = get_message(:solutions,name)
203: if solution
=> 204: message << "\n" << sprintf(solution,*(args.shift || ''))
205: end
206: message
207: end
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:204:in `error_for'
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:270:in `test_connectivity'
===============================================================================
F
===============================================================================
Failure:
You need to be able to connect to the server in order to test authentication.
<false> is not true.
test_authentication(Test2_Authentication)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:280:in `test_authentication'
277: # Checking Authentication
278: #
279: def test_authentication
=> 280: assert $connectivity, error_for(:cant_connect)
281:
282: data = {'rhlogin' => $rhlogin}
283: response = fetch_url_json("/broker/userinfo", data)
===============================================================================
..F
===============================================================================
Failure: You must have an account on the server in order to test: whether you have a valid key loaded in your agent.
test_03_remote_ssh_keys(Test3_SSH)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:317:in `require_login'
314: end
315:
316: def require_login(test)
=> 317: flunk(error_for(:no_account,test)) if $user_info.nil?
318: end
319:
320: def require_remote_keys(test)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:321:in `require_remote_keys'
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:376:in `test_03_remote_ssh_keys'
===============================================================================
F
===============================================================================
Failure: You must have an account on the server in order to test: connecting to your applications.
test_04_ssh_connect(Test3_SSH)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:317:in `require_login'
314: end
315:
316: def require_login(test)
=> 317: flunk(error_for(:no_account,test)) if $user_info.nil?
318: end
319:
320: def require_remote_keys(test)
/Library/Ruby/Gems/1.8/gems/rhc-0.94.8/bin/rhc-chk:383:in `test_04_ssh_connect'
===============================================================================
Finished in 2.403595 seconds.
7 tests, 8 assertions, 3 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
42.8571% passed
Not really understanding why it's not able to connect. I was able to use: rhc domain show, with no problems.
Anyone have any suggestions on how to fix this?
It's a bug that should get fixed in the upcoming release. Even though you see this error it shouldn't affect any other behaviour.

Trouble connecting to MySQL Zend Framework

I'm trying to set up an application using the Zend Framework but the problem i'm getting is as soon as i add the following line in the application.ini the default home page that is created by the zend tool throws a fatal error
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with
message 'Configuration array must have a key for 'password' for login
credentials' in C:\xampp\php\PEAR\Zend\Db\Adapter\Abstract.php:295
Stack trace: #0 C:\xampp\php\PEAR\Zend\Db\Adapter\Abstract.php(183):
Zend_Db_Adapter_Abstract->_checkRequiredOptions(Array) #1
C:\xampp\php\PEAR\Zend\Db.php(270):
Zend_Db_Adapter_Abstract->__construct(Array) #2
C:\xampp\php\PEAR\Zend\Application\Resource\Db.php(142):
Zend_Db::factory('PDO_MYSQL', Array) #3
C:\xampp\php\PEAR\Zend\Application\Resource\Db.php(154):
Zend_Application_Resource_Db->getDbAdapter() #4
C:\xampp\php\PEAR\Zend\Application\Bootstrap\BootstrapAbstract.php(683):
Zend_Application_Resource_Db->init() #5
C:\xampp\php\PEAR\Zend\Application\Bootstrap\BootstrapAbstract.php(626):
Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('db')
6 C:\xampp\php\PEAR\Zend\Application\Bootstrap\BootstrapAbstract.php(586):
Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(NULL) #7
C:\xampp\php\PEAR\Zend\Ap in
C:\xampp\php\PEAR\Zend\Db\Adapter\Abstract.php on line 295
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.dbname = codenamesnm
i'm using xampp with windows 7 Any idea what is wrong
Do not omit these lines:
resources.db.params.username = rob
resources.db.params.password = 123456
set them to "root" or "" if necessary, but let them into your application.ini.

'PDOException' with message 'SQLSTATE[HY000] [2002] zend framework

Any one plz tell me why i am getting this error ?? firstly my internet connection was good today its not good so i am getting this error.how can i fix this any idea plz??
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] A
connection attempt failed because the connected party did not properly respond after a
period of time, or established connection failed because connected host has failed to
respond. ' in D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Db\Adapter\Pdo\Abstract.php:129
Stack trace: #0 D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Db\Adapter\Pdo\Abstract.php(129):
PDO->__construct('mysql:host=192....', 'root', 'root', Array) #1 D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Db\Adapter\Pdo\Mysql.php(109):
Zend_Db_Adapter_Pdo_Abstract->_connect() #2 D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Db\Adapter\Abstract.php(860):
Zend_Db_Adapter_Pdo_Mysql->_connect() #3 D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Db\Adapter\Abstract.php(930):
Zend_Db_Adapter_Abstract->quote('testing', NULL) #4 D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Auth\Adapter\DbTable.php(449): Zen
in D:\SVN
data\WebClient_PHP\trunk\p\library\Zend\Db\Adapter\Pdo\Abstract.php on line
144
this is my config.ini
[general]
db.adapter = PDO_MYSQL
db.params.host = 192.168.0.233
db.params.username = root
db.params.password = my_password
db.params.dbname = "mydatabasename"
some code from my index.php
$config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini', 'general');
$DB = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($DB);
it seems that your code this portion is not working
[general]
db.adapter = PDO_MYSQL
db.params.host = 192.168.0.233
db.params.username = root
db.params.password = my_password
db.params.dbname = "mydatabasename"
either the IP is not correct if so go to cmd in windows and type ipconfig you will get your correct ip paste it here OR just simply write
db.params.host = localhost
if and only if you are using locally may be it works.
for the hostname/host try to use your credentials found in the Zend Server -> PHP Cloud tab -> Overview page -> User Parameters tab... then make the "host" part in the credentials like this:
[general]
db.adapter = PDO_MYSQL
db.params.host = CONTAINERNAME-db.my.phpcloud.com
db.params.username = ZEND_DB_USERNAME
db.params.password = ZEND_DB_USERNAME
db.params.dbname = ZEND_DB_DBNAME
Did you check your mysql database is up?
If you are sure it is, check that your adapters configuration is ok, maybe the host parameter is not properly configured.