Setting connection variables w/ sqlalchemy? [duplicate] - sqlalchemy

I want to set the general_log and general_log_file variables using SQLAlchemy, is there a way to do this? I've been Googling around and can't find anything on the topic.

You can execute any raw SQL query which you need (of course you have to get appropriate rights in the session). To change a variable run something like this:
# change variable name and values to what you need
connection.execute("SET SESSION query_cache_type = OFF")

As mentioned previously, you could use the following code the set a variable using the raw Connection object.
connection.execute("SET SESSION query_cache_type = OFF")
If you have a Session object, you can retrieve the underlying Connection object using the Session.connection() function.
So your code could look as follows.
session.connection().execute("SET SESSION query_cache_type = OFF")

Related

System variable change doesn't work [duplicate]

I ran the command as root:
set ##auto_increment_offset = 2;
But the effect cannot be seen from other connections. Why not? It is global.
From http://dev.mysql.com/doc/refman/5.1/en/replication-options-master.html:
"If the global value of either variable is set, its effects persist until the global value is changed or overridden by setting the session value, or until mysqld is restarted."
That doesn't seem to agree with what I am seeing.
Ultimately, I would like to know if there any way to permanently set the offset for all clients without restarting mysqld?
As per MySQL documentation you need to set values of auto_increment_offset for both GLOBAL and SESSION.
SET GLOBAL auto_increment_offset = 2;
SET SESSION auto_increment_offset = 2;
SHOW VARIABLES LIKE '%auto_increment_offset%';
If the global value of either variable is set, its effects persist until the global value is changed or overridden by setting the session value, or until mysqld is restarted. If the local value is set, the new value affects AUTO_INCREMENT columns for all tables into which new rows are inserted by the current user for the duration of the session, unless the values are changed during that session.
To set it globally you should add prefix 'GLOBAL' or '##global.'. For example -
SET ##GLOBAL.auto_increment_offset = 2;
The '##' is the same as 'SESSION' or '##session.', it sets session variable.
Using System Variables.

Alternative to MySQL's GROUP_CONCAT function

I'm retrieving the data with MySQL function called "GROUP_CONCAT()".
But when I checked the result of "GROUP_CONCAT()" function related column, it was missing some data.
When I google the record missing issue with "GROUP_CONCAT()" function, in the official MySQL site they have mentioned as,
There is a global variable called group_concat_max_len and it will permit the maximum result length in bytes for the GROUP_CONCAT() function, the default value of it as 1024.
Therefore it seems I have to increase that value with following MySQL command,
SET GLOBAL group_concat_max_len = 1000000;
Therefore set this value permanently, I have to edit the MySQL server related configuration file (my.cnf or my.ini) and have to restart the server.
But unfortunately I haven't any permission to do so.
Therefore can you please help me to find out some alternative solution to fix this issue.
Thanks a lot.
Use SET SESSION instead:
SET SESSION group_concat_max_len = 1000000;
Unlike SET GLOBAL, SET SESSION does not require super privilege.
Reference

Pass custom variables in MySQL connection

I am setting up a MySQL connection (in my case PDO but it shouldn't matter) in a REST API.
The REST API uses an internal authentication (username / password). There are multiple user groups accessing the REST API, e.g. customers, IT, backend, customer service. They all use the same MySQL connection in the end because they also use the same end points most of the time.
In the MySQL database I would like to save the user who is responsible for a change in a data set.
I would like to implement this on the MySQL layer through a trigger. So, I have to pass the user information from the REST API to this trigger somehow. There are some MySQL calls like CURRENT_USER() or status that allow to query for meta-information. My idea was to somehow pass additional information in the connection string to MySQL, so that I don't have to use different database users but I am still able to retrieve this information from within the trigger.
I have done some research and don't think it is possible, but since it would facilitate my task a lot, I still wanted to ask on SO if someone did know a solution for my problem.
I would set a session variable on connect.
Thanks to the comment from #Álvaro González for reminding me about running a command on PDO init.
The suggestion of adding data to a temp table isn't necessary. It's just as good to set one or more session variables, assuming you just need a few scalars.
$pdo = new PDO($dsn, $user, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET #myvar = 'myvalue', #myothervar = 'othervalue'"
]);
It's also possible to set session variables at any time after connect, with a call to $pdo->exec().
$pdo->exec("SET #thirdvar = 1234");
You can read session variables in your SQL queries:
$stmt = $pdo->query("SELECT #myvar, #myothervar");
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
print_r($row);
}
You can also read session variables in triggers:
CREATE TRIGGER mytrig BEFORE INSERT ON mytable
FOR EACH ROW
SET NEW.somecolumn = #myvar;

