Server Side Swift with Vapor · Persisting Data with MySQL | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/870225-server-side-swift-with-vapor/lessons/16

The web address at 00:57 seems invalid (https://www.docker.com/docker/mac)

:unamused: looks like they’ve changed their site. Thanks for letting me know!

The current link is Install Docker Desktop on Mac | Docker Documentation

Do you have any tool you would suggest to visualize the database (as table, with the relationship, etc.) ?

@nidupb a Google shows MySQL :: Download MySQL Workbench might work for you. Basically any MySQL GUI should be able to help - just use the same credentials as you provide your Vapor app

1 Like

I’m getting the following error:

Thread 1: Fatal error: Error raised at top level: :warning: MySQL Error: Access denied for user ‘til’@‘172.17.0.1’ (using password: YES)

Docker command used is:

docker run --name mysql -e MYSQL_USER=til -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor -p 3306:3306 -d mysql/mysql-server:5.7

I’ve searched for a solution and was not able to find any.

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @oncoas if you’re still having this issue there’s a few things to check. First, do you get the same error if you stop the Docker container? If so that normally means you have another MySQL instance running locally. If that’s unsuccessful, take a look at your configure.swift - does it match the project files?

Akshays-MacBook-Air-2:TILApp akshaydevkate$ docker run --name mysql -e MYSQL_USER=til -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor -p 3306:3306 -d mysql/mysql-sever:8.7

Unable to find image ‘mysql/mysql-sever:8.7’ locally

docker: Error response from daemon: pull access denied for mysql/mysql-sever, repository does not exist or may require ‘docker login’: denied: requested access to the resource is denied.

See ‘docker run --help’.

what should i do with this error @0xtim

You need mysql/mysql-server:5.7 (not 8.7)

Screenshot 2019-12-30 at 1.22.45 PM

No such module leaf

i tried this

still the same error

What does your Package.swift look like?

Akshays-MacBook-Air-2:TILApp akshaydevkate$ docker run --name mysql -e MYSQL_USER=til -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor -p 3306:3306 -d mysql/mysql-server:5.7
Unable to find image ‘mysql/mysql-server:5.7’ locally
5.7: Pulling from mysql/mysql-server
a316717fc6ee: Pull complete
b64762744f75: Pull complete
a1f742e3aa43: Pull complete
f71a5f0dcc26: Pull complete
Digest: sha256:5396bc60a6c08abb6b7e8350b255324a91ee9f3ea11f009aea3e4b61ead38bf6
Status: Downloaded newer image for mysql/mysql-server:5.7
4c7dfa0de54efbbea34822003bc757371befffae265c01bff5f1de3e03d74e16
docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql (e4fccf955e5a77c8e420026c5c40605e7f270ec3b71a2a656bbd6d0d1be2f691): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.

Akshays-MacBook-Air-2:TILApp akshaydevkate$ docker exec -it mysql mysql -u til -ppassword
Error response from daemon: Container 4c7dfa0de54efbbea34822003bc757371befffae265c01bff5f1de3e03d74e16 is not running

Akshays-MacBook-Air-2:TILApp akshaydevkate$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c7dfa0de54e mysql/mysql-server:5.7 “/entrypoint.sh mysq…” 6 minutes ago Created mysql

You already have some MySQL container or program running on that app

Hi @0xtim
I wan to save an object to my mysql database. The object is created manually. So I don’t have a request. How can I achieve this.

@gopeler where are you when trying to save the object? Wherever you’re trying to save it, you should have access to either a Request or the Application which should be enough to get a database connection

I have access to app but couldn’t make it to save object. Is there Any tutorial or example for it?
I found it. Incase anyone else needs it

let _ = app.requestPooledConnection(to: .mysql).flatMap { connection in
                asset.save(on: connection).always {
                    try? app.releasePooledConnection(connection, to: .mysql)
                }
            }

You can do

app.withPooledConnection(to: .mysql).flatMap { connection in
    asset.save(on: connection)
}

withPooledConnection will automatically release the connection for you.

One point - where are you calling this function? You can probably do .wait() to remove the futures. You can’t throw away the future like that, it will cause memory leaks