Archive: Create Schema on mySQL when finising installation


Create Schema on mySQL when finising installation
I have read many information about MySQL on this forum but I still do not find my answer.
When finishing to install mysql, how can I create a schema and run file script on command line to finish my install by NSIS language.
In my opinion, I do with it but it does not work
eg:

Section Run_Scrilpt
execDos::exec /NOUNLOAD /TIMEOUT=10000 '"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" --user=root --password=passroot --database=openim < "e:\server\openim.sql" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
SectionEnd
I am worry about password of root because I do not know it during user to set up our application.
Somebody used to see this problem please help me!!!
Big thank for someone can reply me!

I also check all cases which I read on forum but It can not still run.


ExpandEnvStrings $0 %COMSPEC%

Exec '"$0" /C "$PROGRAMFILES\MySql\MySql server 5.0\bin\mysql.exe" "openim" < "$INSTDIR\openim.sql"'
Pop $0 # return value

MessageBox MB_OK "Exit code $0"
I only need to create database and run script on mysql!!!!
Please help me!!!!

How can I create a schema by NSIS
I am sorry for self reply. Now I can run a script on MySQL. However, I do not know how to create a database (schema).
How can I do with it????
Your help will be really appreciated!!!
Thank alot,


TO create the database test use (and then go into the db):

CREATE DATABASE IF NOT EXISTS `test` ;
USE test;

You may want also have a look at MySQL doc:
http://dev.mysql.com/doc/refman/5.0/...-database.html

Cheers,
L@u


Thank OrdiMars alot,

I know this command but I do not know how to write it on NSIS because I must run this on MySQL.
On mysql,I must run MySQL.exe and type password to use mySQL after that I can make this command run.
But I do not know password of root or user who install MySQL and write it.That is the first problem which I see.
The second how can I run thing to create database on this. These document on forum also show how to run application on command line not in mysql.

Your help is very useful for me!
Best regard,


You can use InstallOptions to write a custom page that prompts the user for the MySQL root password. Then create a temporary schema file and execute MySQL twice:

Page custom CustomPage CustomFinish " - MySQL root password"
Var $mysql_root_pass

Function CustomPage
InitPluginsDir
SetOutPath $PLUGINSDIR
File mysql.ini ; refer to InstallOptions docs for instructions for creating this file
InstallOptions::initDialog /NOUNLOAD "$PLUGINSDIR\mysql.ini"
InstallOptions::show
FunctionEnd

Function CustomFinish
; NOTE: Be sure to bhange "field 1" to the field number!
ReadINIStr $mysql_root_pass $PLUGINSDIR\mysql.ini "Field 1" State
StrCmp $mysql_root_pass "" 0 +3
MessageBox MB_YESNO|MB_ICONQUESTION "You didn't type a MySQL root password, \
connection to MySQL may fail, do you want to continue?" IDYES +2
Abort ; returns to the page
FunctionEnd

Section
GetTempFileName $R0
FileOpen $R1 $R0 "w"
FileWrite $R1 "CREATE DATABASE IF NOT EXISTS openim;$\r$\n"
FileClose $R1
; First round: create the database
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=$mysql_root_pass < "$R0" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
; Second round: run the schema
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=passroot --database=openim < "e:\server\openim.sql" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
SectionEnd

