我已经设置了一个运行完美的 Google Cloud SQL,但是我在使用文档中给出的任何方法进行连接时遇到了问题:
磷酸二氢钾
$db = new PDO('mysql:unix_socket=/cloudsql/hello-php:my-cloudsql-instance;charset=utf8','<username>','<password>');
Run Code Online (Sandbox Code Playgroud)
mysql_连接
$conn = mysql_connect(':/cloudsql/hello-php:my-cloudsql-instance', '<username>', '<password>');
Run Code Online (Sandbox Code Playgroud)
mysqli
$sql = new mysqli(null, '<username>', '<password>', null, null, '/cloudsql/hello-php:my-cloudsql-instance');
Run Code Online (Sandbox Code Playgroud)
我可以从 MySQL Workbench 进行远程连接,但每当我尝试使用上述功能从网站上进行连接时,我都会收到“在‘读取初始通信数据包’时与 MySQL 服务器失去连接”的消息 - 但我通过授权网络授予了 Cloud SQL 访问权限并检查了 IP给予。我相信这与我不是尝试从 Google App Engine 而是从我自己的开发数据库进行连接这一事实有关。如何从 Google 外部进行连接!?
更新:我已经通过各种方式验证了我的 IP,但此代码仍然无法正常工作。
$host="173.xxx.xx.xxx" <-- given from Google Developers COnsole
$db_username="root";
$db_pasword="xxxx"; <-- password I set after creating the instance
mysql_connect("$host", "$db_username", "$db_password")or die("cannot connect");
Run Code Online (Sandbox Code Playgroud)
该页面给出错误:
警告:mysql_connect() [function.mysql-connect]:在“读取初始通信数据包”时失去与 MySQL 服务器的连接,系统错误:/..../connect.php 第 5 行中的 111 无法连接
当我尝试此 PDO 连接时,我也收到错误: …
现在,我承认我对使用 Google Cloud SQL 还很陌生,但我在本地运行 MySQL 服务器方面拥有多年的经验,我认为自己几乎是这方面的专家。
现在,几天前我已经将手机游戏 RummyFight 使用的数据库上传到 Google Cloud SQL。MySQL 数据库通常在仅具有 2 GB RAM 的 2GHz PC (Win Server 2000) 上运行。在高峰时段,它每秒处理大约 250 个请求,并且由于它构建良好,索引使用良好,我的本地计算机已经能够应对它。
这些天我一直在监控Cloud SQL,我发现性能时不时会出现非常严重的下降。几乎就好像它静止不动一样。我玩游戏的客户也有同样的经历。
请看一下随附的今晚的屏幕截图,并向我解释这是否正常。我确实需要一个可靠的数据库,我不知道我对Google的云SQL要求是否太高。也许它更多的是为了数量而不是速度。
请指教。
PS 云数据库配置为具有异步写入功能的 D4-2 GB RAM。
PS #2:是的,我已经排除了问题出在网络服务器中的可能性,因为它与本地数据库中使用的相同。此外,最大负载仅为 2%。
我发现,如果您通过 SSL 连接到 Cloud SQL 实例,服务器证书中提供的 CommonName 会my-project-123456:myinstance导致证书变得不可验证,因为客户端期望 CN 是主机名或 IP。
这个问题的每个解决方案似乎都相当于“只是禁用验证”,这对我来说是不可接受的,因为:
为什么 GCP 决定正确执行其他所有操作(提供 CA 证书和客户端证书),却在身份验证方面失败了?通过禁用验证,您基本上是在说“我同意在某个时候接受 MITMed”。
由于 PIPA/HIPAA 的原因,我们无法快速、宽松地进行验证的项目怎么办?
不支持关闭验证的 mySQL 客户端怎么办?例如:在即将发布的 5.6.16 版本之前,所有使用 mysqlnd 的 PHP 5.6 mysql 库。
有没有办法让 SSL 在 Cloud SQL 上正常工作?
是否可以计算按 json 键分组的 json 值的总和?
Google云sql上的Mysql版本是5.7.17。
示例_1:我的观点的一个简短示例:
col1 | col2
-----|-----------------------
aaa | {"key1": 1, "key2": 3}
-----|-----------------------
bbb | {"key1": 0, "key2": 2}
-----|-----------------------
aaa | {"key1": 50, "key2": 0}
Run Code Online (Sandbox Code Playgroud)
SQL 查询应该产生:
col1 | col2
-----|-----------------------
aaa | {"key1": 51, "key2": 3}
-----|-----------------------
bbb | {"key1": 0, "key2": 2}
Run Code Online (Sandbox Code Playgroud)
或者
以下任何模式都可以吗?
示例_2:
col1 | col2
-----|-----------------------
aaa | {{"key_name" : "key1", "key_value" : 1}, {"key_name" : "key2", "key_value" : 3}}
-----|-----------------------
bbb | {{"key_name" : "key1", "key_value" : …Run Code Online (Sandbox Code Playgroud) 我需要在运行我们的应用程序之前更新我的数据库架构。为此,基于此线程和此答案,我决定使用 init 容器来完成这项工作。
由于我的 SQL 实例是托管的 Google Cloud SQL 实例,因此我需要gce-proxy能够连接到数据库。我的 initContainers 看起来像这样:
initContainers:
- name: cloudsql-proxy-init
image: gcr.io/cloudsql-docker/gce-proxy:1.09
command: ["/cloud_sql_proxy"]
args:
- --dir=/cloudsql
- -instances=xxxx:europe-west1:yyyyy=tcp:5432
- -credential_file=/secrets/cloudsql/credentials.json
volumeMounts:
- name: dev-db-instance-credentials
mountPath: /secrets/cloudsql
readOnly: true
- name: ssl-certs
mountPath: /etc/ssl/certs
- name: cloudsql
mountPath: /cloudsql
- name: liquibase
image: eu.gcr.io/xxxxx/liquibase:v1
imagePullPolicy: Always
command: ["./liquibase.sh"]
env:
- name: DB_TYPE
value: postgresql
- name: DB_URL
value: jdbc:postgresql://localhost/test
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password …Run Code Online (Sandbox Code Playgroud) 我已阅读Google Cloud SQL网站上的所有文档,现在了解如何创建和管理只读副本,但我还没有看到任何有关如何使用它们的信息,
我无法将 Spring Boot + Spring Data 应用程序部署到 Google App Engine 并使该应用程序连接到 Google CloudSql (MySQL) 数据库。
GAE 应用程序和 CloudSql 实例位于同一项目中。
我可以运行整个应用程序并从本地主机连接到 CloudSql,没有任何问题。
没有数据库连接的 Spring Boot 在 GAE 上可以正常工作,不会出现错误。添加 Spring Data 会引发“com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败”错误。
在 pom.xml 中我包含了:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
另外,我尝试仅使用 mysql 依赖项:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在我的研究中,我没有发现任何关于 Spring Boot + Spring Data + GAE + CloudSql 的好的例子或相关问题。
application.properties 文件中连接到 CloudSql DB 的正确设置是什么?我尝试了以下方法,但都抛出了各种错误:
spring.jpa.hibernate.ddl-auto:none
# tried with GoogleDriver and without:
# spring.datasource.driver-class-name=com.mysql.jdbc.GoogleDriver
# urls configs attempted: …Run Code Online (Sandbox Code Playgroud) google-app-engine spring spring-data google-cloud-sql spring-boot
在 Google 的 SQL Cloud Postgres 服务中,当我通过 Web 控制台为 PostgreSQL 实例创建数据库时,它会自动将数据库的默认“公共”架构的所有者设置为 cloudsqladmin。看来我无法更改所有权:
mydb=> \dn
List of schemas
Name | Owner
--------+---------------
public | cloudsqladmin
(1 row)
mydb=> alter schema public owner to postgres;
ERROR: must be owner of schema public
mydb=> \du
List of roles
Role name | Attributes | Member of
-------------------+------------------------------------------------+---------------------
cloudsqladmin | Superuser, Create role, Create DB, Replication | {}
cloudsqlagent | Create role, Create DB | {cloudsqlsuperuser}
cloudsqlreplica | Replication | {}
cloudsqlsuperuser | Create role, …Run Code Online (Sandbox Code Playgroud) 我在本地运行 Google 的Cloud SQL 代理,它使用如下命令处理本地提供的 Firebase 函数:
/cloud_sql_proxy -instances=my-project-12345:us-central1:my-instance=tcp:1433
Run Code Online (Sandbox Code Playgroud)
但是我真的不知道如何在部署的 Firebase 函数上进行这项工作。
export const typeOrmConnectionOptions: ConnectionOptions = {
name: 'primary',
type: 'mssql',
host: '127.0.0.1',
port: 1433,
username: 'sqlserver',
password: 'my$trongPa$$word',
database: 'TestDB',
synchronize: true,
logging: true,
entities: ['lib/entity/**/*.js'],
...(prod && {
extra: {
socketPath:
'/cloudsql/my-project-12345:us-central1:my-instance=tcp',
credential_file: './admin-service-account-file-long-a1b2c3-hash.json'
}
})
};
Run Code Online (Sandbox Code Playgroud)
就将凭证文件作为 extras 对象的一部分传递给 TypeORM 的连接对象而言,我真的是在黑暗中拍摄,但是我觉得必须有这样的事情才能链接我在下面创建的服务帐户 此步骤后到数据库查询。
我的另一个远见是使用环境变量来设置使用此 JSON 文件的凭据:
process.env.GOOGLE_APPLICATION_CREDENTIALS = fs.readFileSync(
'./admin-service-account-file-long-a1b2c3-hash.json',
'utf8'
)
Run Code Online (Sandbox Code Playgroud)
没有喜悦。
我不认为错误消息有多大帮助,因为我确定我尝试这样做的方式从根本上是不正确的,但是对于它的价值,上面得到了错误
"Failed to connect to 127.0.0.1:1433 - connect ECONNREFUSED …Run Code Online (Sandbox Code Playgroud) javascript node.js google-cloud-sql firebase google-cloud-platform
我在 Google Cloud 中部署了一个 PostgreSQL 数据库,我试图从 Cloud Run 实例连接到该数据库。我尝试了以下两个包,它们最终都导致了相同的异常:
我得到的例外是:
SocketException: Failed host lookup: '/cloudsql/{INSTANCE_CONNECTION_NAME}' (OS Error: Name or service not known, errno = -2)
我在尝试建立连接时两次都到这里,所以在第一个包的情况下:
connection = new PostgreSQLConnection(
'/cloudsql/{INSTANCE_CONNECTION_NAME}',
5432,
'postgres',
username: 'username',
password: 'password');
await connection.open(); // <-- exception thrown here
Run Code Online (Sandbox Code Playgroud)
我曾尝试将主机字符串更改为/cloudsql/INSTANCE_CONNECTION_NAME}/.s.PGSQL.5432,但这不起作用。我首先想到的是权限,Cloud Run 实例使用的服务帐户 (xxx-compute@developer.gserviceaccount.com) 具有该Cloud SQL Editor角色(也尝试过 Client 和 Admin)。
从 dart 控制台应用程序在本地运行相同的数据库代码,我可以通过它的公共 IP 地址连接到我的数据库作为带有两个包的主机,因此数据库本身已启动并正在运行。
有人可以通过此异常为我指出正确的方向/有上述任何包的示例代码,以展示如何将其从 Cloud Run 连接到 Cloud SQL 实例?
编辑:我尝试在本地设置代理以测试连接是否错误,如下所示:
.\cloud_sql_proxy.exe -instances={INSTANCE_CONNECTION_NAME}=tcp:5433 psql
Run Code Online (Sandbox Code Playgroud)
然后将代码中的连接主机值更改为localhost,并将端口更改为 …
google-cloud-sql ×10
mysql ×3
postgresql ×2
cloud ×1
dart ×1
firebase ×1
javascript ×1
json ×1
kubernetes ×1
node.js ×1
spring ×1
spring-boot ×1
spring-data ×1
ssl ×1