简体   繁体   English

Ruby on Rails database.yml中的socket声明是什么?

[英]What is the socket declaration for, in Ruby on Rails database.yml?

What's the use of socket declaration in config/database.yml ? config / database.yml中套接字声明的用途是什么?

Example code: 示例代码:

staging:
  adapter: mysql
  encoding: utf8
  database: (database)
  pool: 5
  username: (user)
  password: (pass)
  socket: /tmp/mysql.sock     # <--------- this line

My app works, wether this line is commented or not. 我的应用程序正常工作,这条线是否被评论。 So what is it for ? 那有什么用呢? What reasons can I have to leave it, comment it or change it's value ? 我有什么理由可以留下它,评论它或改变它的价值?

When two programs want to talk to each other over the network, one program might open up a TCP connection (a "socket") with the other one. 当两个程序想要通过网络相互通信时,一个程序可能会打开与另一个程序的TCP连接(“套接字”)。 The first program needs to know the IP address of the second computer and the port on which the program is listening. 第一个程序需要知道第二台计算机的IP地址和程序正在侦听的端口。

On Linux, when two programs on the same computer want to talk to each other, they can still open up a TCP connection. 在Linux上,当同一台计算机上的两个程序想要相互通信时,它们仍然可以打开TCP连接。 But they can also open up a connection via a "socket file". 但他们也可以通过“套接字文件”打开连接。 Linux makes the socket file API rather similar to the TCP API, so it's not a big deal to update a program that already communicates over the network via TCP to support communicating via socket files too. Linux使套接字文件API与TCP API非常相似,因此更新已经通过TCP通过网络进行通信的程序以支持通过套接字文件进行通信并不是什么大问题。 Socket files are faster than TCP, but only work when both programs are on the same computer. 套接字文件比TCP快,但仅在两个程序在同一台计算机上时才有效。

MySQL has two methods of communicating with it on unix-ish systems, tcp/ip and domain sockets. MySQL有两种在unix-ish系统,tcp / ip和域套接字上与它通信的方法。 By specifying the socket Rails, or rather the database driver, will use the socket instead of a network connection. 通过指定套接字Rails,或者说数据库驱动程序,将使用套接字而不是网络连接。 It can be quicker to use the socket but YMMV. 它可以更快地使用插座,但YMMV。

http://dev.mysql.com/doc/refman/5.5/en/connecting.html http://dev.mysql.com/doc/refman/5.5/en/connecting.html

I wasted a whole afternoon because of a socket specified in my database.yml - the database specifications for the test environment simply weren't parsed. 由于我的database.yml中指定了一个套接字,我浪费了整整一个下午 - 测试环境的数据库规范根本没有被解析。 So if you have a problem with your test-database not being accessible although everything seems to be fine with it, try removing the socket specification. 因此,如果您的测试数据库无法访问,虽然一切看起来都不错,但请尝试删除套接字规范。

This displays the file location for the UNIX socket: 这将显示UNIX套接字的文件位置:

mysqladmin version -u 'your user name' -p

result: 结果:

mysqladmin  Ver 8.42 Distrib 5.7.26, for Linux on x86_64
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version      5.7.26-0ubuntu0.19.04.1
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /var/run/mysqld/mysqld.sock
Uptime:         6 min 40 sec

Your user must have granted permissions. 您的用户必须已授予权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM