Learn how to set up your Vapor application to use MySQL as a database, using Docker, so your data is persisted.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/15
Learn how to set up your Vapor application to use MySQL as a database, using Docker, so your data is persisted.
This might be an obvious point, but just mentioning it for the sake of those who might not have thought of this
For those who already have MySQL running on their machine either via something like MAMP or XAMPP or some other means, you donāt really have to install Docker - unless you really want to, of course You can simply use your existing MySQL server to work with Vapor. While I do have Docker set up already, I also have an existing MySQL installation via MAMP and I simply used that for the tutorial.
I do realize why Docker was used for the tutorial since that is easier than running the viewer/student through a full MySQL installation, but just mentioning this in case this does prove to be useful to someone out there ā¦
Iām getting 2 MySQL Compiling errors (for the build end of the video):
type āSHA1ā has no member ādigestā
value of type āDataā has no member āhexEncodedStringā
https://drive.google.com/open?id=1kYcLmKI7TbYUyuvFSF1kn54tdYo4SEU9
https://drive.google.com/open?id=1e03yQyqnn3xELxEBw-lRUfEfpJ-WF8mR
https://drive.google.com/open?id=1SEvio4I-aNdhy_tMDGVcjm4G1NIkUQox
https://drive.google.com/open?id=1BsbidHQtsjYRldiAR9Vw4tMNzjsjKssR
@amyerson Iām not seeing those errors, can you make sure your Package.swift looks like:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "TILApp",
dependencies: [
// š§ A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-rc"),
.package(url: "https://github.com/vapor/fluent-mysql.git", from: "3.0.0-rc"),
],
targets: [
.target(name: "App", dependencies: ["FluentMySQL", "Vapor"]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App"]),
]
)
And run vapor update
in Terminal to make sure you have the latest RC
Iām getting an error, after I changed my pivot to a MySQLUUIDPivot, the searchHandler method broke.
return try Acronym.query(on: req).group(or) { or in
or.filter(\.short == searchTerm).all()
or.filter(\.long == searchTerm).all()
}.all()
I get the āUse of unresolved identifier orā message. The āORā giving the error is the one inside the .group(or). Iāve been messing around with it, but couldnāt find a way to fix.
@andrekilik change the Pivot shouldnāt affect that file! Are you on RC2?
On RC2, I had to re-write the search handler as follows in order to get it to compile:
return try Acronym.query(on: req).group(.or, closure: {(or) in
try or.filter(\.short == term)
try or.filter(\.long == term)
}).all()
Note the two try
statements within the closure
@fahim in case you havenāt seen it, all the changes required for RC2 are here Changes for the Ray Wenderlich Vapor Videos for Vapor 3 RC 2 Ā· GitHub
Thanks, Tim I did see that and have it bookmarked. I was just responding to @andrekilik since I believe the error he was talking about was the one I mentioned.
Hello,
This is my Package.swift
$ cat Package.swift
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: āTILAppā,
dependencies: [
// A server-side Swift web framework.
.package(url: āGitHub - vapor/vapor: š§ A server-side Swift HTTP web framework.ā, from: ā3.0.0-rc.2ā),
.package(url: āGitHub - vapor/fluent-mysql-driver: šš¬ Swift ORM (queries, models, relations, etc) built on MySQL.ā, from: ā3.0.0-rc.2ā)
],
targets: [
.target(name: āAppā, dependencies: [āFluentMySQLā, āVaporā]),
.target(name: āRunā, dependencies: [āAppā]),
.testTarget(name: āAppTestsā, dependencies: [āAppā])
]
)
I was also getting the same error as @amyerson regarding the ādigestā and āencodingā
Then after doing āvapor updateā seems everything is updated and no longer have those errors.
Is it all working again then?
Thank you very much Tim. It is working again, yes. I do not have those 2 MySQL compiling errors anymore.
I keep getting the compile error 'Missing required module āCSQLiteā.
This is Package.swift
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: āTILAppā,
dependencies: [
// A server-side Swift web framework.
.package(url: āGitHub - vapor/vapor: š§ A server-side Swift HTTP web framework.ā, from: ā3.0.0-rcā),
// šµ Swift ORM (queries, models, relations, etc) built on SQLite 3.
.package(url: "https://github.com/vapor/fluent-mysql.git", from: "3.0.0-rc")
],
targets: [
.target(name: "App", dependencies: ["FluentMySQL", "Vapor"]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App"])
]
)
Does anyone know a way to get this to compile?
@andy4202 that usually means you have leftover references to SQLite somewhere in your code, something like an import FluentSQLite
or SQLiteModel
lying around somewhere. If you do rm -rf .build
to remove any old build artefacts and regenerate the project it should point of the actual error if you canāt find it
It seems like PostgreSQL setup is quite different than what youāve shown. Any chance youāll be doing a video on using that database? For example, I donāt see anything like the automated model setup interfaces that you used with MySQL
@gargoyle they should be almost identical. See this project which is similar but uses Postgres instead of MySQL
@0xtim Please include the link
Hi Tim,
cool tutorial - very good - up to now I was successful in building it as described and shown in the video clips.
I hope this is the right place to post my question, when running the project from Xcode (already changed to FluentMySQL DB connection) I get the following error message
Thread 1: Fatal error: Error raised at top level: MySQL Error: Unsupported auth plugin: caching_sha2_password
File: /App-Programmierung/DB_Access/VAPOR/TILAPP/.build/checkouts/mysql.git-1890032512239690518/Sources/MySQL/Connection/MySQLConnection+Authenticate.swift
Username/Password/Database is set correct as stated within docker MySQL command, and MySQL within docker is running.
Package.swift is correct as stated in your post.
It would be fine if you can give me a hint what I have done wrong.
Many thanks!!
Alex
@a_stefka you havenāt done anything wrong! Unfortunately MySQL 10.0.2(?) introduced a new default authentication module that Vapor doesnāt support yet. Thereās an issue open but for now you can do:
docker run --name mysql -e MYSQL_USER=vapor \
-e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor \
-p 3306:3306 -d mysql/mysql-server:5.7
This specifies the 5.7 MySQL Server which still works