MySQL batch-file call flush tables for export - mysql

I use batch-file for copy database from server1 to server2.
Step 1: call stored procedure for FLUSH TABLES table1,table2, ..., table1000 FOR EXPORT;
Step 2: copy files .ibd and .cfg to temp directory and archive this
Step 3: unlock tables;
The problem is the first step - files .cfg are created and then removed, but unlock the tables is not called. Why? Files .cfg are created and immediately disappear, I do not have time to copy
.bat file command:
mysql -u %db_user% -p%db_password% %db_name% --default-character-set=utf8 < stored_proc_flush_tables.sql
file stored_proc_flush_tables.sql:
DROP PROCEDURE IF EXISTS stored_proc_flush_tables;
DELIMITER //
CREATE PROCEDURE stored_proc_flush_tables
(
)
BEGIN
DECLARE t_name BLOB;
DECLARE tmp_query BLOB;
DECLARE done_tables INT DEFAULT 0;
DECLARE cursor_tables CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_schema=DB_NAME;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_tables = 1;
SET #table_name = '';
SET #tmp_query = '';
OPEN cursor_tables;
tables_loop: LOOP
FETCH cursor_tables INTO t_name;
IF done_tables = 1 THEN
LEAVE tables_loop;
END IF;
SET #tmp_query = CONCAT_WS('', #tmp_query, ',', t_name);
END LOOP;
CLOSE cursor_tables;
SET #tmp_query = TRIM(LEADING ',' FROM #tmp_query);
SET #tmp_query = CONCAT_WS('', 'FLUSH TABLES', ' ', #tmp_query, ' ', 'FOR EXPORT');
PREPARE stmt FROM #tmp_query;
EXECUTE stmt;
END //
DELIMITER ;
call stored_proc_flush_tables();
Files .cfg are created and immediately disappear, I do not have time to copy them

Problem is that you end mysql session that makes FLUSH TABLES ... FOR EXPORT
before you try to copy files.
When mysql session/connection ends all locks unlocked and *.cfg is consired as temporal file is deleted.
So you should have program that makes FLUSH ... FOR EXPORT and keeps session
open and then copies files and after that releases table lock (or ends session).

Related

How to use DELIMITER in mariadb using the .NET MySql connector?

