# Newbie: best way to connect to MySQL?

**URL:** <https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612>\
**Category:** Ruby\
**Created:** [March 13, 2007, 12:35am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612 "2007-03-13T00:35:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 12:35am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/1 "2007-03-13T00:35:38Z")

</div>

Hi All,

I’m teaching myself Ruby, and thus far I’ve managed to successfully  
attach to and INSERT / SELECT / etc records from a MSSQL server using  
the ODBC driver. I’ve been trying to do the same thing with a MySQL  
server, using MyODBC set up to connect to the server and database in  
question, but whenever I try to then run code against that server I  
get an error message.

The code I’m using is:

require “odbc”

conn = ODBC::connect(‘MySQLPitOfDespair’)

The error message I’m getting is:

2007-03-13\_ConnectMySQLODBC.rb:3:in `initialize': S1000 (1045) [MySQL] [ODBC 3.51 Driver]Access denied for user 'ODBC'@'localhost' (using password: NO) (ODBC::Error) from 2007-03-13_ConnectMySQLODBC.rb:3:in`connect’  
from 2007-03-13\_ConnectMySQLODBC.rb:3

I’m reasonably certain that I’ve created the ODBC connection  
correctly, since I can link tables from the MySQL server into a  
Microsoft Access database using the ODBC DSN of ‘MySQLPitOfDespair’.

Is there a better way to connect to a MySQL server using Ruby than via  
ODBC? Is there a syntax for supplying user and password settings for  
the ODBC::connect statement above?

Many thanks for any help!

pt

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 1:35am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/2 "2007-03-13T01:35:52Z")

</div>

On Mar 12, 4:33 pm, “planetthoughtful” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there a better way to connect to a MySQL server using Ruby than via  
> ODBC? Is there a syntax for supplying user and password settings for  
> the ODBC::connect statement above?

Yes, use the mysql gem.

install:  
gem install mysql

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 2:45am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/3 "2007-03-13T02:45:56Z")

</div>

“planetthoughtful” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > install:
> 
> - Can I download the gem file?  
> Yes,you can.

> - Where do I get it from, if I can?  
> there are many sites…for eg:  
> [http://rubyforge.vm.bytemark.co.uk/gems/](http://rubyforge.vm.bytemark.co.uk/gems/)

> - Is there any special syntax for installing it from a local file?  
> No,just place the file \*.gem under the current dir,then execute  
> “gem i filename.gem”

> Thanks again!
> 
> pt

–  
[http://kdr2.net](http://kdr2.net)

```
                                           ------yours Killy Draw

```

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 2:00am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/4 "2007-03-13T02:00:53Z")

</div>

On Mar 13, 10:33 am, “kreiggers” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> gem install mysql  
> Hi,

Thanks for your reply. Do you (or does anyone else) know if I can  
download and install the mysql gem locally? I’m behind a corporate  
firewall and it doesn’t seem that the Gem package manager can get out.

So, I guess what I’m asking is:

- Can I download the gem file?
- Where do I get it from, if I can?
- Is there any special syntax for installing it from a local file?

Thanks again!

pt

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 4:36am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/5 "2007-03-13T04:36:07Z")

</div>

On Mar 12, 6:43 pm, KDr2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “planetthoughtful” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > On Mar 13, 10:33 am, “kreiggers” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > On Mar 12, 4:33 pm, “planetthoughtful” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > wrote:
> 
> > > > Is there a better way to connect to a MySQL server using Ruby than via  
> > > > ODBC? Is there a syntax for supplying user and password settings for  
> > > > the ODBC::connect statement above?

Personally, I like using ActiveRecord to connect to databases. For  
basic database manipulation, it’s a lot easier to use than straight up  
SQL, especially if your databases are configured the way AR wants them  
to be. If not, then there’s some annoying setup work with set\_\* and  
joins that might be more hassle than it’s worth, depending on what  
you’re doing.

Download here:  
[http://rubyforge.org/projects/activerecord/](http://rubyforge.org/projects/activerecord/)

Documentation here:  
[http://ar.rubyonrails.org/](http://ar.rubyonrails.org/)

–Alpha Chen

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 6:31am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/6 "2007-03-13T06:31:14Z")

</div>

On Mar 12, 6:43 pm, KDr2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “planetthoughtful” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > On Mar 13, 10:33 am, “kreiggers” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > On Mar 12, 4:33 pm, “planetthoughtful” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > wrote:
> 
> > > > Is there a better way to connect to a MySQL server using Ruby than via  
> > > > ODBC? Is there a syntax for supplying user and password settings for  
> > > > the ODBC::connect statement above?

Personally, I like using ActiveRecord to connect to databases. For  
basic database manipulation, it’s a lot easier to use than straight up  
SQL, especially if your databases are configured the way AR wants them  
to be. If not, then there’s some annoying setup work with set\_\* and  
joins that might be more hassle than it’s worth, depending on what  
you’re doing.

Download here:  
[http://rubyforge.org/projects/activerecord/](http://rubyforge.org/projects/activerecord/)

Documentation here:  
[http://ar.rubyonrails.org/](http://ar.rubyonrails.org/)

–Alpha Chen

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 13, 2007, 5:56am UTC](https://www.ruby-forum.com/t/newbie-best-way-to-connect-to-mysql/88612/7 "2007-03-13T05:56:12Z")

</div>

On Mar 13, 11:43 am, KDr2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > - Where do I get it from, if I can?
> 
> there are many sites…for eg:[http://rubyforge.vm.bytemark.co.uk/gems/](http://rubyforge.vm.bytemark.co.uk/gems/)
> 
> > - Is there any special syntax for installing it from a local file?
> 
> No,just place the file \*.gem under the current dir,then execute  
> “gem i filename.gem”

Hi - just wanted say thank you for your help with this! I’m not sure  
what was wrong with my Google-Fu, but I’d spent a couple of hours  
trying to find somewhere from which I could download gem files  
directly without any luck.

Now happily connecting to MySQL!

Thanks again!

pt
