Connecting Excel to a remote MySQL server 2013-07-15 Emin Gabrielyan 1. Table of contents 1. Table of contents ............................................................................................................................. 1 2. Introduction ..................................................................................................................................... 1 3. Result ............................................................................................................................................... 1 4. Connection properties..................................................................................................................... 3 5. Linking an area in the Excel with the remote MySQL table ............................................................ 5 6. Establishing a connection ................................................................................................................ 8 7. Configuring the ODBC.................................................................................................................... 18 8. Installation of the ODBC connector............................................................................................... 33 9. Conclusion ..................................................................................................................................... 54 10. Copyright © 2013 Switzernet .................................................................................................... 54 2. Introduction This document describes the connection of an Excel file with a remote MySQL server. The document is organized in the inverse order. I start first by the demonstration of the result then I go backward by showing how the connection is configured and installed. 3. Result Here is an Excel file where an area starting from the cell $J$4 is connected to a remote MySQL server. Table of contents | printed on 2016-03-23 23:15 | Page 1 of 54 Here is a session of a mysql connection to the server, where I change a value in the table: $ mysql -h'37.187.52.44' -u'emin' -p Enter password: mysql> use test Database changed mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 201 | | 11 | 21 | +------+------+ 2 rows in set (0.03 sec) mysql> update t set y=202 where x=10; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 202 | | 11 | 21 | +------+------+ 2 rows in set (0.04 sec) mysql> After a minute the Excel file updates its values: Result | printed on 2016-03-23 23:15 | Page 2 of 54 4. Connection properties In the data / connections menu of the Excel application you have the connection corresponding to the link to the table on the remote server Connection properties | printed on 2016-03-23 23:15 | Page 3 of 54 In the properties pane you have to set the maximal refresh frequency and check the box for refreshing the file when opening the file. Connection properties | printed on 2016-03-23 23:15 | Page 4 of 54 5. Linking an area in the Excel with the remote MySQL table You have to select a cell in your Excel sheet, for Example C5 Linking an area in the Excel with the remote MySQL table | printed on 2016-03-23 23:15 | Page 5 of 54 Then in Data / Existing Connections pane, you have to choose the connection which was already defined (see the following sections for the definition of the connection). Linking an area in the Excel with the remote MySQL table | printed on 2016-03-23 23:15 | Page 6 of 54 For our case the name of the connection is “test t” Click on Open and you will have the following confirmation box Click on OK, and your Excel file will be updated with the values of the table taken from the remote server: Linking an area in the Excel with the remote MySQL table | printed on 2016-03-23 23:15 | Page 7 of 54 6. Establishing a connection Open the Data / Connections tab which shall look as follows. You normally will have no connection yet listed in the window. Click on Add and you will have the following window Establishing a connection | printed on 2016-03-23 23:15 | Page 8 of 54 Browse for more connections by clicking on [Browse for More…]. You will have another window that looks as follows: Establishing a connection | printed on 2016-03-23 23:15 | Page 9 of 54 Here you must click on New Source… and you will have this: Select ODBC DSN Establishing a connection | printed on 2016-03-23 23:15 | Page 10 of 54 Click on Next and you will see in the list the ODBC driver that you have installed for the MySQL connection. For installation of the ODBC driver refer to the following section. As described in the introduction this document is organized in the reverse chronological order. Click on Next, and you will see the table which is behind the Database Connection Establishing a connection | printed on 2016-03-23 23:15 | Page 11 of 54 Leave the data as suggested in the next window Click on Finish Establishing a connection | printed on 2016-03-23 23:15 | Page 12 of 54 I got the above message, as the connection was already created. I am clicking on Yes to replace the file. Now on the list of connections I have a new one, called “test t2”. It corresponds to the link to the exactly the same remote table. Go into its properties Establishing a connection | printed on 2016-03-23 23:15 | Page 13 of 54 And change the refresh frequency and force the refresh when opening the file. Establishing a connection | printed on 2016-03-23 23:15 | Page 14 of 54 Close the connection pane Now use the Data / existing connection button to use the new link “test t2” Establishing a connection | printed on 2016-03-23 23:15 | Page 15 of 54 And to sync the remote table values with the area of your Excel sheet starting from the O4 corner Click on OK, and you will have the values of the remote mysql server in your Excel sheet Establishing a connection | printed on 2016-03-23 23:15 | Page 16 of 54 You can now connect to the mysql server with the command line prompt, and change a value in the table. mysql> update t set y=215 where x=10; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 215 | | 11 | 21 | +------+------+ 2 rows in set (0.04 sec) mysql> The Excel file will update its values soon Establishing a connection | printed on 2016-03-23 23:15 | Page 17 of 54 Note that an early linked table is not updated yet. The reason is that the auto-refresh time of its connection (to the same table) is different. 7. Configuring the ODBC Type ODBC in the Windows’ Start menu and select the Data Sources (ODBC) application Configuring the ODBC | printed on 2016-03-23 23:15 | Page 18 of 54 In the User DSN pane add a new name. The ODBC entries added in the User DSN are limited by the current Windows User only (System DSN covers all users on the computer, and the File DSN creates a file that can be ported to another computer). In the example shown below you see already mysql103. This is the one created by me before. We will now create a new one. Configuring the ODBC | printed on 2016-03-23 23:15 | Page 19 of 54 Click on Add button. You will see the MySQL ODBC 5.2 drivers. These drivers were installed previously. Refer to the next section for the instructions on how-to install the MySQL ODBC driver. Let us use the Unicode version Click on Finish and you will have the following form to fulfill Configuring the ODBC | printed on 2016-03-23 23:15 | Page 20 of 54 We give a new connection name “mysql104” now. Configuring the ODBC | printed on 2016-03-23 23:15 | Page 21 of 54 As soon as you fulfill the correct information, including the password, the drop-down menu of databases allows you to select the database on the remote server. It means that the entered connection parameters are valid. In our example we select the database “test” Configuring the ODBC | printed on 2016-03-23 23:15 | Page 22 of 54 Click also on the Test button to check the connection. Now a new User DSN is created Configuring the ODBC | printed on 2016-03-23 23:15 | Page 23 of 54 In an Excel file, we can see the new DSN. The detailed sequence of screenshots was already shown previously. For a reminder, to reach the following window you have to follow in the Excel file the following path [Data] / [Connections] / [Add…] / [Browse for More…] / [New Source…] / [ODBC DSN] / [Next]. Select now the new data source “mysql104” Configuring the ODBC | printed on 2016-03-23 23:15 | Page 24 of 54 Click on next Give a different ODC file name Configuring the ODBC | printed on 2016-03-23 23:15 | Page 25 of 54 And finish. The first line corresponds to the new connection. We remove all other connections to not create a mess Configuring the ODBC | printed on 2016-03-23 23:15 | Page 26 of 54 And we make sure that this is the new connection Configuring the ODBC | printed on 2016-03-23 23:15 | Page 27 of 54 The properties need an update of the refresh time Configuring the ODBC | printed on 2016-03-23 23:15 | Page 28 of 54 The pane looks as follows after the update Configuring the ODBC | printed on 2016-03-23 23:15 | Page 29 of 54 Now we can use the existing connections to link an area with the remote table Configuring the ODBC | printed on 2016-03-23 23:15 | Page 30 of 54 Confirming the location in the Excel sheet where to we wish to link the remote table. And here is the result Configuring the ODBC | printed on 2016-03-23 23:15 | Page 31 of 54 And after a little update on the server side: mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 2001 | | 11 | 21 | +------+------+ 2 rows in set (0.04 sec) mysql> update t set y=3001 where x=10; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 3001 | | 11 | 21 | +------+------+ 2 rows in set (0.04 sec) mysql> We have the Excel sheet updated as well Configuring the ODBC | printed on 2016-03-23 23:15 | Page 32 of 54 8. Installation of the ODBC connector The driver is installed from here http://dev.mysql.com/downloads/connector/odbc/ The 32 and 64 bit drivers are installed due to an error that occurred when attempting to use them the first time Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 33 of 54 Now we will uninstall the 32 bit version to make sure that it’s the 64 bit version which is working The DSN still show the two names Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 34 of 54 If removing is successful It means that the right driver is operational Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 35 of 54 Finally we can open the Excel file Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 36 of 54 And change the value on the server mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 3001 | | 11 | 21 | +------+------+ 2 rows in set (0.03 sec) mysql> update t set y=4001 where x=10; Query OK, 1 row affected (0.09 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 4001 | | 11 | 21 | +------+------+ 2 rows in set (0.03 sec) mysql> Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 37 of 54 then we wait until the values are updated on the Excel And as the values are not updated, with the following error, therefore we need also the 32 bit version Installing the 32 bit version Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 38 of 54 Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 39 of 54 Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 40 of 54 Removing the previous DSN Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 41 of 54 Removing the 64 bit version of the MySQL Connector/ODBC We are left with the 32 bit version only Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 42 of 54 Creating a new ODBC DSN with the 32 bit version of the connector Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 43 of 54 But we face the following problem Reinstalling again the 64 bit version Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 44 of 54 Attempting to add a new data source This time it works Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 45 of 54 Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 46 of 54 Adding the link in Excel Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 47 of 54 Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 48 of 54 Overwriting Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 49 of 54 Can remove from the folder of links the old files Only the current file is kept Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 50 of 54 Changing the properties to refresh faster Inserting an existing connection Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 51 of 54 And the result is correct Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 52 of 54 Checking whether it is ok after the update on the database side, as if you remember the lack of the 32 bit version caused an error message at the automatic refresh time. mysql> mysql> mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 4001 | | 11 | 21 | +------+------+ 2 rows in set (0.04 sec) mysql> update t set y=5001 where x=10; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t; +------+------+ | x | y | +------+------+ | 10 | 5001 | | 11 | 21 | +------+------+ 2 rows in set (0.03 sec) mysql> Installation of the ODBC connector | printed on 2016-03-23 23:15 | Page 53 of 54 No error this time 9. Conclusion Both 32bit and 64bit drivers of MySQL ODBC are needed to be installed for the proper operation. Copyright © 2013 Switzernet END of the file Conclusion | printed on 2016-03-23 23:15 | Page 54 of 54