O== Using this IN A BASH works as expected :
/usr/bin/mysqldump --add-locks -e --force -R --triggers --add-drop-table --hex-blob -h localhost --password=dbpass --user=dbuser dbname
same commandline via EXEC() of i.e. Java ignores the dbname at the end and instead generates this error message:
"mysqldump: Got error: 1046: No database selected when selecting the database"
which, in itself is nonsense, as it should be "empty database name used to select database"
Runtime task = java.lang.Runtime.getRuntime();
process = task.exec( " ENTER MYSQLDUMP HERE " );
******* This javacode executes commands without giving environ vars to the childprocess.
This may be the reason why the bug is triggered at all. *******
See addition information for a link that generated the same error, but did not have java involved.
The main mistake may be another, but it triggers the same internal bug.
O== WORKAROUND:
/usr/bin/mysqldump --add-locks -e --force -R --triggers --add-drop-table --hex-blob -h localhost --password=dbpass --user=dbuser --databases dbname
Adding --databases fixes the issue.
BUT ::.. it adds another problem to the SQL DUMP.. --databases makes mysqldump believe that there is more to come
and it adds
"mysqldump: Got error: 1046: No database selected when selecting the database"
at the end of the dump, because, again, it tries to use an empty databasename in the "selectDatabase()" call.
O== CONCLUSION:
We have several bugs to deal with :
1. in a none bash env, the last cmdline option is ignored ( or erased in the process ) AND
2. a database connect is done without a database name present to select the database with
3. --databases invents an additional, empty databasename and tries to connect to the database with it.
even if, i don't think so, but i take it into account, that my javacode may did something wrong,
which would be a surprise as any other shell command executed in the last 4 years worked as expected,
something is wrong inside mysqldump.
All those bugs do not happen with bash involved.
O== SIDENOTE:
____________________________________________________________________________________________________________________________________
4. In the process of trying this out, i found out, that --password=dbpassword IS IGNORED as a parameter by the cmdline tool "mysql" and i had to revert to "-pDBPASSWORD" option. This breaks the mysql usage description. I will open a seperate bugreport for it!
____________________________________________________________________________________________________________________________________
O== Version-Release number of selected component (if applicable):
mariadb-10.0.23-1.fc22.i686
O== How reproducible:
Steps to Reproduce:
1. Create a small tool, that calls mysqldump with an execute() system() etc. call which does not involve bash!
2. try to dump something.
Actual results:
mysqldump: Got error: 1046: No database selected when selecting the database
Expected results:
correct parsing of the cmd options without bash environment.
Additional info:
The same error message ( and as it seems the same internal cause inside mysqldump ) happens in other circumventions :
http://www.blog.magepsycho.com/backup-wordpress-project-files-db-using-bash-script/
written between 2013 and now
_________________________________________ SQL SESSION
This is how it looks without --databases added infront of the dbname:
106 Connect db483458@localhost as anonymous on
106 Query /*!40100 SET @@SQL_MODE='' */
106 Query /*!40103 SET TIME_ZONE='+00:00' */
106 Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND L
OGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITI
ONS WHERE TABLE_SCHEMA='' AND TABLE_NAME IN ('db483458'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE ORDER BY LOGFILE_GROUP_NAME
106 Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE
_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='' AND TABLE_NAME IN ('db483458')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME
106 Quit
forget this part :
"4. In the process of trying this out, i found out, that --password=dbpassword ...."
was a different error.
unfortunately, I wasn't able to reproduce this issue with Python (which I am familiar with) and mariadb-10.0.25 (current version of MariaDB in Fedora 23).
Could You please provide me with the sample Java code (which I am not as much familiar with) for me to be able to reproduce it?
Thank You,
but for now, consult this website :
http://marius.bloggt-in-braunschweig.de/2016/04/29/solution-mysqldump-no-database-selected-when-selecting-the-database/
It explains it in detail. you may have to use google translate, but it has an english section.
System.out.println("MYSQLDUMP gestartet für User "+myJobId+" "+dbname+" am "+ new Date().toString() );
String cmd = ".....chrootwrap "+ Username +" /usr/bin/mysqldump --add-locks -e --force -R --triggers --add-drop-table --hex-blob -h localhost --password="+dbpass+" --user="+dbuser+" --databases "+dbname;
String r = dos.readPipe( cmd ) +"\n\n";
The above line of code is already the workaround version with --databases .
where DOS contains:
public String readPipe(String s) {
return readPipe(s,null);
public String readPipe(String s,String stdin) {
String output = "";
Process process;
try {
Runtime task = java.lang.Runtime.getRuntime();
if ( s.indexOf(" ")>0) {
StringHash params = new StringHash();
int c = 0;
while ( s.indexOf("\"")>=0 ) {
// System.out.println( "in="+s ) ;
String arg = zwischen( s , "\"", "\"" ); // extracts text between " and "
params.put("<ARG"+c+">", arg );
s = s.replaceAll( "\""+arg+"\"", "<ARG"+c+">" );
// System.out.println( "out="+s ) ;
String[] cmds = s.split(" ");
for(int i=0;i<cmds.length;i++) {
for(int a=0;a<=c;a++) cmds[i] = cmds[i]. replaceAll( "<ARG"+a+">", params.get( "<ARG"+a+">" ) );
// System.out.println(i +". => "+cmds[i] );
process = task.exec(cmds);
} else process = task.exec(s);
if ( stdin != null ) {
BufferedWriter out = new BufferedWriter( new OutputStreamWriter(process.getOutputStream()) );
out.write(stdin+"\n");
out.flush();
out.close();
String lines ="";
BufferedReader ins = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((lines = ins.readLine())!=null) {
output += lines+"\n";
process.waitFor();
ins = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((lines = ins.readLine())!=null) {
output += lines+"\n";
while ((lines = ins.readLine())!=null) {
output += lines+"\n";
process.destroy();
} catch (Exception e) {
return e.toString();
return output;
And chrootwrap does this :
chdir("/opt/root/");
chroot("/opt/root/");
execve( argv[2], newargv, environ);
printf( "EXEC-Error" );
return 0;
It rearranges the argumentlist into "newargv", drops to a user arg[0] and executes the tool the cmdlines arg[1] is pointing to.
-rwsr-x--- 1 root root
hope that helps now..