How to fix mysql uppercase query in php and mysql

I am currently working on the website that uses ADODB library. In entire website all the queries are written in UPPERCASE.
The problem is when I run the query it doesn't work because of table name which is UPPERCASE. But when I change the table name to lowercase it works.
$sql = "SELECT * FROM MEMBERS where USERNAME = '$username'";
$db = ADONewConnection('mysql');
$db->debug = true;
$db->Connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME);
$resultFriends = $db->Execute($sql);
while ($row = $resultFriends->FetchRow()) {
var_dump($row);
die;
}
Here is the error I get:
ADOConnection._Execute(SELECT * FROM MEMBERS where USERNAME = 'fury', false) % line 1012, file: adodb.inc.php
ADOConnection.Execute(SELECT * FROM MEMBERS where USERNAME = 'fury') % line 15, file: index.php
Bear in mind I don't want to change the scripts. There are 1000 files and 10000 places.
Is there any library or are there any way that I can run this queries without error?
The version for live sire was linux kernel. but the new dev site is ubuntu.
I have done this on ubuntu/ mysql CML and it didn't work.
The solution is I had to reconfigure the mySql database in AWS/rdbs
You have to modify the “lower_case_table_names” parameter for your DB Instance(s). Prior to today, the lower_case_table_names parameter was not modifiable, with a system default of zero (0) or “table names stored as specified and comparisons are case sensitive.” Beginning immediately, values of zero and one (table names are stored in lowercase and comparisons are not case sensitive) are allowed. See the MySQL documentation for more information on the lower_case_table_names parameter.
The lower_case_table_names parameter can be specified via the rds-modify-db-parameter-group API. Simply include the parameter name and specify the desired value, such as in the following example:
rds-modify-db-parameter-group example --parameters "name=lower_case_table_names, value=1, method=pending-reboot" --region us-east-1
Support for modifying parameters via the AWS Management Console is expected to be added later this year.
setting the lower_case_table_names parameter via a custom DB Parameter Group and doing so before creating an associated DB Instance. Changing the parameter for existing DB Instances could cause inconsistencies with point-in-time recovery backups and with Read Replicas.
Amazon RDS

How to force MySQL out of TRADITIONAL mode?

I have an old application that started failing after an upgrade from MySQL 5.0 to 5.1.
A bit of research indicated this is due to "strict mode" which prevents inserting certain types of "invalid" values which previously were just automatically converted to something reasonable.
I tried SET ##SESSION.sql_mode = '' and SET ##GLOBAL.sql_mode = '' but I still get the error.
Also tried commenting out sql_mode in the my.ini.
Is there a stronger, sort of "nuclear" option to fix this?
In my application I usually make sure that the MySQL connection is using traditional mode by issuing
SET SESSION sql_mode = 'ANSI_QUOTES,TRADITIONAL'
on each new connection. I presume that if you just issue
SET SESSION sql_mode = ''
on each new connection, you will have solved the problem.
You should be able to change the default SQL mode for new connections by issuing
SET GLOBAL sql_mode = ''
but you must use an account with sufficient privileges to do this or it won't work.
I think that if you want to make sure a particular SQL mode is in operation for your application, the most robust way to do so is to set it for each and every new connection.
To allow invalid dates, you need:
SET sql_mode = 'ALLOW_INVALID_DATES';
But you'd better fix your application.