how to connect to a MySQL server [closed]

老子叫甜甜 提交于 2019-12-11 18:47:31

问题


First of all, I'm a complete novice when it comes to this procedure.

I've been working on setting up a connection to my local SQL server I've set up using MySQL Workbench. I'm sure my SQL server is running, because I can set up an ODBC DataSource in Control Panel - Administrative Tools, and this connects to my local server (it also lists my databases).

Now it comes to delphi XE-5. I figured setting up a connection would be as easy as logging in to an IP with port, login name and password but after 2 days of reading and trying out I figured that ignorance is blissed.

So can somone tell me step-by-step what connector to use and how to set it up. What possible dll's and/or driver files I need in my exe folder. How to tell my app that there there. And possible other stuff I'm missing.

Thanks in advance


回答1:


The most likely explanation of this is probably that there is a mis-match between the version of libmysql.dll you're using and your DBX set-up, in particular its driver for mySql. For XE6, where I ran into this problem looking into yours, the cause was that the version of the libmysql.dll I was trying to use is apparently too recent for the XE6 DBX driver; whether that is correct or not, the steps below solve the problem for my XE6.

(Readers noting the XE5 tag on the q and the absence of a D7 one might find their mouse twitching towards the -1 button when they first see that this is mostly about D7. However, many of the current SO Delphi qs are from D7 users, and I'm sure I'm not the only one who'll ever face the problem of retro-fitting mySql to DBX in D7; someone more familiar with mySql would probably say they are overkill just for XE5/6.)

This Q inspired me to try getting mySql working with D7 and XE6, with no previous knowledge of it.

Regarding D7, it soon turned out that I had created a problem for myself by not getting a copy of libMySql.dll when D7 was current 14 years ago. I also found a post in the EMBA newsgroups from Quinn Wildman of EMBA about a problem with XE6 and the most recent versions of the dll, saying that the OP in that case had a too-recent version of the DLL.

My first attempt at installing mySql - basically by installing a copy of the current mySqll Workbench - seemed to confirm what QW said; I couldn't open a TSqlConnection in D7 or XE6 using the version of libmysql that comes with it. Fortunately the mySql website has an archive and I found and installed an earlier version of the Workbench, 5.0.18, which had a 2006 version of the libmySql.dll (btw, I found this earlier version of Workbench almost unfathomable to use, unlike the current one, but that's another story).

Getting and installing the libmysq.dll that comes with an less recent version of WB, such as 5.0.18 is probably all that's necessary to to roll back from at too-recent version to get it working with XE5 or 6.

Getting it working with D7 turned out to be considerably trickier, because D7 didn't support v5.x of mySql out of the box. Fortunately there is a site with a v5 DBX driver for D7. but even equipped with that, it was still a bit of a performance to get v5.x of mySql working with D7. I dare say someone familiar with mySql might know a simpler way, but at least the method below worked.

Starting with mySql completely uninstalled from my Win7 64-bit machine, following earlier failed attempts, and with all the mySql stuff below C:\Users[...] deleted (including any mySql database files):

  1. a) Go to http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html and download the source and dll zip files. You need the ini files in the source zip.

    b) Open the source zip and follow the instructions in the dbxdrivers.ini to copy the driver settings to the ini file of the same name in the \Shared\DBExpress folder under the Delphi 7 install directory. Don't forget to add the driver name to the list at the top of the dbxdrivers.ini file.

    c) Likewise edit the contents of dbxconnections.ini from the source zip into the one in [...]\Shared\DBExpress

  2. Go to www.Mysql.com, create an account if you don't aready have one. Download the current version of MySql Workbench (6.1 for MySql as at 22 Jul 2014 Go to the archive tab and download Workbench 5.0.18.

  3. Install the earlier 5.0.18 version of Workbench

  4. Make a copy of libmysql.dll from 5.0.18's folder under program files(x86)\MySql somewhere else, because the next step will remove the original there(!)

  5. Install the later version of Workbench and, if MySql hasn't previously been set up, allow the installer wizard to set up a new local server. Set root password and create a user account. I called mine 'sa'.

  6. Use the Sql Script window in Workbench to create a test database, I used MATestDB as the name because there was already one called 'test', and create a table in it and add a few rows.

  7. Copy dbxopenmysql50.dll from the dll zip in step 1 to the Borland Shared\DBExpress directory mentioned in step 1

  8. Go to the Shared\DBExpress directory and edit the two ini files so that the database, user name and password correspond with the ones in steps 5 and 6.

  9. In the D7 IDE create a new project and add a TSqlConnection to its main form. Check that you can set its Connected property to True.

  10. Save the project to a new folder and copy to it

    • libmysql.dll saved from step 4
    • dbxopenmysql50,dll from the dll zip in step 1
    • the two ini files from [...]\Shared\DBExpress
  11. Repeat steps 9 and 10 for XE6.

  12. mySql for D7 and XE6 should all now be set up.



来源:https://stackoverflow.com/questions/24883505/how-to-connect-to-a-mysql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!