MySQL cannot select a row inserted in a different session - php

My question is in the title.
I open a MySQL session in an application, and that application calls a script that creates another session. I insert a record in that new session, but when I select in the previous session, I cannot see the inserted row.
The following is the insert query:
INSERT INTO VCS.project_files (track_nbr, path, filename)
VALUES ('$track_nbr', '$path', '$filename');
The following is the select:
SELECT pf.project_file_id, pf.track_nbr, pf.filename, pf.path, pf.status
FROM VCS.project_files pf;

I found out this was due to isolation level set to 'repeatable reads'. 'Repeatable reads' means exactly what it says, each read in the old session will get the exact same rows.
In order to fix this, I had to set the database to 'read committed' by doing the following:
Adding the following lines to /etc/my.cnf (Ubuntu) /etc/my.cnf.d/my.cnf (CentOS):
[mysqld]
binlog_format = row
transaction-isolation = READ-COMMITTED
Restarting MySQL (in Ubuntu):
sudo service mysql restart
This allows all sessions to see committed changes from other sessions.
I was used to Oracle database ('read committed' by default) and MySQL is different ('repeatable reads' by default).

Related

MySQL: ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails - but only for 1 sql file

I'm trying to import data from sql files into local MySQL database. I'm using WampServer2.5, MySQL 5.6.17. I'm importing them using MySQL console as PHPMyAdmin interface was causing some problems.
First two files seem to be uploaded correctly. However, they were much smaller than the third one. The steps are always the same:
1. create database xyz;
2. use database xyz;
3. SET FOREIGN_KEY_CHECKS = 0;
4. source path/to/the/xyz.sql
When uploading the third file at some point I get this errors (a couple in a row, then it keeps uploading):
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: [some number]
Current database: xyz
Then a get a few of those, which is strange as I set that in the beginning (maybe there is some reset of this setting when connection is lost?):
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
The result is I have all the tables uploaded, but some of them have no data - mostly because they are referencing user table from that third database, which has no records.
Ideas / questions:
I tried to edit php.ini file to increase memory limit, max file upload size but there is no effect on PHPMyAdmin interface
Can I find only one table in sql file and upload the records to the table in my MYSQL database?
The table was updated after I opened the full sql file i Notepad++, found only lines with inserted values into that particular table, saved it as a new sql file and used 'source' command then.

How to update multiple rows in a php loop which throws a time out 504

I am trying to migrate customers to a new database structure.
I have written a PHP script which loops through every record and determines if that customer has duplicates in the database or not. If there are multiple occurrences, I update all other tables linked to that customer, and update their linked customerID to the latest version of that customer.
The issue I am facing is that running this script by loading migrate.php in a browser takes so long that I get a timeout 504 when running it, even if I run the query for customers names starting with 'A'. Then change to 'B' and run the script, then 'C' and run the script etc... (which would take forever to do anyways).
So my question is: How can I run around 120,000 UPDATE queries either within the PHP loops in the migrate.php page and avoid a 504 timeout (currently set to limit of 5min), or by printing the Queries out, and running them through PHPMyAdmin?
I have printed out (snippet):
BEGIN;
UPDATE renewal_reminder SET renewalid='49884' WHERE renewalid='43239';
UPDATE renewal_note SET renewalid='49884' WHERE renewalid='43239';
UPDATE renewal_request SET renewalid='49884' WHERE renewalid='43239';
UPDATE renewal_timeline SET renewalid='49884' WHERE renewalid='43239';
COMMIT;
But this still kills PHPMyAdmin and returns an error.
Can this large list of UPDATES be used within PHPMyAdmin to run the script, maybe in batches?
UPDATE
Well...
I ended up indexing the customerID in each table that references it, and I ran my original script with the UPDATE queries and it did it in about 5 seconds...
Without the need to INSERT, then copy over the data.
So for anyone with the same problem, try indexing certain columns in your table so your script can reference them quickly.

Query not working in Mysql database