I'm using MariaDB 10.2.12 and connecting using the .NET MySQL connector. The following trigger works fine in MySQL Workbench:
DELIMITER //
CREATE TRIGGER update_last_modified
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
DECLARE miscdataWithDate JSON;
IF JSON_CONTAINS_PATH(NEW.miscdata, 'all', '$.v1.lastModified2') THEN
SET NEW.miscdata = JSON_REPLACE(NEW.miscdata, '$.v1.lastModified2', UTC_TIMESTAMP());
ELSE
SET miscdataWithDate = JSON_SET('{"v1": {}}', '$.v1.lastModified2', UTC_TIMESTAMP());
SET NEW.miscdata = JSON_MERGE(NEW.miscdata, miscdataWithDate);
END IF;
END; //
DELIMITER ;
To run the command from C#/.NET, I used the following. I tried it with and without the final semicolon, in case the library is adding a semicolon:
using (var cmd = new MySqlCommand(#"CREATE TRIGGER update_last_modified
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
DECLARE miscdataWithDate JSON;
IF JSON_CONTAINS_PATH(NEW.miscdata, 'all', '$.v1.lastModified') THEN
SET NEW.miscdata = JSON_REPLACE(NEW.miscdata, '$.v1.lastModified', UTC_TIMESTAMP());
ELSE
SET miscdataWithDate = JSON_SET('{""v1"": {}}', '$.v1.lastModified', UTC_TIMESTAMP());
SET NEW.miscdata = JSON_MERGE(NEW.miscdata, miscdataWithDate);
END IF;
END; //
DELIMITER ;", connection))
{
await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
}
When the trigger is defined (not called), the error is:
Unhandled Exception: System.AggregateException: One or more errors occurred. (You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '//
DELIMITER' at line 1) ---> MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '//
DELIMITER' at line 1
If I simplify the query so it doesn't need DELIMITER set, it works. But even a very simple trigger with a custom delimiter fails.
When searching for how other people have successfully used delimiters with MySQL/MariaDB from .NET, I found the following article: https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-mysqlscript-delimiter.html
The example given uses MySqlScript instead of MySqlCommand, and so I believe that MySqlCommand simply doesn't support delimiters. Here is the updated code, which works fine:
MySqlScript script = new MySqlScript(connection, #"CREATE TRIGGER update_last_modified
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
DECLARE miscdataWithDate JSON;
IF JSON_CONTAINS_PATH(NEW.miscdata, 'all', '$.v1.lastModified') THEN
SET NEW.miscdata = JSON_REPLACE(NEW.miscdata, '$.v1.lastModified', UTC_TIMESTAMP());
ELSE
SET miscdataWithDate = JSON_SET('{""v1"": {}}', '$.v1.lastModified', UTC_TIMESTAMP());
SET NEW.miscdata = JSON_MERGE(NEW.miscdata, miscdataWithDate);
END IF;
END; //");
script.Delimiter = "//";
await script.ExecuteAsync().ConfigureAwait(false);

ErrCode with "Select Into Outfile with a variable" - Confusing Permissions

Background: I work with phpMyAdmin (MySQL Workbench) in a mysql DB. I write some PHP code to import data in the DB and execute this with the task scheduler of windows. <= this works fine!
Now I want to export some data into a file in a Windows folder. At first I write the SQL code in phpMyAdmin to see some debug-infos. <= this is where the problem occurs.
My Topic:
I want to export some columns of my DB. My Goal is to put a variable CURRENT_TIMESTAMP in the filename. For this I use the Concat statement.
My code (posted below), gets the result of the following error:
Can't create/write to file 'C:\Temp\Export\2018-08-08 09:21:27.txt' (Errcode: 13 "Permission denied")
Funny thing is, if I replace the variable CURRENT_TIMESTAMP with e.g. "Hello World" there is no error and my file is created in the folder.
Here is my code:
*set #sql = concat("SELECT `LS_ID_Nr`,
`Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL`,
`Umstellzeit`,
`Produktionszeit`,
`Teilmeldung`,
`Fertigmeldung` INTO OUTFILE 'C:/Temp/Export/",CURRENT_TIMESTAMP,".txt' fields terminated by ';' lines terminated by '\r\n' From praemie where Proof_P = 0");
prepare s1 from #sql;
execute s1;
DROP PREPARE s1;
UPDATE praemie SET Proof_P = 1 WHERE Proof_P = 0;*
Does anybody have an idea why there is an Permission Error with the use of a variable? Thanks in advance!
Ooooohh....
I get it now!
The problem is, that windows not handle ":" in filenames. So I have to edit the code with the Date_Format statement like this:
set #sql =
concat("SELECT `LS_ID_Nr`, `Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL`,
`Umstellzeit`, `Produktionszeit`, `Teilmeldung`, `Fertigmeldung`
INTO OUTFILE 'C:/Temp/Export/Test - ", DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')," - Test.txt'
fields terminated by ';'
lines terminated by '\r\n'
From praemie where Proof_P = 0")

Delphi 2010 : UniDAC vs Indy-MultiThread safety handleing method

I am doing develop Indy based application.
Server has several Indy TCP Server components.
So It works under multi-threads and handles mysql db.
I have faced one problem.
That is about the exceptions of MySQL DB in threads.
When serveral threads attack to same db table, then It says me like follows
UniQuery_Mgr: Duplicate field name 'id'
UniQuery_Mgr: Field 'grp_id' not found //of course grp_id field is really existed.
Assertion failure (C:\Program Files (x86)\unidac539src\Source\CRVio.pas, line 255)
Commands out of sync; You can't run this command now
ReceiveHeader: Net packets out of order: received[0], expected[1]
UniQuery_Mgr: Cannot perform this operation on a closed dataset
How to do I ? UniQuery_Mgr is TUniQuery component.
and my query handling code is normally like this
Code 1
sql := 'SELECT * FROM data_writed;';//for example
UniQuery_Mgr.SQL.Clear;
UniQuery_Mgr.SQL.Add(sql);
UniQuery_Mgr.ExecSQL;
Code 2
try
sql := 'SELECT * FROM gamegrp_mgr;';
UniQuery_Mgr.SQL.Clear;
UniQuery_Mgr.SQL.Add(sql);
UniQuery_Mgr.ExecSQL;
if UniQuery_Mgr.RecordCount > 0 then
begin
MAX_GAME_GROUP_COUNT := UniQuery_Mgr.RecordCount + 1;
UniQuery_Mgr.First;
i := 1;
while not UniQuery_Mgr.Eof do
begin
Game_Group_ID[i] := UniQuery_Mgr.FieldByName('grp_id').AsInteger;
Game_Game_ID[i] := UniQuery_Mgr.FieldByName('game_id').AsInteger;
UniQuery_Mgr.Next;
Inc(i);
end;
end;
except
on E : Exception do
begin
EGAMEMSG := Format('GAME group read error: <%s> # %s',[ E.ToString, DateTimeToStr(now)]);
Exit;
end;
end;
Code 3
try
sql := 'UPDATE data_writed SET write_gamegrp = ' + QuotedStr('0') + ';';
UniQuery_Mgr.SQL.Clear;
UniQuery_Mgr.SQL.Add(sql);
UniQuery_Mgr.ExecSQL;
except
on E : Exception do
begin
EGAMEMSG := Format('data updating error: <%s> # %s',[ E.ToString, DateTimeToStr(now)]);
Exit;
end;
end;
My handling DB components is bad ? Other thread-safe method is existed???

mySQL function behaves differently on 2 servers which are essentially the same

Allright, here is a hard one...
I have a development server with mySQL 5.1.73 on which I wrote a function to normalize a string for searching purposes.
When moving the function to the production environment, same mySQL version, same major OS version (CentOS 6.5) newest patches, same major kernel version etc. The function stopped working.
Here is the function
CREATE DEFINER=`user`#`%` FUNCTION `normalize`(str VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE normstring VARCHAR(255);
DECLARE i INT;
SET i = 0;
SET normstring = '';
SET str = lower(str);
loop1: WHILE i < length(str) DO
CASE substring(str,i,1)
WHEN 'ä' THEN SET normstring = concat(normstring,'ae');
WHEN 'ö' THEN SET normstring = concat(normstring,'oe');
WHEN 'ü' THEN SET normstring = concat(normstring,'ue');
WHEN 'ß' THEN SET normstring = concat(normstring,'ss');
WHEN '/' THEN SET i = i + 1; ITERATE loop1;
WHEN '.' THEN SET i = i + 1; ITERATE loop1;
WHEN '-' THEN SET i = i + 1; ITERATE loop1;
WHEN '(' THEN SET i = i + 1; ITERATE loop1;
WHEN ')' THEN SET i = i + 1; ITERATE loop1;
WHEN ' ' THEN SET i = i + 1; ITERATE loop1;
WHEN '\'' THEN SET i = i + 1; ITERATE loop1;
WHEN '\\' THEN SET i = i + 1; ITERATE loop1;
ELSE SET normstring = concat(normstring,substring(str,i,1));
END CASE;
SET i = i + 1;
END WHILE;
RETURN normstring;
END$$
DELIMITER ;
On the development server this converts 'Mönßtär' to 'moensstaer', but on the production server it converts it to 'mönßtä'
Changing
SET i = 0; and WHILE i < length(str)
to
SET i = 1; and WHILE i <= length(str)
corrects the missing last character, so the result is 'mönßtär' but one server should not start counting with 0 the other one with 1, right?
And the production server leaves all special characters untouched.
I have compared all global variables, not only those explicitly set in my.cnf, and except timezone, password and symlink setings they are equal (yes I should correct those differences, but that should have nothing to do with my problem, right?)
Are there some compile-settings which can influence this behaviour, or some external libraries that mySQL uses?
I'll probably have to find a workaround for the problem - I plan to normalize in the application rather than the database - the function is too slow in large queries anyway - but it would have been nice to convert the existing data in the database. But I'm really curious as to what causes such strange behaviour.
Character-Set settings on both servers (from the running environment):
character_set_client........................ utf8
character_set_connection.................... utf8
character_set_database...................... utf8
character_set_filesystem.................... binary
character_set_results....................... utf8
character_set_server........................ utf8
character_set_system........................ utf8
collation_connection........................ utf8_unicode_ci
collation_database.......................... utf8_unicode_ci
collation_server............................ utf8_unicode_ci
I believe you have different character set between your local environment and production. Look into these articles on how to detect How do I find out the default server character set in mysql? and how to change Change server variable character_set_server.

SQL - shrink database log file

I am trying to shrink my database log file. I have tried to run:
USE databasename
BACKUP log databasename
WITH truncate_only
DBCC shrinkfile (databasename_log, 1)
I get the error message:
Msg 155, Level 15, State 1, Line 3
'truncate_only' is not a recognized
BACKUP option.
Am I missing something?
SQL Server 2008 no longer allows the NO_LOG / TRUNCATE_ONLY options.
To truncate your transaction log, you either have to back it up (for real) or switch the database's Recovery Model to Simple. The latter is probably what you really want here. You don't need Full recovery unless you are making regular transaction log backups to be able to restore to some point mid-day.
I think the best way is to use a script like this:
USE AdventureWorks
GO
-- Use some dynamic SQL just only not to re-write several times the name
-- of your db, or to insert this snippet into a loop for all your databases...
DECLARE #dbname varchar(50) = 'AdventureWorks';
DECLARE #logFileName varchar(50) = #dbname + '_log';
DECLARE #SQL nvarchar(max);
SET #SQL = REPLACE('ALTER DATABASE {dbname} SET RECOVERY FULL;', '{dbname}', #dbname);
EXECUTE(#SQL);
DECLARE #path nvarchar(255) = N'F:\BCK_DB\logBCK' + CONVERT(CHAR(8), GETDATE(), 112) + '_'
+ REPLACE(CONVERT(CHAR(8), GETDATE(), 108),':','') + '.trn';
BACKUP LOG #dbname TO DISK = #path WITH INIT, COMPRESSION;
DBCC SHRINKFILE(#logFileName);
-- determine here the new file size and growth rate:
SET #SQL = REPLACE('ALTER DATABASE {dbname} MODIFY FILE (NAME = ' + #logFileName + ', SIZE = 32000MB, FILEGROWTH = 10%);',
'{dbname}', #dbname);
EXECUTE(#SQL);
GO
http://www.snip2code.com/Snippet/12913/How-to-correctly-Shrink-Log-File-for-SQL