Ruby Install Mysql Gem Windows

This quickstart demonstrates how to connect to an Azure Database for MySQL using a application and the gem from Windows, Ubuntu Linux, and Mac platforms. It shows how to use SQL statements to query, insert, update, and delete data in the database. This topic assumes that you are familiar with development using Ruby and that you are new to working with Azure Database for MySQL. Prerequisites This quickstart uses the resources created in either of these guides as a starting point:. Install Ruby Install Ruby, Gem, and the MySQL2 library on your own computer. Windows.

Installing MYSQL2 Ruby on Rails gem on a Windows environment can be difficult, typically showing the infamous “ERROR: Failed To Build Native Extension” error: If you’re seeing this error, the good news is it’s actually quite simple to resolve. It’s caused because you don’t have the MYSQL C-connector library installed. Installing the libmysqlclient-dev gives you the necessary files to compile the mysql2 gem which is what Rails will use to connect to MySQL when you setup your Rails app. When you're finished, you can skip to the Final Steps.

Download and Install the 2.3 version of. Launch a new command prompt (cmd) from the Start menu.

Install

Change directory into the Ruby directory for version 2.3. Cd c: Ruby23-x64 bin. Test the Ruby installation by running the command ruby -v to see the version installed. Test the Gem installation by running the command gem -v to see the version installed. Build the Mysql2 module for Ruby using Gem by running the command gem install mysql2. MacOS. Install Ruby using Homebrew by running the command brew install ruby.

For more installation options, see the Ruby. Test the Ruby installation by running the command ruby -v to see the version installed. Test the Gem installation by running the command gem -v to see the version installed.

Build the Mysql2 module for Ruby using Gem by running the command gem install mysql2. Linux (Ubuntu). Install Ruby by running the command sudo apt-get install ruby-full. For more installation options, see the Ruby. Test the Ruby installation by running the command ruby -v to see the version installed. Install the latest updates for Gem by running the command sudo gem update -system.

Ruby Install Mysql Gem Windows

Test the Gem installation by running the command gem -v to see the version installed. Install the gcc, make, and other build tools by running the command sudo apt-get install build-essential. Install the MySQL client developer libraries by running the command sudo apt-get install libmysqlclient-dev. Build the mysql2 module for Ruby using Gem by running the command sudo gem install mysql2. Get connection information Get the connection information needed to connect to the Azure Database for MySQL.

You need the fully qualified server name and login credentials. Log in to the. From the left-hand menu in Azure portal, click All resources, and then search for the server you have creased (such as myserver4demo). Click the server name myserver4demo. Select the server's Properties page, and then make a note of the Server name and Server admin login name. If you forget your server login information, navigate to the Overview page to view the Server admin login name, and if necessary reset the password.

Run Ruby code. Paste the Ruby code from the sections below into text files, and then save the files into a project folder with file extension.rb (such as C: rubymysql createtable.rb or /home/username/rubymysql/createtable.rb). To run the code, launch the command prompt or Bash shell. Change directory into your project folder cd rubymysql. Then type the Ruby command followed by the file name, such as ruby createtable.rb to run the application. On the Windows OS, if the Ruby application is not in your path environment variable, you may need to use the full path to launch the node application, such as 'c: Ruby23-x64 bin ruby.exe' createtable.rb Connect and create a table Use the following code to connect and create a table by using CREATE TABLE SQL statement, followed by INSERT INTO SQL statements to add rows into the table.

The code uses a class.new method to connect to Azure Database for MySQL. Then it calls method several times to run the DROP, CREATE TABLE, and INSERT INTO commands.

Then it calls method to close the connection before terminating. Replace the host, database, username, and password strings with your own values.

Require 'mysql2' begin # Initialize connection variables. Host = String('myserver4demo.mysql.database.azure.com') database = String('quickstartdb') username = String('myadmin@myserver4demo') password = String('yourpassword') # Initialize connection object. Client = Mysql2::Client.new(:host = host,:username = username,:database = database,:password = password) puts 'Successfully created connection to database.' # Drop previous table of same name if one exists client.query('DROP TABLE IF EXISTS inventory;') puts 'Finished dropping table (if existed).' # Drop previous table of same name if one exists.

Client.query('CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);') puts 'Finished creating table.' # Insert some data into table. Client.query('INSERT INTO inventory VALUES(1, 'banana', 150)') client.query('INSERT INTO inventory VALUES(2, 'orange', 154)') client.query('INSERT INTO inventory VALUES(3, 'apple', 100)') puts 'Inserted 3 rows of data.' # Error handling rescue Exception = e puts e.message # Cleanup ensure client.close if client puts 'Done.' End Read data Use the following code to connect and read the data by using a SELECT SQL statement. The code uses a class.new method to connect to Azure Database for MySQL.

Then it calls method to run the SELECT commands. Then it calls method to close the connection before terminating. Replace the host, database, username, and password strings with your own values. Require 'mysql2' begin # Initialize connection variables.

Host = String('myserver4demo.mysql.database.azure.com') database = String('quickstartdb') username = String('myadmin@myserver4demo') password = String('yourpassword') # Initialize connection object. Client = Mysql2::Client.new(:host = host,:username = username,:database = database,:password = password) puts 'Successfully created connection to database.'

# Read data resultSet = client.query('SELECT. from inventory;') resultSet.each do row puts 'Data row = (%s,%s,%s)'% row'id', row'name', row'quantity' end puts 'Read ' + resultSet.count.tos + ' row(s).' # Error handling rescue Exception = e puts e.message # Cleanup ensure client.close if client puts 'Done.' End Update data Use the following code to connect and update the data by using an UPDATE SQL statement. The code uses a class.new method to connect to Azure Database for MySQL. Then it calls method to run the UPDATE commands. Then it calls method to close the connection before terminating.

Replace the host, database, username, and password strings with your own values. Require 'mysql2' begin # Initialize connection variables. Host = String('myserver4demo.mysql.database.azure.com') database = String('quickstartdb') username = String('myadmin@myserver4demo') password = String('yourpassword') # Initialize connection object. Client = Mysql2::Client.new(:host = host,:username = username,:database = database,:password = password) puts 'Successfully created connection to database.' # Update data client.query('UPDATE inventory SET quantity =%d WHERE name =%s;'% 200, ' 'banana ') puts 'Updated 1 row of data.' # Error handling rescue Exception = e puts e.message # Cleanup ensure client.close if client puts 'Done.'

Ruby Install Mysql Gem Windows 8

Version

End Delete data Use the following code to connect and read the data by using a DELETE SQL statement. The code uses a class.new method to connect to Azure Database for MySQL.

Then it calls method to run the DELETE commands. Then it calls method to close the connection before terminating. Replace the host, database, username, and password strings with your own values. Require 'mysql2' begin # Initialize connection variables. Host = String('myserver4demo.mysql.database.azure.com') database = String('quickstartdb') username = String('myadmin@myserver4demo') password = String('yourpassword') # Initialize connection object. Client = Mysql2::Client.new(:host = host,:username = username,:database = database,:password = password) puts 'Successfully created connection to database.'

# Delete data resultSet = client.query('DELETE FROM inventory WHERE name =%s;'% ' 'orange ') puts 'Deleted 1 row.' # Error handling rescue Exception = e puts e.message # Cleanup ensure client.close if client puts 'Done.' End Next steps.

Posted on