http://www.packtpub.com/article/exporting-data-from-ms-access-2003-to-mysql by Dr.

Jay Krishnaswamy | July 2009 | PHP Most businesses use a software mix in their IT arsenal that makes business sense to them. Because of this, often they have to migrate a part, or whole of their data from one software program to another. In this article by Dr. Jay Krishnaswamy, the built-in method of exporting tables in Microsoft Access is explored to take a table in Microsoft over to MySQL, the open source database product that changed hands recently. This article steps you through the process with a number of screen shots to guide you along the way.

Introduction
It is assumed that you have a working copy of MySQL which you can use to work with this article. The MySQL version used in this article came with the XAMPP download. XAMPP is an easy to install (and use) Apache distribution containing MySQL, PHP, and Perl. The distribution used in this article is XAMPP for Windows. You can find documentation here. Here is a screen shot of the XAMPP control panel where you can turn the services on and off and carry out other administrative tasks.

You need to follow the steps indicated here: 1. Create a database in MySQL to which you will export a table from Microsoft Access 2003 2. Create a ODBC DSN that helps you connecting Microsoft Access to MySQL

3. Export the table or tables 4. Verify the exported items

Creating a database in MySQL
You can create a database in MySQL by using the command 'Create Database' in MySQL or using a suitable graphic user interface such as MySQL workbench. You will have to refer to documentation that works with your version of MySQL. Herein the following version was used. The next listing shows how a database named TestMove was created in MySQL starting from the bin folder of the MySQL program folder. Follow the commands and the response from the computer. The Listing 1 and the folders are appropriate for my computer and you may find it in your installation directory. The databases you will be seeing will be different from what you see here except for those created by the installation.

Listing 1: Login and create a database
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:Documents and SettingsJayaram Krishnaswamy>cd C:>cd xamppmysqlbin C:xamppmysqlbin>mysql -h localhost -u root -p Enter password: ********* Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.1.30-community MySQL Community Server (GPL) Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cdcol | | expacc | | mengerie | | mydb | | mysql | | phpmyadmin | | test | | testdemo | | webauth | +--------------------+ 10 rows in set (0.23 sec) mysql> create database TestMove; Query OK, 1 row affected (0.00 sec) mysql> show databases;

The preference for host name is localhost v/s either the Machine Name (in this case Hodentek2) or the IP address. In windows the commands are not case sensitive. In the MySQL version used for this article the version is MySQL ODBC5.+--------------------+ | Database | +--------------------+ | information_schema | | cdcol | | expacc | | mengerie | | mydb | | mysql | | phpmyadmin | | test | | testdemo | | testmove | | webauth | +--------------------+ 11 rows in set (0. The default tab is User DSN. The first 'Show Databases' command does not display the TestMove we created which you can see in response to the 2nd Show Databases command.dll. Click Start | Control Panel | Administrative Tools | Data Sources (ODBC) and open the ODBC Data Source Administrator window as shown.1 and the file name is MyODBC5.00 sec) mysql> The login detail that works error free is shown. Change . Creating an ODBC DSN to connect to MySQL When you install from XAMPP you will also be installing an ODBC driver for MySQL for the version of MySQL included in the bundle.

.to System DSN as shown here.

... button to open the Create New Data Source window as shown.Click the Add.

Scroll down and choose MySQL ODBC 5. The MySQL Connector/ODBC Data Source Configuration window shows up.1 Driver as the driver and click Finish. .

If all . You must have your User Name/Password information to proceed further. The database is the name of the database you created earlier (TestMove) and this should show up in the drop-down list if the rest of the information is correct. Accept the default port.You will have to provide a Data Source Name (DSN) and a description. The server is the localhost.

Click and test the connection using the Test button.information is correct the Test button gets enabled. . You should get a response as shown.

There are a number of other flags that you can set up using the 'Details' button. The defaults are acceptable for this article. Click OK on the MySQL Connector/ODBC Data Source Configuration window.Click the OK button on the Test Result window. .

You have successfully created a System DSN 'AccMySQL' as shown in the next window. Click OK. .

