Jump to content

mysql sql import script


tinboye

Recommended Posts

Does anyone have an example script or instructions on how i can make it so when a user creates a game account/ adds a gameserver to their account, that if the game requires mysql db, that it will auto import the sql file into the created database. I already have these scripts in the game server template.

 

http://help.tcadmin.com/Automatically_Create_a_MySQL_Database

 

thanks for your help

Link to comment
Share on other sites

  • 2 weeks later...

So i managed to get this to work by simply taking the http://help.tcadmin.com/Automatically_Create_a_MySQL_Database script of restoring a backup.

 

Operating System: Any
Description: Restore MySQL database after move
Script Engine: IronPython
Event: After move
Ignore execution errors Unchecked
Script:
import clr;
import System;

clr.AddReference("TCAdmin.DatabaseProviders.MySql");
clr.AddReference("TCAdmin.SDK");
from TCAdmin.DatabaseProviders.MySql import MySqlManager;
from System import String;
from System.IO import File, Path;

mysql_server="localhost";
mysql_root="root";
mysql_password="";

if not ThisService.Variables.HasValue("MySQLUser") :
Script.Exit();

backupfile=Path.Combine(ThisService.RootDirectory, "databasename.sql");

with MySqlManager() as mysql:
escapeduser=mysql.PrepareSqlValue(ThisService.Variables["MySQLUser"]);
escapedpass=mysql.PrepareSqlValue(ThisService.Variables["MySQLPassword"]);
mysql.Connect(String.Format("Data Source={0};User Id={1};Password={2};Pooling=False;", mysql_server, mysql_root, mysql_password));

mysql.ExecuteNonQuery(String.Format("DROP DATABASE IF EXISTS {0};", escapeduser));
if mysql.Execute(String.Format("SELECT * FROM mysql.user WHERE user='{0}' AND host='localhost';", escapeduser)).Rows.Count == 1 :
 mysql.ExecuteNonQuery(String.Format("DROP USER {0}@localhost;", escapeduser));

mysql.ExecuteNonQuery(String.Format("CREATE DATABASE {0};", escapeduser));
mysql.ExecuteNonQuery(String.Format("GRANT ALL PRIVILEGES ON {0}.* TO '{0}'@'localhost' IDENTIFIED BY '{1}';", escapeduser, escapedpass));

if File.Exists(backupfile) :
with MySqlManager() as mysql2:
 mysql2.Connect(String.Format("Data Source={0};Database={1};User Id={1};Password={2};Pooling=False;", mysql_server, ThisService.Variables["MySQLUser"], ThisService.Variables["MySQLPassword"]));
 mysql2.ImportDatabase(backupfile);
 File.Delete(backupfile);

 

and made sure that the sql file was in the root directory of the game folder.

I have this script execute once the mod for the server is installed.

 

works like a charm

Link to comment
Share on other sites

yes that was me lol, im still figuring things out. mainly how the configuration editor works. I still have not figured out how a user is only able to edit certain fields of a cfg file. Or if thats even possible.

 

Also trying to figure out how I can make it so mysql user/pass can be retrieved as a link in the user control panel. I have seen this done with gtx gaming. Just not sure how to do that.

Edited by tinboye
Link to comment
Share on other sites

  • 2 weeks later...
yes that was me lol, im still figuring things out. mainly how the configuration editor works. I still have not figured out how a user is only able to edit certain fields of a cfg file. Or if thats even possible.

 

Also trying to figure out how I can make it so mysql user/pass can be retrieved as a link in the user control panel. I have seen this done with gtx gaming. Just not sure how to do that.

 

Displaying the user/pass is easy.

 

As an example, I have a script that installs mysql and creates the user with a random password. That password is saved to a variable. You can then make a custom button that echos out the password.

 

ThisService.Variables["ExampleUser"] = Random.RandomString(10,True,True)
ThisService.Save()

 

Feel free to add me on Skype. I'm not always around but I enjoy teaching people about TCA. Skype is barrycarey66

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Who's Online   0 Members, 0 Anonymous, 27 Guests (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Terms of Use