# Multiple Server Setup

*Article Topics:* [*Setup*](https://docs.toomuchmedia.com/getting-started/common-topics/setup)

This article explains how to set up NATS across multiple servers. The are a number of solutions you can implement to use multiple servers/databases to improve performance and distribute the workload.

## Database Server(s)

NATS supports the ability to query a secondary "slave" database when running any non-essential reports. A slave database is essentially an exact copy of your actual NATS database that is typically used by NATS for larger, time-intensive queries. You can have as many slave databases attached to your master NATS database as you want at no extra charge. Using a slave database will greatly improve performance on both your join pages and on your reporting pages, as they will not be fighting for the same database resources when they are both running at the same time.

{% hint style="danger" %}
**Important:** Your host must ensure that the database user has permission to create temporary tables in both the master and slave databases.
{% endhint %}

To use a slave database in NATS, your host must first create the slave database in MySQL and set up replication between the two. Replication is how the master and slave databases keep data up-to-date with each other. MySQL's official documentation on replication can be [found here](http://dev.mysql.com/doc/refman/5.0/en/replication.html).

{% hint style="warning" %}
**Please Note:** NATS Technical Support *cannot* and *will not* create slave databases or setup replication as this is something that is set up outside of the NATS system
{% endhint %}

Once your host sets up replication, the next step is to tell NATS how to connect to the primary slave database. Before you start this next step, you need to get the following information from your host:

* The hostname/address of the slave database
* The username for the slave database
* The password for the slave database
* The name of the slave database

Then in your includes/config.php file add the following lines:

{% code overflow="wrap" %}

```
$config['DB_STATS_SERVER'] = 'localhost';     
//replace localhost with address/hostname of where slave database lives

$config['DB_STATS_USER'] = 'access';          
//replace access with the username for slave database

$config['DB_STATS_PASSWORD'] = 'my_password'; 
//replace my_password with the password for slave database

$config['DB_STATS_DB'] = 'db_name';           
//replace db_name with the name of the slave database
```

{% endcode %}

Where the STATS indicates the copy. You can verify the information by navigating to the [Database Settings section of the Home](https://docs.toomuchmedia.com/nats-admin/configuration/configuration-overview/home#database-settings) tab in the [Configuration Admin](https://docs.toomuchmedia.com/nats-admin/configuration).&#x20;

## Optional Servers

If you have multiple slave databases replicating from your master NATS database, you can have NATS automatically choose one of your slave databases at random when a user hits a page/script that uses the slave database(s) by setting them up as an array. &#x20;

There are several types of servers you can have configured for your NATS install, each with its own way of setup.

* DB\_API\_SERVER
* DB\_REPORTING\_SERVERS
* DB\_ADMIN\_REPORTING\_SERVERS
* DB\_SURFER\_SERVERS

### API Database Server

NATS supports the ability to query a secondary "slave" database for use with the REST API. API Database Servers can not be stored in an array.&#x20;

In your includes/config.php file, add the following lines:

{% code overflow="wrap" %}

```
$config['DB_API_SERVER'] = 'localhost';     
//replace localhost with the  address/hostname of where API slave database lives

$config['DB_API_USER'] = 'access';          
//replace access with the username for API slave database

$config['DB_API_PASSWORD'] = 'my_password'; 
//replace my_password with the password for API slave database

$config['DB_API_DB'] = 'db_name';           
//replace db_name with the name of the API slave database
```

{% endcode %}

### Database Reporting

The Database Reporting Server gives you a secondary slave database to display reporting stats. This can be set up as either an array or a single option.&#x20;

#### Configure as an Array

In your includes/config.php file, add the following lines:

```
$config['DB_REPORTING_SERVERS'] = Array(
	"0" => Array(
		'SERVER' => 'localhost',   //replace localhost with address of where first slave database lives
		'USER' => 'access1',       //replace access1 with the username for the first slave database
		'PASSWORD' => 'password1', //replace password1 with the password for first slave database
		'DB' => 'slave_name1'      //replace slave_name1 with the name of the first slave database
	),
	"1" => Array(
		'SERVER' => 'localhost',   //replace localhost with address of where second slave database lives
		'USER' => 'access2',       //replace access2 with the username for the second slave database
		'PASSWORD' => 'password2', //replace password2 with the password for second slave database
		'DB' => 'slave_name2'      //replace slave_name2 with the name of the second slave database
	)
	//continue in this manner for as many slave databases you wish to configure
);
```

#### Configure as a Single Database

In your includes/config.php file, add the following lines:

{% code overflow="wrap" %}

```
$config['DB_REPORTING_SERVER'] = 'localhost';     
//replace localhost with the address/hostname of where the slave database lives

$config['DB_REPORTING_USER'] = 'access';          
//replace access with the username for the slave database

$config['DB_REPORTING_PASSWORD'] = 'my_password'; 
//replace my_password with the password for the slave database

$config['DB_REPORTING_DB'] = 'db_name';           
//replace db_name with the name of the database
```

{% endcode %}

### Admin Database Reporting

The Admin Database Reporting Server functions similarly to the Database Reporting server but is reserved for admin access only, giving admins faster queries when viewing NATS reporting. Admin Database Reporting can only be configured in an array.&#x20;

In your includes/config.php file, add the following lines:

```
$config['DB_ADMIN_REPORTING_SERVERS'] = Array(
	"0" => Array(
		'SERVER' => 'localhost',   //replace localhost with address of where first slave database lives
		'USER' => 'access1',       //replace access1 with the username for the first slave database
		'PASSWORD' => 'password1', //replace password1 with the password for first slave database
		'DB' => 'slave_name1'      //replace slave_name1 with the name of the first slave database
	),
	"1" => Array(
		'SERVER' => 'localhost',   //replace localhost with address of where second slave database lives
		'USER' => 'access2',       //replace access2 with the username for the second slave database
		'PASSWORD' => 'password2', //replace password2 with the password for second slave database
		'DB' => 'slave_name2'      //replace slave_name2 with the name of the second slave database
	)
	//continue in this manner for as many slave databases you wish to configure
);
```

### Surfer Servers

The Surfer Server Database is used for tracking your signup scripts. Server Database Servers can only be configured in an array.

In your includes/config.php file, add the following lines:

```
$config['DB_SURFER_SERVERS'] = Array(
	"0" => Array(
		'SERVER' => 'localhost',   //replace localhost with address of where first slave database lives
		'USER' => 'access1',       //replace access1 with the username for the first slave database
		'PASSWORD' => 'password1', //replace password1 with the password for first slave database
		'DB' => 'slave_name1'      //replace slave_name1 with the name of the first slave database
	),
	"1" => Array(
		'SERVER' => 'localhost',   //replace localhost with address of where second slave database lives
		'USER' => 'access2',       //replace access2 with the username for the second slave database
		'PASSWORD' => 'password2', //replace password2 with the password for second slave database
		'DB' => 'slave_name2'      //replace slave_name2 with the name of the second slave database
	)
	//continue in this manner for as many slave databases you wish to configure
);
```

Lastly, navigate to the [Database Settings section of the Home](https://docs.toomuchmedia.com/nats-admin/configuration/configuration-overview/home#database-settings) tab in the [Configuration Admin](https://docs.toomuchmedia.com/nats-admin/configuration) and enable the **Allow Track Slave** and **Allow Signup Slave** options.

<figure><img src="https://2232071635-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWJVne6meHqGT8rzsBdod%2Fuploads%2FKJCZzuIOTXKQWqnEFakw%2Fsetup-multipleserversetup-surferservers.png?alt=media&#x26;token=a60b781b-37cc-4bd1-bc78-b70be0e6b01b" alt=""><figcaption></figcaption></figure>
