简体   繁体   中英

Why am I able to read a MySQL table with SELECT but I am sometimes unable to INSERT or UPDATE?

I have a MySQL issue which troubles me for long time. Why sometimes a table is able to be SELECT but not able to be UPDATE or INSERT ?

Maybe it is the reason of table lock. But I tried "SHOW OPEN TABLES", it doesn't shows table is locked.

Is there any other reason which will cause this problem?

Why sometimes a table is able to be SELECT but not able to be UPDATE or INSERT?

Most likely because the user you are connecting with does not have UPDATE or INSERT permissions. You can check what GRANTS you have using a command like this in MySQL:

SHOW GRANTS FOR 'user_name'@'localhost';

Be sure to change user_name to the username you are using to connect to the server with. Also be sure localhost matches the host you are connecting to. But that said, MySQL GRANTS can be confusing. For example, I have seen some servers—when I had to clean up someone else's work—where localhost would not work, but 127.0.0.1 would work so you might need to run the above command like this:

SHOW GRANTS FOR 'user_name'@'127.0.0.1';

And if you need to adjust your GRANTS then you could run this; be sure to change the database_name , user_name , localhost & password to match your setup:

GRANT USAGE ON `database_name`.* TO 'user_name'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `database_name`.* TO 'user_name'@'localhost';

The first line GRANT USAGE grants basic usage to the database to the user. The next line grants specific access rights and this is a fairly common stack that allows access to SELECT , UPDATE or INSERT items into the database as well as other sundry items.

That said, based on your comments it seems that this might be an issue where someone—or some code—with access to that database is running a LOCK TABLES statement that would result in the behavior you describe. Tables would be locked while work is done. And then an UNLOCK TABLES would be run afterwards to allow UPDATE and INSERT commands to go through.

If you wish to debug & have server level access you might want to temporarily enable MySQL logging to see what queries are coming through. But remember: Activating the MySQL log is a resource hog & can slow down your server. Only do it during a window when you know you can safely test.

Using an Ubuntu 12.04 serve setup for an example, you would open op the MySQL config file like so:

sudo nano /etc/mysql/my.cnf

Then search for the settings for general_log_file and general_log which should be commented out & look like this:

# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1 

Uncomment both general_log_file and general_log and then restart MySQL like this:

sudo service mysql restart

Now you can follow the MySQL logs like so with tail :

sudo tail -f -n 200 /var/log/mysql/mysql.log

And if you see LOCK TABLES pop up in there, you know that is the cause. To disable logging just comment out those lines & restart MySQL again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM