[MySQL](https://www.mysql.com/) is a popular open-source relational database management system (RDBMS). The `loopback-connector-mysql` module provides the MySQL connector module for the LoopBack framework.
This installs the module from npm and adds it as a dependency to the application's`package.json`file.
If you create a MySQL data source using the data source generator as described below, you don't have to do this, since the generator will run `npm install` for you.
## Creating a MySQL data source
Use the[Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html)to add a MySQLdata source to your application.
The generator will prompt for the database server hostname, port, and other settings
required to connect to a MySQL database. It will also run the `npm install` command above for you.
The entry in the application's `/server/datasources.json` will look like this:
```javascript
"mydb": {
"name": "mydb",
"connector": "mysql",
"host": "myserver",
"port": 3306,
"database": "mydb",
"password": "mypassword",
"user": "admin"
}
```
Edit `datasources.json` to add any other additional properties that you require.
### Properties
<table>
<thead>
<tr>
<thwidth="150">Property</th>
<thwidth="80">Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>collation</td>
<td>String</td>
<td>Determines the charset for the connection. Default is utf8_general_ci.</td>
</tr>
<tr>
<td>connector</td>
<td>String</td>
<td>Connector name, either “loopback-connector-mysql” or “mysql”.</td>
</tr>
<tr>
<td>connectionLimit</td>
<td>Number</td>
<td>The maximum number of connections to create at once. Default is 10.</td>
</tr>
<tr>
<td>database</td>
<td>String</td>
<td>Database name</td>
</tr>
<tr>
<td>debug</td>
<td>Boolean</td>
<td>If true, turn on verbose mode to debug database queries and lifecycle.</td>
</tr>
<tr>
<td>host</td>
<td>String</td>
<td>Database host name</td>
</tr>
<tr>
<td>password</td>
<td>String</td>
<td>Password to connect to database</td>
</tr>
<tr>
<td>port</td>
<td>Number</td>
<td>Database TCP port</td>
</tr>
<tr>
<td>socketPath</td>
<td>String</td>
<td>The path to a unix domain socket to connect to. When used host and port are ignored.</td>
</tr>
<tr>
<td>supportBigNumbers</td>
<td>Boolean</td>
<td>Enable this option to deal with big numbers (BIGINT and DECIMAL columns) in the database. Default is false.</td>
For TIMESTAMP and DATE types, use the`dateType`option to specify custom type. By default it is DATETIME.
Example:
```javascript
{ startTime :
{ type: Date,
dataType: 'timestamp'
}
}
```
**Note:** When quering a `DATE` type, please be aware that values sent to the server via REST API call will be converted to a Date object using the server timezone. Then, only `YYYY-MM-DD` part of the date will be used for the SQL query.
For example, if the client and the server is in GMT+2 and GMT -2 timezone respectively. Performing the following operation at `02:00 on 2016/11/22` from the client side:
```javascript
var products = Product.find({where:{expired:new Date(2016,11,22)}});
```
will result in the REST URL to look like: `/api/Products/?filter={"where":{"expired":"2016-12-21T22:00:00Z"}}` and the SQL will be like this:
```SQL
SELECT * FROM Product WHERE expired = '2016-12-21'
```
which is not correct.
**Solution:** The workaround to avoid such edge case boundaries with timezones is to use the `DATE` type field as a **_string_** type in the LoopBack model definition.
The MySQL connector supports _model discovery_ that enables you to create LoopBack models
based on an existing database schema using the unified[database discovery API](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels). For more information on discovery, see [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).
### Auto-migratiion
The MySQL connector also supports _auto-migration_ that enables you to create a database schema
from LoopBack models using the [LoopBack automigrate method](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-automigrate).
For more information on auto-migration, see[Creating a database schema from models](https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html) for more information.
Destroying models may result in errors due to foreign key integrity. First delete any related models first calling delete on models with relationships.