(edit: it may be worthy to note that I'm a little rusty at NSIS, so there might be some minor problems with this code; I couldn't test it because everything's booted into Linux at the moment)

-dandaman32

Thank you very much dandaman32,
I know my requirement is not suitable but I am studying about InstallOptions and many things I do not understand clearly :( .
Could you please send me the format file mysql.ini?
I am so sorry for my inconvenience because I still do not study it well.
Thank alot,
:D



Section "RunDB"
GetTempFileName $R0
FileOpen $R1 $R0 "w"
FileWrite $R1 "CREATE DATABASE IF NOT EXISTS openim;$\r$\n"
FileClose $R1
; First round: create the database
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=$mysql_root_pass < "$R0" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
; Second round: run the schema
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=passroot --database=openim < "e:\server\openim.sql" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
SectionEnd
Now I can understand InstallOption. When I read your code, i have some confuse.

; First round: create the database
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=$mysql_root_pass < "$R0" ""'

It runs $R0 to create database on mysql.When I create database on mysql, we must pass 3 function
+ Run mysql.exe
+ Enter password
+ Type this command "create database if not exists openim"
Firstly, your command run mysql.exe and I can not understand how to work.
Although I try your code,I does not work. I aslo change many times but I do not work.
eg:
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" --password=$mysql_root_pass < "$R0" ""'
If someone use to see this problem, please tell me!!!
Thank you very much for your help,

dandaman32 posted:

Page custom CustomPage CustomFinish " - MySQL root password"
Var $mysql_root_pass

Function CustomPage
InitPluginsDir
SetOutPath $PLUGINSDIR
File mysql.ini ; refer to InstallOptions docs for instructions for creating this file
InstallOptions::initDialog /NOUNLOAD "$PLUGINSDIR\mysql.ini"
InstallOptions::show
FunctionEnd

Function CustomFinish
; NOTE: Be sure to bhange "field 1" to the field number!
ReadINIStr $mysql_root_pass $PLUGINSDIR\mysql.ini "Field 1" State
StrCmp $mysql_root_pass "" 0 +3
MessageBox MB_YESNO|MB_ICONQUESTION "You didn't type a MySQL root password, \
connection to MySQL may fail, do you want to continue?" IDYES +2
Abort ; returns to the page
FunctionEnd

Section
GetTempFileName $R0
FileOpen $R1 $R0 "w"
FileWrite $R1 "CREATE DATABASE IF NOT EXISTS openim;$\r$\n"
FileClose $R1
; First round: create the database
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=$mysql_root_pass < "$R0" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
; Second round: run the schema
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'"$PROGRAMFILES\MySQL\MySQL Server 5.0\bin\mysql.exe" \
--user=root --password=passroot --database=openim < "e:\server\openim.sql" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
SectionEnd


Now I can understand what you want to help me,however I run your script. It always displays Error 1
I do not know Error 1 to appear where, so I can not repair it.
However, I change your code with this:


Section
StrCpy $passroot_sql '123456789'
GetTempFileName $R0
FileOpen $R1 $R0 "w"
FileWrite $R1 "CREATE DATABASE IF NOT EXISTS openim;$\r$\n"
FileClose $R1
; First round: create the database
execDos::exec /NOUNLOAD /TIMEOUT=10000 \ 'mysql.exe --user=root --password=$passroot_sql < "$R0" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
; Second round: run the schema
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'mysql.exe --user=root --password=$passroot_sql --database=openim < "e:\server\openim.sql" ""'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
SectionEnd


It announces Error -14
I can not repair it.
Somebody see this problem, please help me,
I try my best to study NSIS during many weeks but now I am still not good.
Your help is very useful for me,
Thank a lot,

You have extra quotes after "e:\server\openim.sql". Remove them.


Thank Kichik,

Kichik posted
You have extra quotes after "e:\server\openim.sql". Remove them.
I try with both but It can not work and show [COLOR=dark red] ERROR -14[/COLOR]

Section
StrCpy $passroot_sql '123456789'
GetTempFileName $R0
FileOpen $R1 $R0 "w"
FileWrite $R1 "CREATE DATABASE IF NOT EXISTS openim;$\r$\n"
FileClose $R1
; First round: create the database
execDos::exec /NOUNLOAD /TIMEOUT=10000 \ 'mysql.exe --user=root --password=$passroot_sql < "$R0"'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
; Second round: run the schema
execDos::exec /NOUNLOAD /TIMEOUT=10000 \
'mysql.exe --user=root --password=$passroot_sql --database=openim < "e:\server\openim.sql"'
Pop $0 # return value
MessageBox MB_OK "Exit code $0"
SectionEnd



Actually, I only want to run two command on console window:

C:\>mysql.exe --user=root --password=$passroot_sql < e:\db.txt //to create database
C:\>mysql.exe --user=root --password=$passroot_sql < e:\openim.sql //to run file script

I try my best to run with all exec,execwait, execshell, ExecCmd::Exec.... but I can not work well.
Please someone help me,
Thank a lot,

You should consult MySQL's documentation to find out what error -14 means.


Thank Kichik,
I will find it on mysql forum.And now I can do it well.
In my application, I use passdialog to get password of mysql to create schema or run sql. But I do not know password which user input is right or wrong.
I want to check it but in my code, I wrote it on file bat and run it. Also, I use nsExec::exec to hide window console.
How can I know error happen when user type wrong password?
Console window is hidden so how can I display error for them to know or anything else to know?
Thank you for your help,


Make a test schema (.sql file) that creates a database with a name that's total gibberish and then drops it again. Then use Pop after you call nsExec to get the return code of mysql.exe. Use StrCmp to check the status code - 0 is success, anything else is failure. AFAIK that's the only reliable way to check the password.

-dandaman32


Thank dandaman32 for your reply,
I also think about this and do with it already. But I can pop error. I do not know why
In file schema.sql "create database if not exists OpenIM"
My code here:


$passroot is got from dialog password
FileOpen $1 $0 "w"
FileWrite $1 "@echo off$\r$\n"
StrCpy $2 $TEMP 2 ##C:
FileWrite $1 "$2$\r$\n"
FileWrite $1 "mysql.exe --user=root --password=$passroot < c:\openim\home\database\schema.sql$\r$\n"
FileClose $1
nsExec::exec "$0"
Pop $0
IfErrors 0 NoAbort
MessageBox MB_OK "Error code $0"
Detailprint "Create database is error"
NoAbort:
Delete $0
Detailprint "Create database successful"

I think I have some mistakes in my code, if you see it, please tell me!!!
Thank alot,

-14 comes from ExecDos errors enum:


enum ERROR_CODES {
ERR_DUPHANDLE = -16,
ERR_CREATEPIPE,
ERR_CREATEPROC,
...

"Could not create process" means Win API CreateProcess() call returned error. So I guess mysql is not in path for the last script.