Google Cloud SQL Incorrect Key File - mysql

On several occasions, I've tried to do a query on Google Cloud SQL that involves an order by statement, only to have the query fail with the error
Incorrect key file for table '/cloudsqltmp/#sql_44f4_1.MYI'; try to repair it
This sounds like the /cloudsqltmp/ partition is filling up with my temporary table. The result set isn't that big, however, and the program doing that query has done so on several other occasions, so I suspect that the space is actually filling up with someone else's temporary table. I was able to clear this by restarting the instance several times (I assume it finally gave me a new machine, or the space cleared up), but this seems very inelegant.
Is there a better way to handle this if it happens again?
Is there a better way to prevent this from happening?
If my assumption of what happened is wrong - what actually happened?

What I would recommend doing is:
Creating a Google Compute Engine instance and run MySQL on it. This way if you run into this problem again, you can use the follow solution below.
1) Modify the /tmp partition to have more space. There is a very useful link here on how to do it.
2) Create a CronJob to clear the /tmp. If you are not comfortable with CronJob, I found another tool called tmpreaper. To install it, run sudo apt-get install tmpreaper. Also use InnoDB instead of MyISIAM. Google recommends using it.
3) You are correct to assume that /tmp is getting full since restarting the instance resolves the issue
I hope my recommendations were helpful.

Related

MYSQL only INSERT query slow (takes exactly 60 sec), only for some tables

I'm new to MYSQL and there is something really weird happened and I can't figure out why.
Recently, the INSERT query to some of the table become extremely slow. Weirdly enough, the query time all around 60 secs.
The tables are all with the only 10k to 35k entries, so I think they are not that big.(But indeed they are the biggest one in the database, though.)
And the slowness is only with INSERT query, DELETE, UPDATE, SELECT are all executed with 0.000x sec.
Can some help me find out why is this happening?
UPDATE: So I turned on the general log and noticed all my INSERT queries are followed with 'DO sleep(60)'. It seems my server got hacked?
Where can I find this malicious script inject the sleep() command after INSERT?
If you use code to build the queries, copy the code base off the server to your machine (ideally in a VM, just in case) and search for the changes within the code. Alternatively, you could restore the code base from source control (you use source control, right?!).
If it's store procedures you use, you'll need to change them back to a working version without the sleep. Check previous backups to try and find out when this happened, which might help a wider investigation as to how they got in and did what they did.
You'll also need to think about the wider implications of this. Do you store user data? If so, then you'll need to inform them that you've had your database compromised and therefore they should assume their accounts are and change their passwords.
Finally, wipe the server. A hacked server is no longer in your control (or that's how you should look at it). Wipe it, reinstall everything, and put in changes to help prevent the same hack happening again.

Table In Use - MySQL

I have a serious issue with an 'in use' table.
An admin for the site attempted to back up the database with mysqldumper but an important table remains in use. The entire site is down.
Have tried to repair the table from CPanel and PHPMyAdmin but no luck. The same file not found error is returned.
The site is ran through rented Web host so shell might be out of the question.
Have tried just about everything without luck.
I appreciate that it's not about to go on, any ideas on this one please?
Run this command in the Database directory:
recover --safe-recover --force [Table Name]
Depending on table size, this can take a long time. On large tables it's much faster to restart the MySql service. That sometimes works, though it did not work for the OP.
(Yes, I know it's a year late. Traffic was terrible.)

Run large sql script

I have just reloaded my laptop and am now trying to setup my localhost again.
Currently I am trying to re-setup the database.
the issue is,
The script is 169,328 KB.
This keeps crashing whatever I use to run the query and I get the error: The mysql Server has gone away.
Seems everyone is suggesting splitting the script. Instead as this is only me setting my localhost back up, I have temporarily increased the max_packet_size.
This would explain the error.
Perhaps you should open the script and see if you can chunk it into smaller, more manageable pieces.
I don't know what you're doing about transactions, but perhaps the rollback segment (or its MySQL equivalent) is getting too large. If that's the case, break the script into several transactions that you can safely commit individually.
If you're looking to avoid the err message, consider one of these remedies:
ensure that your environment or commands aren't causing this issue. Causes for MySQL gone-away.
split your large script into smaller scripts. You could then run those in sequence.

Writing a MySQL Plugin

I am currently looking into writing a plugin for mysql in C. I have been reading and studying MySQL 5.1 Plugin Development (http://www.amazon.com/MySQL-Plugin-Development-Sergei-Golubchik/dp/1849510601) which has been helping me out a lot. However I can't seem to find any examples that execute queries. I tried Googling for one but couldn't find any relevant examples. My goal for the time being is to write just a simple plugin that after some data is entered into a table will just perform some basic select queries. If anyone would be willing to share a link to such an example or provide one, it would be most appreciated.
I haven't used Mysql with C before, however you might want to take a look at these links:
http://dev.mysql.com/doc/refman/5.1/en/c-api-function-overview.html
http://www.mysql.com/downloads/connector/c/
If you know MySql I think those links would be a good starting point at least.
As someone who's written a storage engine plugin, I've found that executing a query from within a MySQL plugin is incredibly difficult. MySQL isn't re-entrant, due to locking within the MySQL process. You could use the MySQL client api (as suggested by chris) to connect to the same server. But it is 99% likely you will simply deadlock the whole server.
I do not know if this might help, but I have created a small mysql plugin written in C. The plugin basically sets up a function which when triggered from mysql will send information about a remove, insert or update query to a running node socket server.
You can find the repository here:
https://github.com/Cyclonecode/mysql-notification
It sounds to me like it's not a good idea to try to execute queries within a storange engine, because this introduces re-entrancy that the server doesn't handle.
However, it is possible that you could do it from a daemon thread, as the event scheduler already does. Likewise, Handlersocket does something similar (but uses the handler API rather than executing queries).
In any case, it all sounds a bit fishy. If you are just learning, try writing a few UDFs (they're easy).

Mysql Table crashed. Should I repair daily?

Out of blue moon, all of sudden, 1 of my database is crashed. This is not the first time, last time I use the "repair table" command and was lucky able to fix it. But it happens again, same table, same error, same solution.
Error:
1194: Table 'users' is marked as crashed and should be repaired
Do I need to repair my tables every day/week/month? Is there a permanent solution for "table-crashing" problem? Its really scary!!! Please help
This shouldn't happen normally, if your tables are crashing it means something is wrong with your system. Likely a bad disk or bad DRAM (or one of lots of unlikely things, like bad pci->sata bridge, etc...). If you have another system around, try migrating to that system and see if your errors continue.
You do have another equivalent system around, right? In case the primary DB crashes hard, and you need to restore a backup ASAP?
I suppose it could also be a bug in mysql, make sure the version you are using doesn't have known bugs.
To debug this problem, let alone just about any other problem, start looking in your logs. If you MySQL server runs on Windows, there are event logs for application and system, and I would look at those. If your MySQL server runs on Linux, logs are usually found under /var/log, and you usually have to be root to view them. A good way to view on linux is
tail (where is replaced by the real name of the file).
As you have been advised in another answer, you might have disk issues. Run a hardware diagnostic on the disks.
Whatever you decide to do, please do not depend on repairing a table; because you soon might not be able to. Using an Informix SE database, I had to rebuild a table, and there was nothing apparently wrong with the data in the table. It was a nightmare, and took the better part of a weekend. I could not export the database, on which our test and development systems depend.