MySQL Provider¶
After you've added the MySQL Provider package to your project, setting the provider up in code is easy.
Add to Droplet¶
First, register the MySQLProvider.Provider
with your Droplet.
import Vapor import MySQLProvider let config = try Config() try config.addProvider(MySQLProvider.Provider.self) let drop = try Droplet(config) ...
Configure Fluent¶
Once the provider is added to your Droplet, you can configure Fluent to use the MySQL driver.
Config/fluent.json
{ "driver": "mysql" }
Seealso
Learn more about configuration files in the Settings guide.
Configure MySQL¶
If you run your application now, you will likely see an error that the MySQL configuration file is missing. Let's add that now.
Basic¶
Here is an example of a simple MySQL configuration file.
Config/mysql.json
{ "hostname": "127.0.0.1", "user": "root", "password": "password", "database": "hello" }
Note
It's a good idea to store the MySQL configuration file in the Config/secrets
folder since it contains sensitive information.
URL¶
You can also pass the MySQL credentials as a URL.
Config/mysql.json
{ "url": "http://root:password@127.0.0.1/hello" }
Read Replicas¶
Read replicas can be supplied by passing a single master
hostname and an array of readReplicas
hostnames.
Config/mysql.json
{ "master": "master.mysql.foo.com", "readReplicas": ["read01.mysql.foo.com", "read02.mysql.foo.com"], "user": "root", "password": "password", "database": "hello" }
Tip
You can also provide the readReplicas
as a comma-separated string.
Driver¶
You can get access to the MySQL Driver on the droplet.
import Vapor import MySQLProvider let mysqlDriver = try drop.mysql()
Configure Cache¶
You can also choose to use your Fluent database (now set to MySQL) for caching.
Config/droplet.json
{ "driver": "fluent" }
Learn more about caching here.
Done¶
Next time you boot your Droplet, you should see:
Database prepared
You are now ready to start using Fluent with your MySQL database.