即使我能够通过终端将其连接也无法通过Xcode应用程序连接到MySQL Server [英] Can't Connect to MySQL Server through Xcode application even tho I am able to connect it via Terminal

查看:79
本文介绍了即使我能够通过终端将其连接也无法通过Xcode应用程序连接到MySQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个从MySQL服务器获取数据的应用程序.

我从 Homebrew 安装了MySQL,并使用了 PerfectMySql swift软件包.

我想先在本地主机上对其进行测试,但无法从我的应用程序 将其连接 .

当我在命令行中 mysql -u root -p 时,它可以正常工作.

我的MySQL连接代码:

 导入PerfectHTTP导入PerfectHTTPServer导入PerfectMySQL进口基金会公共类数据库{让主机=本地主机";//或"127.0.0.1";让用户="root"让密码="12345678";让数据库=宠物";func databaseConnect(host: String, user: String, password: String, db: String) ->MySQL {let mysql = MySQL()//创建要使用的MySQL实例让连接= mysql.connect(主机:主机,用户:用户,密码:密码,db:db)警卫相连,否则{//验证我们已成功连接打印(mysql.errorMessage())返回mysql}返回mysql}public func insertGame(title:字符串,描述:字符串,createdDate:字符串){//连接到我们的数据库var db = databaseConnect(主机:主机,用户:用户,密码:密码,db:数据库)推迟 {db.close()//此defer块确保无论结果如何,我们都会在完成后终止连接}//创建我们要执行的语句let语句= MySQLStmt(db)让insert =将游戏插入(ID,标题,描述,发行日期)VALUES(\(statement.insertId()),'\(title)','\(description)','\(createdDate)');"_ = statement.prepare(声明:插入)//运行查询让querySuccess = statement.execute()//检查我们的查询是否成功,否则返回保护查询成功否则{打印(db.errorMessage())返回}打印(插入成功!");}} 

当我在Xcode中启动我的应用程序时,它给了我这个错误:

  host ="127.0.0.1"; 

  host =" localhost" 

我在网络和StackOverflow上搜索了几个小时,但找不到任何可解决此问题的方法.

在此或在其他论坛上询问的所有其他问题都与通过终端修复连接问题有关,这对我来说不是问题.

此处仅询问了一个相关问题,但不幸的是未得到回答.->

I'm trying to create an application that fetches data from the MySQL server.

I installed MySQL from Homebrew and using the PerfectMySql swift package.

I want to test it on my localhost first but I'm unable to connect it from my application.

When I mysql -u root -p in the command line, it works as it should.

My code for MySQL connection:

import PerfectHTTP
import PerfectHTTPServer
import PerfectMySQL
import Foundation

public class DB {

    let host = "localhost" // or "127.0.0.1"
    let user = "root"
    let password = "12345678"
    let database = "pets"
   
    func databaseConnect(host: String, user: String, password: String, db: String) -> MySQL {
        
        let mysql = MySQL() // Create an instance of MySQL to work with
        
        let connected = mysql.connect(host: host, user: user, password: password, db: db)
        
        guard connected else {
            // verify that we have connected successfully
            print(mysql.errorMessage())
            return mysql
        }
        
        return mysql
    }
    
    public func insertGame(title: String, description: String, createdDate: String){
    
        // Connect to our database
        var db = databaseConnect(host: host, user: user, password: password, db: database)
        
        defer {
            db.close() //This defer block makes sure we terminate the connection once finished, regardless of the result
        }
        
        // Create the statement we wish to execute
        let statement = MySQLStmt(db)
        let insert = "INSERT INTO game(id, title, description, release_date) VALUES (\(statement.insertId()), '\(title)', '\(description)', '\(createdDate)');"
        _ = statement.prepare(statement: insert)
        
        // Run the query
        let querySuccess = statement.execute()
        
        // Check that our query was successfuly, otherwise return out
        guard querySuccess else {
            print(db.errorMessage())
            return
        }
        
        print("Insert successful!");
    }
    
}

When I start my application in Xcode, it gives me this error:

host = "127.0.0.1"

host = "localhost"

I searched the web and StackOverflow for hours but couldn't find anything to fix this issue.

Every other question asked here or other forums are about fixing connection problems via Terminal, which is not the issue for me here.

Only a related question asked here but it is unfortunately unanswered. -> Can't connect to MySQL server on 'localhost'

解决方案

You said that you were receiving the following error on your macOS client.

Can't connect to MySQL server on '127.0.0.1' (1)

I know you've probably figured it out by now, but for the sake of future readers, this message (which returns an error code of 2003) can result on macOS targets if you neglect to enable "Outgoing Connections (Client)":

这篇关于即使我能够通过终端将其连接也无法通过Xcode应用程序连接到MySQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