e-commerce sites. Listing 2: Database TestMove is empty mysql> use testmove. communities. blogs.00 sec) mysql> Choosing an Open Source CMS: Beginner's Guide Find the best CMS and start working with it to create web sites. Database changed mysql> show tables. and intranets Export the Employees table from MS Access to TestMove . Empty set (0.Verify the contents of TestMove The TestMove is a new database created in MySQL and as such it is empty as you verify in the following listing.

Open the Northwind. .mdb (or any other mdb file) file as shown in the next figure.

. Click on Export to open the Export Table 'Employees' To.Highlight the Employees table and make a right click to bring up a drop-down menu as shown. . window.. Click on the Save as type drop-down to reveal the various file types that you can export to as shown.

You will notice the AccMySQL created earlier. .Click on ODBC Sources the Export window pops-up as shown. Click OK. This opens the Select Data Source window. Click on the Machine Data Source tab to show the various available DSN's.

If there is an error it however gives a error message. I assume you have not closed down the mysql > prompt. Enter commands as shown in the listing. +--------------------+ | Tables_in_testmove | +--------------------+ | employees | +--------------------+ 1 row in set (0.Choose AccMySql and click OK. mysql> use testmove Database changed mysql> show tables.00 sec) mysql> . Verifying and accessing exported table Now go back to the DOS screen. The program returns you to MS Access's Northwind database without giving you an indication about the success/failure of the export.

Apt.bmp | Andrew received his BTS commercial and a Ph. mysql> use testmove Database changed mysql> show tables. +--------------------+ | Tables_in_testmove | +--------------------+ | employees | +--------------------+ 1 row in set (0. the Seattle Chamber of Commerce. Capital Way | Tacoma | WA | 98401 | USA | (206) 555-9482 | 3457 | EmpID2. He is fluent in French and Italian and reads German. E.| 2 | | 2 | Fuller | Andrew | Vice President. She also co mpleted "The Art of the Cold Call.bmp | Education includes a BA in psychology from Colorado State University. in international marketing from the University of Dallas. 2A | Seattle | WA | 98122 | USA | (206) 555-9857 | 5467 | EmpID 1. He joined the company as a sales representative. was promoted to sales manager and was then named vice president of sales.00 sec) mysql> Select * from Employees. and the Pacific Rim Importers Association. | NULL | | 3 | Buchanan | Steven | Sales Manager | Mr.20th Ave. | 1952-0 2-19 00:00:00 | 1992-08-14 00:00:00 | 908 W. Sales | Dr. Andrew is a member of the Sales Management Roundtable. Probe a little further by entering a select statement to see how much of the data has come in. +------------+-----------+-----------+-------------------------------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------+-----------+ | EmployeeID | LastName | FirstName | Title | TitleOfCourtesy | BirthD ate | HireDate | Address | City | Region | PostalCode | Country | HomePhone | Extension | Photo | Notes | ReportsTo | +------------+-----------+-----------+-------------------------------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------+-----------+ | 1 | Davolio | Nancy | Sales Representative | Ms.D." Nancy is a member of Toastmasters International. | 1955-0 3-04 00:00:00 | 1993-10-17 00:00:00 | 14 Garrett Hill | London | NULL | .Now you can see that the Employees table has entered the TestMove database in MySql. | 1968-1 2-08 00:00:00 | 1992-05-01 00:00:00 | 507 .

where he was promoted to sales manager. He has also taken the courses "Multi-Cultural Selling" and "Time Management for the Sales Professional. | 2 | | 4 | Suyama | Michael | Sales Representative | Mr. Buchanan has completed the courses "Successful Telemarketing" and "International Sale s Management. he spent 6 months in an orientation program at the Seattle office a nd then returned to his permanent post in London." He is fluent in French. Upon joining the company as a sales representative." He is fluent in Japanese and can read and write French.bmp | Michael is a graduate of Sussex University (MA. economics) and the University of California at Los Angeles (MBA. . with a BSC degree. The next figure shows the same query run in MySQL Workbench a graphic tool used in querying and modeling MySql databases. Portuguese. Mr.| 3 | +------------+-----------+-----------+-------------------------------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------+-----------+ 4 rows in set (0. | 1963-0 7-02 00:00:00 | 1993-10-17 00:00:00 | Coventry House Miner Rd.00 sec) mysql> The output is not particularly pretty but you can see that the data has come in. Andrews University. | London | NULL | EC2 7JR | UK | (71) 555-7773 | 428 | E mpID6. Scotland.bmp | Steven Buchanan graduate d from St. marketing). and Spanish.SW1 8JR | UK | (71) 555-4848 | 3453 | EmpID5.

However there are few documents available that describe best practices for performing such a migration. Because MS Access applications were often created in an ad hoc fashion. That session brought together a number of MySQL users with a goal of identifying key success factors for moving MS Access applications to MySQL. Migration Issues According to a recent survey. migration can pose particular challenges. Application migration issues: MS Access applications often . The article also described the process of copying data in an MS Access database to MySQL. over 20% of MySQL users plan to migrate a Microsoft Access applications to MySQL over the next 12 months. This document summarizes discussion from the “MS Access Migration” session at the 2007 MySQL User Group meeting in California. MySQL Users reported two common migration problems: Data migration issues: MS Access data conversion is often complicated by poor schema design and even low data quality.Summary The article described the procedure to create an ODBC DSN for connecting to a MySQL Database.

MySQL users cited increasing pressures for companies to migrate Access applications: • • • • • Low data quality: Access applications often have out-of-date corporate data or corrupt data based on poorly defined schemas Poor security: Access applications do not integrate with corporate security and do not allow advanced security such as role-based access controls Limited manageability: Access applications can not be centrally managed by IT No web-based distribution: Access applications cannot be accessed over the web SOX compliance: Access applications are often identified in corporate audits as a significant source of risk. For example. Addressing Data Migration Issues MySQL provides a data migration tool. this tool is only as good as the underlying schema and data of the database to be converted. Because they grow organically. making them impossible to convert automatically. then adding ad hoc forms and reports.contain logic or design errors in their forms and reports. Rebuild the schema: create a new schema in MySQL that reflects SQL best practices rather than trying to simply recreate the Access schema in MySQL 2. Often. the schema may lack primary. the MySQL Migration Tool. The two most common data quality issues with Access migration are: Access data schema is not SQL-ready: Access developers are typically not familiar with the basics of SQL schema design. Because schema and data quality issues are so pervasive with Access. . these applications usually lack formal requirements. cleanse the data. and referential integrity constraints. foreign key. The group consensus was that a successful migration path has three fundamental tasks: 1. MySQL users often find it easier to rebuild the data schema in MySQL from scratch. However. MySQL DBAs report that Access schemas often resemble an Excel spreadsheet more than a classic SQL schema. Access applications are built by downloading corporate data to Excel. Rewrite the application: rebuild the Access application using web development tools like php or ActiveGrid Motivation for MS Access to MySQL Migration Access is the default choice of departmental developers with moderate technical skills. then import the data into the new MySQL schema 3. converting the spreadsheet to an Access database. Clean the data: extract the data from the Access database.

MySQL users reported little success with automated conversion. The following table shows the similarities between the MS Access and ActiveGrid visual development tools: Building an ActiveGrid application is as simple as following a three step process based on the Model-View-Controller (MVC) design pattern: . ActiveGrid is a web 2. most MySQL users choose to rewrite the application.0 visual builder like ActiveGrid. I don’t even want the logic moved – I don’t trust it. Similarly. Rapid Application Development For MySQL . it is often possible to consolidate several Access forms into a single.ActiveGrid MySQL users recognize that the process of migrating MS Access applications to MySQL often requires rebuilding the application. One MySQL user reported finding text strings in fields which were meant to be date fields for geotechnical data. well-designed web page.” Desire to make application web-based: most MySQL users would prefer to migrate “legacy” MS Access applications to more robust web architectures Security requirements: MySQL users often want to add “enterprise” security features like Siteminder/LDAP authentication and role-based access controls Although there are tools available that automate the conversion of a MS Access application to Java. In addition. Many MS Access application developers (and MySQL DBAs for that matter!) prefer a visual approach to building applications and have little interest in complex Java frameworks. the data in Access databases is often corrupt. There is still the issue of what to do with the forms and reports associated with the Access application. They are very interested in any tools that can accelerate the application development process. the preferred approach is to port MS Access applications using a coding language like PHP or a web 2. Addressing Application Migration Issues Porting the data from Access to MySQL only addresses part of the problem. Instead. While is possible to use ODBC to access MySQL data from Access. it is often possible to consolidate multiple Access applications into a single web application. ActiveGrid is ideal for these developers.Access data is not clean: in part because the tables were not defined rigorously.0 visual builder for MySQL that greatly simplifies the task of migrating an MS Access application. The reasons for rewriting Access applications include: • • • Quality issues: “given that the original MS Access app was written by a nonprogrammer.

Primary keys 2.1 relationships 3. ensure foreign key uniqueness for 1. The developer can define new actions using a visual action editor that call web services or custom code modules written in Java or Python. Build the controller(s). The developer specifies this information by importing an existing database schema or using a visual data editor. 3. For example. . The model defines the data used in the application.CSV export. decimal data types 5. A Process for MS Access Migrating The consensus of MySQL users is that automated conversion tools for MS Access do not work. security and web services. Default values for columns 5. Rewrite the application: review the forms. Ensure that data types agree. particularly for date. or the developer can create new web pages using a visual screen builder. along with cardinality constraints and delete propagation constraints 4. Although this is time intensive.1. the best practice for Access migration is to rebuild the schema. Ensure that non-null columns have a value 4. Ensure proper definitions for the following elements 1. integer. including database tables and relationships between tables. ActiveGrid can create default Ajax web pages based on the database schema. Before importing the data.. Null-allowed columns 6. Ensure primary key uniqueness 2. The controllers manage actions within the application. using the MySQL Migration tool or a simple . Indexes for common search and join columns 3. Create the views. Clean the data: extract the data from the MS Access database. Instead. Rebuild the schema: create a new schema in MySQL that reflects SQL best practices rather than trying to simply recreate the MS Access schema in MySQL. it is the only way to ensure that the resulting application is of sufficient quality to be maintainable. reports and queries of the Access application and re-design them rebuild the application forms and reports using web tools rather than trying to convert the existing application and scripts. Ensure referential integrity: check that primary key exists for all foreign keys. including navigation between pages and invoking data. 2. Define the model. Foreign keys for all relationships. Views 2. This lays out a step-by-step process to migrate an MS Access application to MySQL: 1. Import cleansed data into new MySQL schema 3. tools that translate existing Access applications to Java often result in 80% complete solutions where finishing the last 20% of the work takes longer than starting from scratch. perform data cleansing: 1. The view describes the web pages displayed by the application. cleanse the data and then rewrite the application.

best practices for Access to MySQL migration require careful migration of data to a new schema along with a requirements-driven rebuilding of the application forms and reports using web-based development tools. Import MySQL data schema into visual builder tool such as ActiveGrid 2. however. Create new web pages that provide graphical interface for application using the ActiveGrid page editor 3. Primary key collision involves two MySQL servers creating table rows containing different data. Other resources There are a number of additional resources on the web describing MS Access to MySQL migrations: Dual-Master MySQL 5 Replication Done Right MySQL replication is the most flexible way to deal with scalability.3? First Name Last Name Email þÿ þÿ þÿ þÿ . but the same primary key. In summary. MySQL is an increasingly attractive database solution for companies trying to improve the security and data quality of their departmental applications. feel free to visit our forum and share details! Problems? Neo Code Software Can Help! What's 10 .1. When this happens replication stops. custom Java or Python code. However a successful migration from Access requires pairing MySQL with a web development tool. At some point the weirdness gets noticed. step-by-step. we'll outline. In a dual master setup each server functions as both a master and a slave to the other server. replication can result in disaster. The most common problem with replication is primary key collision. If you run into any problems. or web services. Define actions that provide needed functionality for application using the ActiveGrid action editor. If not done right. the difference between the data on the servers grows. We'll call the two MySQL servers Server A and Server B. In this tutorial. how to avert disaster by creating a dual master MySQL replication setup configured to avoid primary key collision. With replication stopped. Then begins the painful process of recovery. of trying to weave masses of conflicting data into a whole.

You'll need to know the IP address of each server. If in doubt.cnf or my. password('<slave password>'). Select_priv. 'Y'. 'Y'. Creating A Slave User: Server MySQL command line> USE mysql. Password.ini). Below is what needs to be added to the configuration for Server A: server-id = 1 replicate-same-server-id = 0 auto-increment-increment = 2 auto-increment-offset = 1 master-host = <IP address of Server B> . Reload_priv. 'Y'. These users are used by MySQL for the slave to master connection and need to be given specific privileges. '<slave user>'. Repl_slave_priv) VALUES ('<Hostname/IP>'. Super_priv. User. On each server you'll need to edit your MySQL Server configuration file (usually called my. create a dump of one server's version of the database then import it into the other server. Server MySQL command line> FLUSH PRIVILEGES. Server MySQL command line> INSERT INTO user (Host. Example of A MySQL Dump: Server A command line> mysqldump -u <mysql user> -p<mysql password> -c <database name> > <filename of dump> (copy dump file to Server B) Server B command line> mysql -u <mysql user> -p<mysql password> -D <database name> < <filename of dump> The next thing to do is create a "slave user" on each of the two servers. 'Y'). You can name them whatever you'd like. Configuring The MySQL Servers The next thing to do is configure each MySQL server.Preparing For Replication The first thing to do when getting ready for replication is to make sure that the database on each server is in the same state.

log # change this to a path/name appropriate to your system binlog-do-db = <database name> Below is what needs to be added to the configuration for Server B: server-id = 2 replicate-same-server-id = 0 auto-increment-increment = 2 auto-increment-offset = 2 master-host = <IP address of Server A> master-user = <slave user> master-password = <slave password> master-connect-retry = 60 replicate-do-db = <database name> log-bin= C:\mysql\log\log-bin. Note: The two MySQL configuration variables that prevent key collisions are autoincrement-increment and auto-increment-offset.master-user = <slave user> master-password = <slave password> master-connect-retry = 60 replicate-do-db = <database name> log-bin = C:\mysql\log\log-bin. do this! A relay log.log # change this to a path/name appropriate to your system binlog-do-db = <database name> After making the changes to your configuration. is simply a local echo of data read from the master's log. The value of auto-increment-increment should be set to N. restart the two servers. . A warning message will likely recommend that you specify the name of your relay log in your server configuration. Synchronizing the Servers The last thing needed to set up replication is the synchronization of the servers. issue the "slave stop" command then the "show master status" command. In the MySQL command line of each server. This information is needed to "synchronize" the two servers. The auto-increment-offset and server-id configuration variables should be set as consecutive integers (in this case 1 and 2). To avoid future problems. for those that are wondering. where N is equal to the number of servers in the replication setup (in this case two). This will give you infomation that you'll need to manually provide to the other server. Check your MySQL error logs for any problems.

issue the "start slave" command on both servers. and synchronize the servers (as outlined in the previous section). Have fun! . the slave status of each server may look normal. Resetting Replication It doesn't take much for replication to go out of sync.Next. This will generally disrupt replication even though. delete their relay logs. To reset replication. while those generated on Server B should always be even. Replication should now be working! To confirm that replication is working. Once you've entered the above command. shut down both servers. A network outage can be simulated by unplugging one of the MySQL servers from the network. Deleting the relay logs will cause each server to re-read from their master. after restoring network connectivity. first issue the "show slave status" command on both servers. try inserting rows to both. on each server enter the command below into the MySQL command line of each server: Server MySQL command line> CHANGE MASTER TO MASTER_HOST='<master's IP>'. While one server is unplugged. MASTER_LOG_FILE='<master's log file name>'. It makes sense to learn how to reset replication before something goes wrong. MASTER_PASSWORD='<slave password>'. Both "Slave_IO_Running" and "Slave_SQL_Running" should be "YES". MASTER_LOG_POS=<master's log file position>. Testing Before putting a replication setup into production. If both aren't "YES". A simple network interruption to one server can effectively halt two-way replication if data gets written during the interruption. you'll need to reset replication. be sure to thoroughly test it. Primary keys generated on Server A should always be odd numbers. MASTER_USER='<slave user>'.

Sign up to vote on this title
UsefulNot useful