I am having serious headache with MySQL database. Here is my problem.
I am developing a website in Php with SQL Server as database, everything working fine. database 1
Than I have to change database to MySQL, I've done that and create some stored procedures and function, everything working fine here too. database 2
Now I've have to shift MySQL database to another MySQL server, database 3. Problem occurs here because now my old stored procedure and query which I've created are not working. My DB3 structure is same as DB2.
If I fire this query in DB2 select * from Online_Patient; no error.
Same query in DB3 throws an error:
Error Code: 1146. Table 'Online_Patient' doesn't exist
But when I change my query to select * from online_patient; it gives me correct result.
So is there any way to solve this error or do I have to change all query and stored procedures, there are dynamic query in stored procedure so just can't convert upper case to lower case.
Locate your mysql configuration file. It should be named my.cnf. You can run mysqld --help --verbose in the beginning of the output the are the default locations where you can look for the file. My output is:
Default options are read from the following files in the given order:
C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf C:\xampp\mysql\my.ini C:\xampp\mysql\my.cnf
Find the lower_case_table_names directive and set it to 1:
lower_case_table_names = 1
From MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_lower_case_table_names
If set to 0, table names are stored as specified and comparisons are case sensitive. If set to 1, table names are stored in lowercase on disk and comparisons are not case sensitive. If set to 2, table names are stored as given but compared in lowercase. This option also applies to database names and table aliases. For additional information, see Section 10.2.2, “Identifier Case Sensitivity”.
Then restart mysql server.

error while inserting null data in mysql phpmyadmin

Whenever I am inserting any blank data in my table it works on XAMPP but it does not work on my IIS server having PHP version 5.3 and MySQL version 5.5 I found error. What setting should I do in my configuration file so that any null value and blank data can be inserted, not only that if my table has five columns and if in one query page only two data is to be inserted rest is not written then also it shows error which is
insert failed Field 'XXX' doesn't have a default value
insert failed Data truncated for column 'Man_Days' at row 1
Is there any setting changes which can be done in my.ini or php.ini or config.inc.php
Firstly You must try to avoid inserting blank data in table. if it's not possible then try to insert the field which have value. So You may try to use Your mysql insert query like below:-
insert into table_name (field1,field3) values(value1,value2);

How to run multiple sql queries using php without giving load on mysql server?

I have a script that reads an excel sheet containing list of products. These are almost 10000 products. The script reads these products & compares them with the products inside mysql database, & checks
if the product is not available, then ADD IT (so I have put insert query for that)
if the product is already available, then UPDATE IT (so I have put update query for that)
Now the problem is, it creates a very heavy load on mysql server & it shows a message as "mysql server gone away..".
I want to know is there a better method to do this excel sheet work without making load on mysql server?
I am not sure if this is the case, but judging from your post, I assume it could be the case that for every check you initilize a new connection to the MySQL server. If that indeed is the case you can simply connect once before you do this check, and run all future queries trought this connection.
Next to that a good optimization option would be to introduce indexes in MySQL that would significantly speed up product search, introduce index for those product table columns, that you reference most in your php search function.
Next to that you could increase MySQL buffer size to something above 256 MB in order to cache most of the results, and also use InnoDB so you do not need to lock whole table every time you do the check, and also the input function.
I'm not sure why PHP has come into the mix. Excel can connect directly to a MySql database and you should be able to do a WHERE NOT IN query to add items and a UPDATE statements of ons that have changed Using excel VBA.
http://helpdeskgeek.com/office-tips/excel-to-mysql/
You could try and condense your code somewhat (you might have already done this though) but if you think it can be whittled down more, post it and we can have a look.
Cache data you know exists already, so if a products variables don't change regularly you might not need to check them so often. You can cache the data for quick retrieval/changes later (see Memcached, other caching alternatives are available). You could end up reducing your work load dramatically.
Have you seperated your mysql server? Try running the product checks on a different sub-system, and merge the databases to your main, hourly or daily or whatever.
Ok, here is quick thought
Instead of running the query, after every check, where its present or not, add on to your sql as long as you reach the end and then finally execute it.
Example
$query = ""; //creat a query container
if($present) {
$query .= "UPDATE ....;"; //Remember the delimeter ";" symbol
} else {
$query .= "INSERT ....;";
}
//Now, finally run it
$result = mysql_query($query);
Now, you make one query at the last part.
Update: Approach this the another way
Use the query to handle it.
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;
Reference

Categories