Reprinted from http://blog.csdn.net/nette/archive/2009/07/30/4394849.aspx
Unity3d connection to MySQL database
1. Set up a MySQL database in Mac OS X
Download MySQL for Mac http://dev.mysql.com/downloads/mysql/5.0.html
You can use the PKG format of the binary installation package to install MySql in Mac OS X 10.2.x ("Jaguar") and later versions instead of the binary distribution version. Note that this software package does not support earlier versions of Mac OS X (for example, 10.1.x ).
The installation package is located in the hard disk image (. DMG) file. You must double-click the icon in the search box to install the package. Images should be installed and displayed.
Note: Before continuing the installation, you must use the MySQL Manager application (on the Mac OS X Server) or use the mysqladmin shutdown command line to close all MySQL Server instances.
To install the MySQL PKG file, double-click the package icon. The installation package installer for Mac OS X will be started and you will be guided to complete MySQL installation.
Mac OS X installation package installer has a defect. You will find this error message in the target disk selection dialog box:
You cannot install this software on this disk. (null)
If this error occurs, you only need to click go back to return to the previous window. Then click "continue" to select the target disk again. You should be able to select the target disk correctly. We have reported this defect to Apple and it is investigating this issue.
Install Mac OS X PKG of MySQL to/usr/local/MySQL-version, and install a symbolic connection,/usr/local/MySQL, pointing to a new location. If the/usr/local/MySQL directory exists, first change it to/usr/local/MySQL. Bak. After installation, the installer runs mysql_install_db to create an authorization table in the MySQL database.
The installation layout is similar to that of the TAR binary distribution. All MySQL binary codes are in the/usr/local/MySQL/bin directory. By default, the MySQL socket file is created as/tmp/MySQL. Sock.
MySQL installation requires the MySQL user account of Mac OS X. In Mac OS X 10.2 and later versions, the MySQL user account should exist by default.
If you are running the Mac OS X Server, install MySQL. The following table lists the MySQL versions installed with Mac OS X Server versions:
MySQL version of Mac OS X Server
10.2-10.2.2 3.23.51
10.2.3-10.2.6 3.23.53
10.3 4.0.14
10.3.2 4.0.16
10.4.0 4.1.10a
This section only covers the official MySQL Mac OS X PKG version. Be sure to read Apple's help information on MySQL installation: run the "Help View" application, select "Mac OS X Server" help, and search for "MySQL ", read the content titled "Install MySQL.
For MySQL versions installed on Mac OS X servers, if the version is earlier than 4.0, note that mysqld_safe should be used instead of safe_mysqld to start mysqld.
If you have installed the MySQL package for Marc liyanage downloaded from the http://www.entropy.ch on Mac OS X, you can just use the binary installation layout shown on this page to install according to the update instructions.
If you upgrade Marc 3.23.xx or Mac OS X's MySQL server to the official MySQL PKG, you also need to convert the existing MySQL authorization table to the current format, because many new security permissions have been added.
If you want to automatically start MySQL during system startup, you also need to install MySQL startup Item. In MySQL 5.1, it is part of the Mac OS X installation disk image and is an independent installation package. Double-click the mysqlstartupitem. icon and follow the instructions to install it.
Please note that startup Item only needs to be installed once! You do not need to install the MySQL package each time you upgrade it.
The startup Item of MySQL 5.1 is installed in/library/startupitems/mysqlcom. (In versions earlier than MySQL 4.1.2, the installation path is/library/startupitems/MySQL, But it conflicts with MySQL startup Item installed on Mac OS X Server ). When startup Item is installed, add the variable mysqlcom =-yes-to the system configuration file/etc/hostconfig -. If you want disable to automatically start MySQL, you only need to change this variable to mysqlcom =-no -.
On Mac OS X Server, MySQL is installed by default using the variable MySql in the/etc/hostconfig file. MySQL AB startup Item installer sets it to MySQL =-no-disable this variable. In this way, the startup time does not conflict with the mysqlcom variable used by MySQL AB startup Item. However, the running MySQL server is not disabled. You should close it yourself.
After installation, you can run the following command in the terminal window to start MySQL. You must have the administrator privilege to perform this task.
If you have installed startup Item:
Shell> sudo/library/startupitems/mysqlcom start
(Enter your password, if necessary)
(Press Control-D or enter "exit" to exit the shell)
If you do not use startup Item, enter the following command sequence:
Shell> Cd/usr/local/MySQL
Shell> sudo./bin/mysqld_safe
(Enter your password, if necessary)
(Press Control-Z)
Shell> BG
(Press Control-D or enter "exit" to exit the shell)
You should be able to connect to the MySQL server, for example, by running/usr/local/MySQL/bin/MySQL.
Note: The account in the MySQL authorization table does not have a password. After starting the server, you should set the password according to the instructions in section 2.9, "setup and test after installation.
You can create an alias for the Shell Source file to make it easier to access common programs, such as running MySQL and mysqladmin from the command line. The tcsh syntax is:
Aliasmysql/usr/local/MySQL/bin/MySQL
Aliasmysqladmin/usr/local/MySQL/bin/mysqladmin
For Bash, use:
Aliasmysql =/usr/local/MySQL/bin/mysqlaliasmysqladmin =/usr/local/MySQL/bin/mysqladmin
It is best to add/usr/local/MySQL/bin to the path environment variable. For example, if the shell command is tcsh, add the following line to the $ home/. tcshrc file:
Setenv path $ {path}:/usr/local/MySQL/bin
If the. tcshrc file does not exist in the root directory, use a text editor to create the file.
If you upgrade the existing installation, note that the old installation directory is not uninstalled when you install the new MySQL PKG. Unfortunately, the Mac OS X installer has not provided any function to correctly upgrade the software package.
To use an existing database in a new installation, copy the content in the old data directory to the new data directory. When you perform this operation, make sure to disable the new and old servers. After copying the previously installed MySQL database files and successfully starting a new server, you should unmount the old files to save hard disk space. In addition, the old package receipts directory in/library/Receipts/mysql-VERSION.pkg should be detached.
Ii. MySQL management tools
Download http://dev.mysql.com/downloads/gui-tools/5.0.html
By default, MySQL only has the root user and can only log on locally. After using the root user, add other users.
User Root Login
Add a local user named username and password
Grant all privileges on *. * To username @ localhost identified by 'Password' with grant option;
Add a remote user named username and password
Grant all privileges on *. * To username @ "%" identified by 'Password' with grant option;
After adding a user, restart the MySQL service.
Iii. MySQL connector/NET 1.0
Http://dev.mysql.com/downloads/connector/net/1.0.html
Note: The later version is unavailable;
Download the binaries and no installer versions. decompress the package and you will find the mysql. Data. dll file in the bin folder. Copy this file to the assets folder and place it in any directory. Then you can use using MySQL. Data. mysqlclient in the C # script;
The generated project must place the DLL in the same directory. And can be used normally in windows;
Iv. Code
Using unityengine;
Using system;
Using system. collections;
Using system. Data;
Using mysql. Data. mysqlclient;
Public class cmysql: monobehaviour {
// Global variables
Private Static mysqlconnection dbconnection;
Static string host = "192.168.1.100 ";
Static string id = "MySQL ";
Static string Pwd = "123456 ";
Static string result = "";
Void ongui ()
{
Host = guilayout. textfield (host, 200, guilayout. Width (200 ));
Id = guilayout. textfield (ID, 200, guilayout. Width (200 ));
Pwd = guilayout. textfield (PWD, 200, guilayout. Width (200 ));
If (guilayout. Button ("test "))
{
String connectionstring = "Server =" + host + "; database = test; user id =" + ID + "; Password =" + PWD + "; pooling = false ";
Opensqlconnection (connectionstring );
}
Guilayout. Label (result );
}
// On quit
Public static void onapplicationquit (){
Closesqlconnection ();
}
// Connect to database
Private Static void opensqlconnection (string connectionstring ){
Dbconnection = new mysqlconnection (connectionstring );
Dbconnection. open ();
Result = dbconnection. serverversion;
Debug. Log ("connected to database." + result );
}
// Disconnect from database
Private Static void closesqlconnection (){
Dbconnection. Close ();
Dbconnection = NULL;
// Debug. Log ("disconnected from database." + result );
}
// MySQL Query
Public static void doquery (string sqlquery ){
Idbcommand dbcommand = dbconnection. createcommand ();
Dbcommand. commandtext = sqlquery;
Idatareader reader = dbcommand. executereader ();
Reader. Close ();
Reader = NULL;
Dbcommand. Dispose ();
Dbcommand = NULL;
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nette/archive/2009/07/30/4394849.aspx