30

When I want to execute an update query on my table I got an error saying:

1036 - Table data is read only.

How can I fix that?

Table attributes in /var/db/mysql are set to 777.

'Repair Table' function doesnt seems to help.

Is there anything I can do with that?

Cyclone
  • 14,839
  • 23
  • 82
  • 114
  • I had the same error in MaridDB 10.6 for table with ROW_FORMAT=COMPRESSED After removing this option all works as exprected – M-A-X Nov 06 '21 at 14:33

12 Answers12

38

In my case, mysql config file had innodb_force_recovery = 1. Commenting that out solved the issue.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rahi
  • 1,435
  • 1
  • 12
  • 20
  • innodb_force_recovery is issue, comment this line or remove it – Thoman Dec 18 '16 at 11:08
  • 1
    In /etc/my.cnf I set: innodb_file_per_table=0 and innodb_force_recovery=0. then, restart mysql service and finally it works! Thanks – atwebceo Jun 07 '17 at 02:16
  • But the service mariadb doesn't start without `innodb_force_recovery = 5` in my case, but I have readonly tables for `mysql_upgrade` which needs to be connected to running service. It sounds like a dead-lock. – Ariel Bogdziewicz Jun 02 '23 at 05:43
33

who owns /var/db/mysql and what group are they in, should be mysql:mysql. you'll also need to restart mysql for changes to take affect

also check that the currently logged in user had GRANT access to update

ncremins
  • 9,140
  • 2
  • 25
  • 24
10

In case you (like me) are trying to temporarily alter data via the MySQL Workbench interface:

  • If the table does not have a primary key, MySQL Workbench has no way of identifying the row you are trying to alter, so therefore you cannot alter it.

  • Solution in that case is to either alter the data via another route, or simply to add a primary key to the table.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Fom
  • 485
  • 6
  • 14
6

You should change owner to MYSQL:MYSQL.

Use this command: chown -Rf mysql:mysql /var/lib/mysql/DBNAME

Tim Visée
  • 2,988
  • 4
  • 45
  • 55
kamranonline
  • 315
  • 1
  • 5
  • 14
3

My situation is everytime I needed to edit "innodb_force_recovery = 1" in my.ini to force mysql to start, and the error log showed some error said:

Attempted to open a previously opened tablespace. Previous tablespace mysql/innodb_table_stats uses space ID: 1 at filepath: .\mysql\innodb_table_stats.ibd. Cannot open tablespace profile/profile_commentmeta which uses space ID: 1 at filepath: .\profile\profile_commentmeta.ibd

I didn't know why this file was not able to open and it caused so many other"table read only" problems to other databases too.

So here is how I fixed this problem in a simple way without hurting other files.

1 First of all, make sure if you add innodb_force_recovery = 1 below [mysqld] in my.ini file, and it is working, under path: X:\xampp\mysql\bin\my.ini

2 Then next step, export all the databases through localhost/phpmyadmin under the export tab, and store them somewhere, like this:

export .sql databases somewhere

3 comment out the data filefolder to data-bak, then create a new data filefolder,

enter image description here

4 Next step, import all .sql database back from phpmyadmin panel, please also copy phpmyadmin filefolder from the old data-bak filefolder to the new data filefolder. If any file is necessary, go back to data-bak filefolder to copy and paste.

enter image description here

Now all fixed and done, don't need to force mysql to start everytime. Hope this also works for you.

Community
  • 1
  • 1
Sophie cai
  • 195
  • 4
  • 13
1

MySQL doesn't have write access to the database file. Check the permissions and the owner of the file.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • if you change the permissions manually don't forget to issue a FLUSH PRIVILEGES command. – Jody Mar 05 '12 at 23:57
0

On windows I use Xampp server I comment the line in my.ini innodb_force_recovery = 1 to #innodb_force_recovery = 1 the problem resolved

vpgodara
  • 329
  • 2
  • 2
0

I solved the same issue by editing app. armour configuration file. Found the answer here: https://stackoverflow.com/a/14563327/31755661

eddy85br
  • 284
  • 4
  • 12
0

maybe you get read only error from your table storage engine.
Check you Storage Engine, maybe if it is MRG_MYISAM change it to MyISAM and try again.

0

If you are running selinux in enforcing mode then check your /var/log/messages for audit faults. If you see the tell-tale "****" messages about selinux blocking write access to your table files in / then you need to relabel those files so that they have this label:

system_u:object_r:mysqld_db_t:s0

What you could have is a broken label from copying those files over from a user directory (such as during a recovery attempt).

There's a great resource for selinux here:

https://docs.fedoraproject.org/en-US/Fedora/11/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-SELinux_Contexts_Labeling_Files-Persistent_Changes_semanage_fcontext.html

Just remember that you will need to do this for all of those files, which could be many. Then you will want to run the "restorecon -R -v " command to get the recursive (-R) application of the new labels. There is no support for -R in the semanage command, as far as I could tell.

For reference, the semanage command to relabel looks like this:

semanage fcontext -a -t mysqld_db_t 'filename'

The quoting of the file name is critical for the command to work.

0

In my case there was a table with read-only state set and when I tried to restart mysql service it would not even start again and with no descriptive error.

Solution was to run fsck on the drive (with many fixes), which was advised after Ubuntu reboot.

I'm running Ubuntu in VirtualBox under Windows and it often hangs or is having functionality problems.

Esamo
  • 111
  • 4
-1

One other way to receive this error is to create your table with a "Like" statement and use as source a merged table. That way the newly create table is read-only and can't "receive" any new records.

so

CREATE TABLE ic.icdrs_kw37 LIKE ic.icdrs ... #<- a merged table.

then:

REPLACE INTO ic.icdrs_kw37 ...  # -> "Table is read-only"

bug or feature?

user1693885
  • 493
  • 4
  • 5