If the Magento application is already in production or if you’ve already installed custom code or components, you might need to configure split databases manually. Before continuing, contact Magento Support to see if this is necessary in your case.
Manually splitting databases involves:
Create the checkout and order management system (OMS) databases
Run a series of SQL scripts that:
Drop foreign keys
Back up sales and quote database tables
Move tables from your main Magento database to the sales and quote databases
If any custom code uses JOINs with tables in the sales and quote databases, you cannot use split databases. If in doubt, contact the authors of any custom code or extensions to make sure their code does not use JOINs.
This topic uses the following naming conventions:
The main Magento database name is magento and its user name and password are both magento
The quote database name is magento_quote and its user name and password are both magento_quote
The quote database is also referred to as the checkout database.
The sales database name is magento_sales and its user name and password are both magento_sales
The sales database is also referred to as the order management system (OMS) database.
This guide assumes all three databases are on the same host as the Magento application. However, the choice of where to locate the databases and what they're named is up to you. We hope our examples make the instructions easier to follow.
Back up the Magento system
We strongly recommend you back up your current database and file system so you can restore it later in the event of issues during the process.
This section discusses how to create database instances for sales and quote tables.
Click to show how to create database instances
Create sales and OMS quote databases as follows:
Log in to your database server as any user.
Enter the following command to get to a MySQL command prompt:
mysql -u root -p
Enter the MySQL root user’s password when prompted.
Enter the following commands in the order shown to create database instances named magento_quote and magento_sales with the same user names and passwords:
create database magento_quote;
GRANT ALL ON magento_quote.* TO magento_quote@localhost IDENTIFIED BY 'magento_quote';
create database magento_sales;
GRANT ALL ON magento_sales.* TO magento_sales@localhost IDENTIFIED BY 'magento_sales';
Enter exit to quit the command prompt.
Verify the databases, one at a time:
quote database:
mysql -u magento_quote -p
exit
Order management database:
mysql -u magento_sales -p
exit
If the MySQL monitor displays, you created the database properly. If an error displays, repeat the preceding commands.
Continue with the next section.
Configure the sales database
This section discusses how to create and run SQL scripts that alter quote database tables and back up data from those tables.
Sales database table names start with:
salesrule_
sales_
magento_sales_
The magento_customercustomattributes_sales_flat_order table is also affected
This section contains scripts with specific database table names. If you've performed customizations or if you want to see a complete list of tables before you perform actions on them, see Reference scripts.
Click to create and run sales database SQL scripts
Create the following SQL scripts in a location that is accessible by the user as whom you log in to your Magento server. For example, if you log in or run commands as root, you can create the scripts in the /root/sql-scripts directory.
Remove foreign keys
This script removes foreign keys that refer to non-sales tables from the sales database.
Create the following script and give it a name like 1_foreign-sales.sql. Replace <your main Magento DB name> with the name of your Magento database.
Configure the sales database
Run the preceding script:
Log in to your MySQL database as the root or administrative user:
mysql -u root -p
At the mysql> prompt, run the script as follows:
source <path>/<script>.sql
For example,
source /root/sql-scripts/1_foreign-sales.sql
After the script run, enter exit.
Back up sales data
This section discusses how to back up sales tables from the main Magento database so you can restore them in the separate sales database.
Click to back up and restore sales data
If you’re currently at the mysql> prompt, enter exit to return to the command shell.
Run the following mysqldump commands, one at a time, from the command shell. In each, substitute the following:
<your database root user name> with the name of your database root user
<your database root user password> with the user’s password
<your main magento DB name> with the name of your Magento database
<path> with a writable file system path
Script 1
Script 2
Script 3
Script 4
Restore sales data
This script restores sales data in your quote database.
Convert tables from InnoDb to NDB type in dump files:
sed -ei 's/InnoDb/NDB/' <file name>.sql
Remove rows with a FULLTEXT key from dumps because NDB tables don’t support FULLTEXT.
Restore the data
Run the following commands:
where
<your sales DB name> with the name of your sales database.
In this topic, the sample database name is magento_sales.
<root user name> with your MySQL root user name
<root user password> with the user’s password
Verify the location of the backup files you created earlier (for example, /var/sales.sql)
Configure the quote database
This section discusses tasks required to drop foreign keys from sales database tables and move tables to the sales database.
This section contains scripts with specific database table names. If you've performed customizations or if you want to see a complete list of tables before you perform actions on them, see Reference scripts.
Quote database table names start with quote. The magento_customercustomattributes_sales_flat_quote and magento_customercustomattributes_sales_flat_quote_address tables are also affected
Drop foreign keys from quote tables
This script removes foreign keys that refer to non-quote tables from quote tables. Replace with the name of your Magento database.
Click to drop foreign keys from quote tables
Create the following script and give it a name like 2_foreign-key-quote.sql:
Run the script as follows:
Log in to your MySQL database as the root or administrative user:
mysql -u root -p
At the mysql> prompt, run the script as follows:
it`.
source /
source /root/sql-scripts/2_foreign-key-quote.sql
After the script runs, enter exit.
Back up quote tables
This section discusses how to back up quote tables from the main Magento database and restore them in your quote database.
Click to back up and restore quote tables
Run the following command from a command prompt:
mysqldump -u <your database root user name> -p <your main Magento DB name> magento_customercustomattributes_sales_flat_quote magento_customercustomattributes_sales_flat_quote_address quote quote_address quote_address_item quote_item quote_item_option quote_payment quote_shipping_rate quote_id_mask > /<path>/quote.sql;
Open <your Magento install dir>/app/etc/env.php in a text editor and update it using the guidelines discussed in the following sections.
Update database connections
Locate the block starting with 'default' (under 'connection') and add 'checkout' and 'sales' sections. Replace sample values with values appropriate for your site.
Update resources
Locate the block starting with 'resource' and add 'checkout' and 'sales' sections to it as follows:
Reference scripts
This section provides scripts you can run that print a complete list of affected tables without performing any actions on them. You can use them to see what tables are affected before you manually split databases, which can be useful if you use extensions that customize the Magento database schema.
Click to view reference SQL scripts
To use these scripts:
Create a .sql script with the contents of each script in this section.
In each script, replace <your main magento DB name> with the name of your Magento database.
In this topic, the sample database name is magento.
Run each script from the mysql> prompt as source <script name>
Examine the output.
Copy the result of each script to another .sql script, removing the pipe characters (|).
Run each script from the mysql> prompt as source <script name>.
Running this second script performs the actions in your main Magento database.
Remove foreign keys (sales tables)
This script is the removes foreign keys that refer to non-sales tables from the sales database.
Remove foreign keys (quote tables)
This script removes foreign keys that refer to non-quote tables from quote tables.
Drop sales tables
This script drops sales tables from the Magento database.