Permission denied when creating schema for primary/replicas #6937

Closed
opened 2026-01-22 15:41:43 +01:00 by admin · 6 comments
Owner

Originally created by @fmarchalemisys on GitHub (Mar 2, 2022).

Bug Report

Q A
BC Break no
Version 2.11

Summary

Creating the schema from the command line fails if a primary/replica configuration is used.

php bin/console doctrine:schema:create

SQL commands are sent to the replica instead of the primary db.

The culprit is SchemaTool

It executes CREATE statements with executeQuery($sql) instead of executeStatement($sql).

Current behavior

Creating the schema fails due to CREATE statement being denied on replica. The replica is read only. The schema can't be created on the replica.

How to reproduce

Unless you have two db servers configured as master/slave, the setup can be replicated on a single db server. Create a R/W user to simulate the primary and a R/O user with SELECT permission to simulate the replica.

doctrine:
    dbal:
        driver: 'pdo_mysql'
        server_version: 'mariadb-10.3.11'
        charset: utf8mb4
        url: 'mysql://live_fred:password@127.0.0.1:3306/live_fred'
        replicas:
            replica1:
                url: 'mysql://live_fred_ro:password@127.0.0.1:3306/live_fred'

Run the command:

php bin/console doctrine:schema:create
In ToolsException.php line 19:
                                                                                                                                                                                                                                            
  Schema-Tool failed with Error 'An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1142 CREATE command denied to user 'live_fred_ro'@'localhost' for table 'core_print_ticket_translation'  
  ' while executing DDL: CREATE TABLE core_print_ticket_translation … 

Expected behavior

SQL CREATE statements must be sent to the primary.

CREATE statements are not queries. SchemaTool must call executeStatement($sql).

Originally created by @fmarchalemisys on GitHub (Mar 2, 2022). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.11 #### Summary Creating the schema from the command line fails if a primary/replica configuration is used. php bin/console doctrine:schema:create SQL commands are sent to the replica instead of the primary db. The culprit is [SchemaTool](https://github.com/doctrine/orm/blob/2.11.x/lib/Doctrine/ORM/Tools/SchemaTool.php#L98) It executes CREATE statements with `executeQuery($sql)` instead of `executeStatement($sql)`. #### Current behavior Creating the schema fails due to CREATE statement being denied on replica. The replica is read only. The schema can't be created on the replica. #### How to reproduce Unless you have two db servers configured as master/slave, the setup can be replicated on a single db server. Create a R/W user to simulate the primary and a R/O user with SELECT permission to simulate the replica. ~~~~~ doctrine: dbal: driver: 'pdo_mysql' server_version: 'mariadb-10.3.11' charset: utf8mb4 url: 'mysql://live_fred:password@127.0.0.1:3306/live_fred' replicas: replica1: url: 'mysql://live_fred_ro:password@127.0.0.1:3306/live_fred' ~~~~~ Run the command: php bin/console doctrine:schema:create ~~~~~ In ToolsException.php line 19: Schema-Tool failed with Error 'An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1142 CREATE command denied to user 'live_fred_ro'@'localhost' for table 'core_print_ticket_translation' ' while executing DDL: CREATE TABLE core_print_ticket_translation … ~~~~~ #### Expected behavior SQL CREATE statements must be sent to the primary. CREATE statements are not queries. SchemaTool must call `executeStatement($sql)`.
admin added the Bug label 2026-01-22 15:41:43 +01:00
admin closed this issue 2026-01-22 15:41:44 +01:00
Author
Owner

@leuchtdiode commented on GitHub (Apr 4, 2022):

Experienced this issue as well. Schema tool is trying to execute statements on replicas (which are in read-only mode in my setup). It should only be executed on primary only.

@leuchtdiode commented on GitHub (Apr 4, 2022): Experienced this issue as well. Schema tool is trying to execute statements on replicas (which are in read-only mode in my setup). It should only be executed on primary only.
Author
Owner

@eichie commented on GitHub (Apr 11, 2022):

+1

@eichie commented on GitHub (Apr 11, 2022): +1
Author
Owner

@elpoutro commented on GitHub (Jan 18, 2023):

Same issue with "php bin/console doctrine:schema:update" since update form doctrine 2.3 to 2.8.

@elpoutro commented on GitHub (Jan 18, 2023): Same issue with "php bin/console doctrine:schema:update" since update form doctrine 2.3 to 2.8.
Author
Owner

@pich commented on GitHub (Apr 12, 2023):

+1

@pich commented on GitHub (Apr 12, 2023): +1
Author
Owner

@greg0ire commented on GitHub (Apr 12, 2023):

Fixed in https://github.com/doctrine/orm/releases/tag/2.14.2

@greg0ire commented on GitHub (Apr 12, 2023): Fixed in https://github.com/doctrine/orm/releases/tag/2.14.2
Author
Owner

@pich commented on GitHub (Apr 13, 2023):

Thanks. Great job @simPod & @greg0ire . I owe you guys a beer :)

@pich commented on GitHub (Apr 13, 2023): Thanks. Great job @simPod & @greg0ire . I owe you guys a beer :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6937