无法通过socket' /tmp/mysql.sock&#39连接到本地MySQL服务器; (2)

时间:2014-03-16 11:05:51

标签: mysql sql

我已安装MySQL服务器并尝试连接到它,但收到错误:

    Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

我检查了我的/ tmp目录,没有mysql.sock。我无法在任何地方找到mysql.sock。我读到它可能在

    /var/lib/mysql/mysql.sock

但我也检查了那里,甚至没有mysql目录,只有/ lib内部的一些后缀。任何人都可以帮我解决这个问题吗?

18 个答案:

答案 0 :(得分:238)

尝试启动MySQL服务器:

mysql.server start

答案 1 :(得分:54)

我在更新OS X Yosemite后得到了同样的问题,解决方案很简单,检查系统首选项 - > mysql,状态为STOP。只需重新启动它,它现在在我的Mac上工作正常。

答案 2 :(得分:41)

对于MAMP

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

来自https://coderwall.com/p/w5kwzw/solved-can-t-connect-to-local-mysql-server-through-socket-tmp-mysql-sock

更新: 每次我的计算机重新启动时,我都必须输入此命令,因此我创建了一个快捷方式。

在终端类型中执行以下操作:

~: vi ~/.profile

添加

alias ...='source ~/.profile'
alias sockit='sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock'

保存。

在终端类型中:

~: ...来源.profile配置。

现在在终端,您只需输入

即可
~: sockit

答案 3 :(得分:29)

以下命令解决了我的问题:

sudo chown -R _mysql:mysql /usr/local/var/mysql

sudo mysql.server start

答案 4 :(得分:8)

以下解决了我的问题:

检查您的MySQL服务器列在哪里:netstat -nlp 如果它是列表到TCP,那么在连接到DB时使用127.0.0.1而不是" localhost"

检查MySQL文档here

答案 5 :(得分:8)

尝试所有解决方案后,只有在指定主机

后才能为我工作
mysql -u root -p -h127.0.0.1

请求密码

Enter password:

按enter键

如果一切正常,它将会起作用。

答案 6 :(得分:8)

/etc/my.cnf中的mysql配置文件中进行以下更改,然后重新启动mysqld dameon进程

[client]
socket=/var/lib/mysql/mysql.sock

同样检查一下这个相关的帖子

Can't connect to local MySQL server through socket '/tmp/mysql.sock

答案 7 :(得分:7)

试试这个对我有用。

sudo /usr/local/mysql/support-files/mysql.server start

答案 8 :(得分:5)

经过几个小时的努力,唯一有效的是

sudo mysql.server start

然后使用

进行安全安装
mysql_secure_installation 

然后通过

连接到数据库
mysql -uroot -p

Mysql是通过自制软件安装的,版本是

Server version: 5.7.21 Homebrew

指定版本可能会有所帮助,因为解决方案可能因版本而异。

答案 9 :(得分:4)

如果您在Mac OS X中使用XAMPP并使用Homebrew安装MySQL,则可能存在此问题。在XAMPP管理器窗口中,转到Manage Servers并选择MySQL,然后单击configure并打开配置文件,在那里你有套接字文件路径,把路径放在MySQL主机配置中它应该可以工作。

这是这样的:

...
[client]
#password   = your_password
port        = 3306
socket      = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
...

然后,例如在Django中:

...    
DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.mysql",
            "NAME": "database_name",
            "USER": "user",
            "PASSWORD": "password",
            "HOST": "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock",
            "PORT": "",
        }
    }
...

希望这有帮助。

答案 10 :(得分:2)

在终端中输入如下内容:

mysql.server start

答案 11 :(得分:1)

从终端停止并启动mysql服务器解决了我的问题。 下面是在MacO中停止和启动mysql服务器的cmd。

sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start

注意: 从Mac System首选项重新启动服务无法解决Mac中的问题。因此,请尝试从终端重新启动。

答案 12 :(得分:0)

我花了很多时间做这个 我想将django应用程序放在服务器上,当我运行services.AddDbContext<YourDbContext>时遇到了这个问题

然后!我设置这个 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<YourDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings"))); } 终于成功了!

答案 13 :(得分:0)

首先,知道数据目录对我来说是关键。 /usr/local/var/mysql 在这里,至少有一个扩展名为.err的文件,其开头是我的本地计算机名称。它具有我需要诊断的所有信息。

我想我先安装mysql 8搞砸了。我的应用程序与它不兼容,因此我不得不降级到5.7

对我有用的解决方案是 /usr/local/etc/my.cnf

找到此行。我认为它与mysql 8有关:

mysqlx-bind-address = 127.0.0.1 

删除它是因为在mysql 5.7中它在错误日志中表示不喜欢它

如果该行不在bind-address下,也请在其中添加。

socket=/tmp/mysql.sock

