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
Related
I am using project deployment. I have several project parameters. My packages only use project-level parameters, and no package-level ones. I have programatically deployed my project and set an environmental reference:
I call each package from a SQL Agent job. I am unable to link my environment variables to the package when it runs. I have successfully linked the project to the environment:
But now when I run my agent job, it fails. When I look at the SSISDB reports, it says it "created execution", but shows no variables.
Do I actually have to explicitly link every variable in each package to the environment variable? Why even bother to group them by environment?
I have created my environmental references like this (sql cmd):
EXEC [SSISDB].[catalog].[create_environment_reference] #environment_name='$(ChooseEnvironment)', #reference_id=#reference_id OUTPUT, #project_name='$(ProjectName)', #folder_name='$(folderName)', #reference_type=R
EXEC SSISDB.catalog.set_object_parameter_value #parameter_name=N'EmailFrom', #parameter_value='EmailFrom', #project_name=$(ProjectName), #object_type=20, #folder_name=$(FolderName), #value_type=N'R'
Additional info: I have created a sql agent job that calls each package with a job step like this:
set #cmd = N'/ISSERVER "\"\SSISDB\CHAT\SSISPackages\Chat_Load_RMS_InputFiles.dtsx\"" /SERVER "\"' + #TargetDBServer + '\"" /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT /REPORTING E'
EXEC #ReturnCode = msdb.dbo.sp_add_jobstep #job_id=#jobId,
#step_name=N'PACKAGE: Chat_Load_RMS_InputFiles.dtsx',
#step_id=1,
#cmdexec_success_code=0,
#on_success_action=3,
#on_success_step_id=0,
#on_fail_action=2,
#on_fail_step_id=0,
#retry_attempts=0,
#retry_interval=0,
#os_run_priority=0, #subsystem=N'SSIS',
#command=#cmd,
#database_name=N'master',
#flags=0
Do I need to add a reference id to my SSIS #cmd variable? Also, if I address this in the job, can I remove my code above to set each project-level variable to an environment, or do I still need that? It seems like for cleanliness, I should just be able to say: this project uses this environment. Done. Otherwise, it's almost like using package-level variables and all the tinkering those require.
If you are running a package as an "direct" SSIS step in SQL Agent step, you have to select the environment in the package configuration tab on the step configuration dialog.
If you running it using TSQL script you need to provide a reference id when calling catalog.create_execution:
DECLARE
#reference_id bigint,
#FullPackageName NVARCHAR(100);
SELECT #reference_id = reference_id
FROM [$(SSISDB)].catalog.environment_references er
INNER JOIN [$(SSISDB)].catalog.projects AS p
ON p.project_id = er.project_id
INNER JOIN [$(SSISDB)].catalog.folders AS f
ON f.folder_id = p.folder_id
WHERE er.environment_folder_name IS NULL
AND er.environment_name = #EnvironmentName
AND p.name = #ProjectName
AND f.name = #FolderName;
IF ##ROWCOUNT = 0
BEGIN
DECLARE
#msg NVARCHAR(100);
SET #msg = N'Could not find a reference for a local (.) ''' + #EnvironmentName + N''' environment.';
THROW 50000, #msg, 1;
END;
SET #FullPackageName = #PackageName + N'.dtsx';
EXEC [$(SSISDB)].catalog.create_execution
#package_name = #FullPackageName,
#execution_id = #execution_id OUTPUT,
#folder_name = #FolderName,
#project_name = #ProjectName,
#use32bitruntime = False,
#reference_id = #reference_id;
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).
I have downloaded and installed MySQL Connector 5.1 x64 so I can use MySQL with Delphi. I can make connection with ODBC and do a connection from my Delphi environment and from MySQL Workbench.
But, when I build my Query at runtime, I get an error saying:
Project AAA.exe raised exception class EOleException with message 'Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another'. Process stopped. Use Step or Run to continue.
My code:
qDates := TADOQuery.Create(Component);
qDates.Connection := FConnection;
qDates.SQL.Text :=
'select ' +
' * ' +
'from ' +
' resulttable ' +
'where ' +
' oid = :oid ' +
' and datedial >= :datebegin and datedial <= :dateend'; // <<-- Exception here
Details:
The exception happens right on the text assignment, before I have a chance to configure parameters.
If I comment out the where clause the assignment goes fine.
This is similar to Using parameters with ADO Query (mysql/MyConnector) but the difference is that I assign whole text at once and I get the exception before I have a chance to configure parameters.
The puzzling part - exact same code works fine on my other machine, but I can not figure out what is different.
Hence the question - what could cause the above exception outside of the Delphi code and MySQL server?
This seems to be a quirk with the MySQL ODBC provider.
If you assign the connection after setting the SQL text, then it will work.
The reason why can be found here.
qDates := TADOQuery.Create(Component);
// do net yet assign TADOConnection to prevent roundtrip to ODBC provider
qDates.SQL.Text :=
'select ' +
' * ' +
'from ' +
' resulttable ' +
'where ' +
' oid = :oid ' +
' and datedial >= :datebegin and datedial <= :dateend';
qDates.Connection := FConnection;
UPDATE
This QC entry explains the exact reason for this problem.
In short, the ADODB unit, patch this line from the RefreshFromOleDB procedure :
Parameter.Attributes := dwFlags and $FFFFFFF0; { Mask out Input/Output flags }
To:
if dwFlags and $FFFFFFF0 <= adParamSigned + adParamNullable + adParamLong then
Parameter.Attributes := dwFlags and $FFFFFFF0; { Mask out Input/Output flags }
I'm trying to get data from MySQL DB into Rstudio-server. My actions are like
mydb = dbConnect(MySQL(), user='user', password='password', dbname='dbname', host='localhost')
query <- stri_paste('select sellings.updated_at AS Up_Date, concat(item_parameters.title, " ", ad_attributes.int_value) AS Class, CONCAT(geos.name, " ", geos.kind) AS place, geos.lon, geos.lat, sellings.price AS price, ((geo_routes.distance*2/1000 + 100)) AS delivery_cost FROM sellings, users, item_parameters, ad_attributes, geos, geo_routes WHERE users.encrypted_password!="" && item_parameters.title="Класс" && sellings.price IS NOT NULL && ad_attributes.int_value IS NOT NULL AND users.id=sellings.user_id AND item_parameters.id=ad_attributes.item_parameter_id AND sellings.id = ad_attributes.ad_id AND sellings.geo_guid = geos.guid AND geos.routable_guid = geo_routes.src_guid AND geo_routes.distance = (SELECT geo_routes.distance FROM geo_routes, geos WHERE geos.guid = sellings.geo_guid AND geo_routes.src_guid = geos.routable_guid AND geo_routes.dst_guid = (SELECT geos.routable_guid FROM geos WHERE geos.name = "Воронеж" && geos.kind = "г")) ORDER BY Up_Date;')
rs = dbGetQuery(mydb, query)
And I get an empty dataframe. But when I do the same with my local DB everything is OK. The query takes a pretty long time, about 3 minutes, but it works properly. Moreover the same query works right from the command line in MySQL. On the server, it takes about 4 seconds. OS of server is Debian 7, OS of local machine is Win 8. Any idea?
Sometimes when querying from the command line the default schema has been set in a previous command. This command doesn't carry over to R so the exact same query from a command line to a R session might not work. Maybe check the dbname.
Insert the below statements in your SQL query
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
It worked for me
I know that there is general_log that logs all queries, but I want to find out which query has an error, and get the error message. I have tried running an error query on purpose, but it logs as a normal query and doesn't report it with error. Any ideas?
There is no functionality in MySQL to do this.
You will have to stick with application-layer logging.
I know this is ancient now, but for anyone having the same issue that lands here from Google, here's my two cents.
If you're using the cli client, you can simply redirect your sterr to a file, and then parse through that.
mysql -u user -p 2> errors.log
It is possible to log error queries with the MariaDB Audit Plugin.
The MariaDB Audit Plugin works for MariaDB, MySQL and Percona Server.
For example, for these queries
select now();
select now()+();
select 9+();
select 'hello';
log seems like this:
20150807 23:00:36,mnv-Satellite-L300D,root,localhost,82,377,QUERY,`test`,'select now()
LIMIT 0, 1000',0
20150807 23:00:37,mnv-Satellite-L300D,root,localhost,82,379,QUERY,`test`,'select now()+()',1064
20150807 23:00:37,mnv-Satellite-L300D,root,localhost,82,382,QUERY,`test`,'select 9+()',1064
20150807 23:00:38,mnv-Satellite-L300D,root,localhost,82,383,QUERY,`test`,'select \'hello\'
LIMIT 0, 1000',0
Last column is return code. 0 is Ok. Else - error.
Even though this question is quite old I hope it will be useful to someone who searched for mysql log error queries or similar terms.
Not too long ago I also required mysqld to log only erroneous queries. I found that mysql-proxy enables you to do that and wrote a small LUA script:
local err_flag = false
function read_query( packet )
if packet:byte() == proxy.COM_QUERY then
local user = proxy.connection.client.username
local host = proxy.connection.client.src.name
if user:lower() == 'someuser' then -- change this to any condition where errors should be logged
proxy.queries:append(1, packet, {resultset_is_needed = true})
proxy.queries:append(2, string.char(proxy.COM_QUERY) .. "SET #last_query = '" .. string.sub(packet, 2) .. "'", {resultset_is_needed = true} )
proxy.queries:append(3, string.char(proxy.COM_QUERY) .. "SHOW WARNINGS", {resultset_is_needed = true} )
end
return proxy.PROXY_SEND_QUERY
end
end
function insert_query(err_t, err_n, err_m)
local query = "INSERT INTO `somedb`.`mysql_error` " .. -- change log destination
"(`date`, `err_num`,`err_type`, `err_message`, `problem_query`, `conn_id`)" ..
" VALUES ( NOW(), " ..
err_n .. "," .. "\"" ..
err_t .."\"" .. "," .. "\"" ..
err_m .. "\"" .. "," ..
"#last_query" .. "," ..
proxy.connection.server.thread_id .. ")"
proxy.queries:append(4, string.char(proxy.COM_QUERY) .. query, {resultset_is_needed = true})
return proxy.PROXY_SEND_QUERY
end
function read_query_result(inj)
local res = assert(inj.resultset)
if inj.id == 1 then
err_flag = false
if res.query_status == proxy.MYSQLD_PACKET_ERR then
err_flag = true
return proxy.PROXY_IGNORE_RESULT
end
elseif inj.id == 2 then
return proxy.PROXY_IGNORE_RESULT
elseif inj.id == 3 then
if err_flag == true then
for row in res.rows do
proxy.response.type = proxy.MYSQLD_PACKET_ERR
proxy.response.errmsg = row[3]
insert_query(row[1], row[2], row[3])
end
return proxy.PROXY_SEND_RESULT
end
return proxy.PROXY_IGNORE_RESULT
elseif inj.id == 4 then
return proxy.PROXY_IGNORE_RESULT
end
end
DDL needed for logging table, adjust somedb.mysql_error to liking, but don't forget to do so in the above LUA script also.
CREATE TABLE `somedb`.`mysql_error` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`err_num` smallint(6) NOT NULL,
`err_type` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`err_message` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`problem_query` varchar(8000) COLLATE utf8_unicode_ci NOT NULL,
`conn_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
To use the script, run
/path/to/mysql-proxy --proxy-lua-script=/path/to/mysql-proxy-log-error-queries.lua
or if that fails (>=v0.9)
/path/to/mysql-proxy --proxy-lua-script=/path/to/mysql-proxy-log-error-queries.lua --plugins=proxy
The proxy runs on port 4040 by default, to test:
mysql -u username -p --host=127.0.0.1 --port=4040
and run some faulty sql.
When everything seems to be in order set the port in your application to 4040 instead of the actual mysqld port and you have mysql error logging on the database level.
Final note: mysql-proxy is beta. Use with caution I guess. Been running here for almost half a year now without problems, however YMMV.
I have tried running an error query on
purpose, but it logs as a normal query
and doesn't report it with error. Any
ideas?
so, you did it wrong. No other idea without the code.
in PHP I'm doing it this way (assume you're using mysql driver):
$res=mysql_query($sql) or trigger_error(mysql_error().$sql);
it will log all erroneous queries if you have log_errors setting on (and you have to)
EDIT:
I see now, you want global level logging,m not application level.
But may be application level will suit you as well?
MariaDB is capable to do this with a plugin https://mariadb.com/kb/en/sql-error-log-plugin/ which is distributed with mariadb.
I tried this yesterday and it works as advertised.
One need to just run:
install plugin SQL_ERROR_LOG soname 'sql_errlog';
and the queries with errors will go to $datadir/sql_errors.log that is on most linux install at /var/lib/mysql/sql_errors.log.