Vapor中使用Fluent
作为数据库的驱动,它现在可支持的数据库类型有:MySQL,SQL lite,MongoDB,PostgreSQL。因为MySQL用得较多,我们先来学习它。
安装过程
安装MySQL-Provider
在Package.swift
文件中加入
.Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 2)
然后执行执行vapor clean
和rm -rf .build Package.pins
,最后执行vapor update
和vapor build
。
安装完MySql之后报错mysql/mysql.h file not found
以及Could not build Objective-C module CMySQL
,这时因为MySql数据库需要更新,执行下面的指令
brew update && brew install mysql vapor/tap/cmysql pkg-config
然后再执行vapor xcode
就可以运行成功了。
配置
在Droplet中添加驱动
我们要往Config
对象中添加相应的Provider
,如下所示
import MySQLProvider
let config = try Config()
try config.addProvider(MySQLProvider.Provider.self)
let drop = try Droplet(config)
配置Fluent
在fluent.json
文件中加入如下的配置
{
"//": "The underlying database technology to use.",
"//": "memory: SQLite in-memory DB.",
"//": "sqlite: Persisted SQLite DB (configure with sqlite.json)",
"//": "Other drivers are available through Vapor providers",
"//": "https://github.com/search?q=topic:vapor-provider+topic:database",
"driver": "mysql",
}
配置MySQL
在Config文件夹下面新建文件mysql.json
,并添加如下内容
{
"hostname": "localhost",
"user": "root",
"password": "yourPassword",
"database": "yourDatabase"
"poort": "3306"
}
也可以将证书作为url传入MySQL。
{
"url": "http://root:password@172.0.0.1/hello"
}
多份读取(Read Replicas)
多份读取可以通过配置hostname或者是readReplicas
接口数组来进行配置。在mysql.josn中加入:
{
{
"master": "master.mysql.foo.com",
"readReplicas": ["read01.mysql.foo.com", "read02.mysql.foo.com"],
"user": "root",
"password": "password",
"database": "hello"
}
}
Tip:也可以将readReplicas用字符串表示,多个字符串用逗号分隔开。
驱动
你可以在Routes中得到MySQL Driver
(前提是在你自己的MySQL数据库中创建了your_table
)表。
import MySQLProvider
get("mysql") { req in
let mysqlDriver = try self.mysql()
let user = try mysqlDriver.raw("SELECT * FROM your_table")
let reusltJon = try JSON(node: user)
return reusltJon
}
然后在浏览器中输入http://localhost:8080/mysql
,如果看到输出了相应的JSON传就证明安装成功了。
配置缓存
在Config/droplet.json
里面可以配置fluent
缓存,这里fluent
缓存走的是mysql
:
{
"driver": "fluent"
}
下次,当启动Droplet的时候,如果出现:
Database prepared
就说明安装成功了。
帮助
- 如果运行出现
The current hash key "0000000000000000" is not secure.
Update hash.key in Config/crypto.json before using in production.
Use `openssl rand -base64 <length>` to generate a random string.
The current cipher key "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" is not secure.
Update cipher.key in Config/crypto.json before using in production.
Use `openssl rand -base64 32` to generate a random string.
这说明我们需要运行openssl rand -base64 <length>
,以及openssl rand -base64 32
来产生新的hash key和cipher key,并且将原来的数值替换掉。
- MySql更改密码:
请参考:
- https://stackoverflow.com/questions/2101694/mysql-how-to-set-root-password-to-null
- https://stackoverflow.com/questions/30692812/mysql-user-db-does-not-have-password-columns-installing-mysql-on-osx
- https://sraji.wordpress.com/2011/08/10/how-to-reset-mysql-root-password/
- https://stackoverflow.com/questions/9624774/after-mysql-install-via-brew-i-get-the-error-the-server-quit-without-updating
- https://stackoverflow.com/questions/9624774/after-mysql-install-via-brew-i-get-the-error-the-server-quit-without-updating/9704993#comment16367803_9704993
需要注意的是:sudo mysqld_safe --skip-grant-tables
执行完之后,要重新打开一个终端执行。