
Configuring MySQL server connection in SmartFoxServer
- Open the
config.xml
in your favorite text editor. - Go to line 203 of the
config.xml.
This line should be within the structure of aZone
tag with name asdbZone.
- Change the lines 203-218 from the
config.xml:
Original code:
<DatabaseManager active="false"> <Driver>sun.jdbc.odbc.JdbcOdbcDriver</Driver> <ConnectionString>jdbc:odbc:sfsTest</ConnectionString> <!-- Example connecting to MySQL <Driver>org.gjt.mm.mysql.Driver</Driver> <ConnectionString>jdbc:mysql://192.168.0.1:3306/sfsTest </ConnectionString> --> <UserName>yourname</UserName> <Password>yourpassword</Password> <TestSQL><![CDATA[SELECT COUNT(*) FROM contacts]]></TestSQL>
- Replace the code in lines 203-218 with the following code:
<DatabaseManager active="true"> <Driver>org.gjt.mm.mysql.Driver</Driver> <ConnectionString>jdbc:mysql://127.0.0.1:3306/mysql </ConnectionString> <UserName>root</UserName> <Password></Password> <TestSQL><![CDATA[SELECT NOW()]]></TestSQL>
The new setting activates the DatabaseManager
and configures the JDBC driver to the MySQL connector that we just downloaded.
We also changed the user name and password of the connection to the database to "root" and empty password.
Note
We will use the empty password through out the development process but it is strongly recommended to set your own database user password.
There is a TestSQL
setting where we can write a simple database query so that the SmartFoxServer will try to run it to test if the database connection is correct. As we have not created any new databases for the virtual world, we will test the database connection by querying the current server time.
Restarting the server
We've just set up the connection between SmartFoxServer and third-party database. It is time to test the new setting by restarting the SmartFoxServer.
To stop the SmartFoxServer in Windows and Linux, press Ctrl + C. To stop it in Mac OS X, click on the Cancel button in the SmartFoxServer log window.
Note
There is a log that appears as usual after we start up the server again. It is important to check the log carefully every time the config.xml
is changed. The logfile can provide details of any errors that occur when it tries to load the configure file.
For example, if we configure the database connection just now but forget to activate the DatabaseManager
, the server will start up correctly. Then you may spend a lot of time debugging why the database connection is not working until you find that the DatabaseManager
is not active at all. This happened to me several times while I was developing my first Flash virtual world.
If the server is running with the new database connection settings, the following lines will be appearing in the log. There can be different database manager settings for each zone. When checking the log, we should be aware which zone the log is referring to. We are configuring the database manager of dbZone
zone now.
DB Manager Activated ( org.gjt.mm.mysql.Driver )
Zone: dbZone
If we forget to activate the DatabaseManager
, we will not see the DB Manager Activated wording. Instead, the following message may appear in the log:
DB Manager is not active in this Zone!
Moreover, if the SmartFoxServer faces some fatal error on start up, it will terminate itself with more detailed error logs. The following lines are an example for error logs that appear when the MySQL connector file is missing:
Can't load db driver: org.gjt.mm.mysql.Driver
[ Servre ] > DbManager could not retrive a connection. Java.sql.SQLException: Configuration file not found
DbManagerException: The Test SQL statement failed! Please check your configuration.
These lines state that the testing SQL failed to run, which we just set to test the connection. It also describes what exception has caused this error to help the debugging.