转到/tmp目录并删除其中的所有mysql.sock文件。在服务器启动时,它将重新创建袜子文件

使用处于停止状态的mySQL删除数据目录。我的是/usr/local/var/mysql。这是日志所在的位置

我从那里跑了

>mysqld --initialize

然后一切开始起作用...此命令最后将为您提供一个随机密码。保存该密码以进行下一步

运行此密码以分配我自己的密码。

>mysql_secure_installation 

两者

>brew services stop mysql@5.7

>mysql.server start

现在正在工作。希望这可以帮助。大约需要3个小时的反复试验。

答案 14 :(得分:0)

首先输入以下内容:

func parsePrivateSecKey(privateKey: SecKey) -> (mod: Data, exp: Data) {
        let pubAttributes = SecKeyCopyAttributes(privateKey) as! [String: Any]

        print("pubAttributes----\(pubAttributes)")

        //        let keydata = pubAttributes[kSecPrivateKeyAttrs as String] as! [String: Any]
        //        print("----key data ------===\(keydata)")

        let data = Data(base64Encoded: "")
        // Check that this is really an RSA key
        guard    Int(pubAttributes[kSecAttrKeyType as String] as! String)
            == Int(kSecAttrKeyTypeRSA as String) else {
                // throw "Tried to parse non-RSA key as RSA key"
                return (mod: data!, exp: data!)
        }

        // Check that this is really a private key
        guard    Int(pubAttributes[kSecAttrKeyClass as String] as! String)
            == Int(kSecAttrKeyClassPrivate as String)
            else {
                // throw "Tried to parse non-private key as private key"
                return (mod: data!, exp: data!)
        }

        let keySize = pubAttributes[kSecAttrKeySizeInBits as String] as! Int

        // Extract values
        let pubData  = pubAttributes[kSecValueData as String] as! Data
        print("pubData---\(pubData)")
        //  SecKeyCopyModulus

        //Find modulus data length
        let modulusLengthData = pubData.subdata(in: 9..<11)
        print(modulusLengthData as NSData)
        let modulusLength = BigUInt(modulusLengthData)
        print("mod lenth ===\(modulusLength)")
        //find MODULUS
        var modulus  = pubData.subdata(in: 11..<(Int(modulusLength) + 11))
        let reqMod = BigUInt(modulus)
        print("final modulus====\(reqMod)")

        //Find exponent data length
        var expDataLength = pubData.subdata(in: (Int(modulusLength) + 11 + 7)..<(Int(modulusLength) + 11 + 7 + 1))
        print(expDataLength as NSData)
        var bytes_to_find : [UInt8] = [0xFF]    //0xFF = 255 size
        let datafind = Data(bytes: &bytes_to_find, count: bytes_to_find.count)
        let range = expDataLength.range(of: datafind, options: [], in: Range(NSRange(location: 0, length: expDataLength.count)))
        var exponentLength = BigUInt(expDataLength)
        print("exponent lenth ===\(exponentLength)")
        //Find EXPONENT
        var exponent  = pubData.subdata(in: (Int(modulusLength) + 11 + 7 + 1)..<(Int(modulusLength) + 11 + 7 + 1 + Int(exponentLength)))
        //check if its size is 255 or more and get data length accordingly
        if range == nil {
            expDataLength = pubData.subdata(in: (Int(modulusLength) + 11 + 7)..<(Int(modulusLength) + 11 + 7 + 2))
            print("0xFF not found...")
            exponentLength = BigUInt(expDataLength)
            print("exponent lenth ===\(exponentLength)")
            //Find EXPONENT
            exponent  = pubData.subdata(in: (Int(modulusLength) + 11 + 7 + 1)..<(Int(modulusLength) + 11 + 7 + 2 + Int(exponentLength)))

        }


        let reqExp = BigUInt(exponent)
        print("final exponent====\(reqExp)")

        if modulus.count > keySize / 8 { // --> 257 bytes
            modulus.removeFirst(1)
        }

        return (mod: modulus, exp: exponent)
    }

然后这个-:

brew services start mysql

答案 15 :(得分:0)

我也遇到过同样的问题。这是我的固定方法。

第1步:使用命令删除mysql:

brew uninstall --force mysql

第2步:运行命令brew doctor,这将为您提供一些与您的冲泡包装有关的提示。

第3步:使用以下命令清理Brew包装:

brew cleanup

第4步:使用以下命令移动/删除以前安装的mysql数据:

mv /usr/local/var/mysql/ /usr/local/var/old_mysql

第5步:最后使用命令重新安装mysql:

brew install mysql

答案 16 :(得分:-1)

对于CentOS,init mysql的文件位于:

/etc/init.d/mysqld start

答案 17 :(得分:-1)

如果您在macOS上运行,则首先检查“系统偏好设置”,看看MySQL是否正在运行,这将变得更加容易。