Ubuntu服务器升级后mysql远程访问被关 SQLSTATE[HY000] [2002] Connection refused

1、首先确定数据库在本机可启动,并且开启systemctl restart mysql(重启命令)

systemctl restart mysql   重启
systemctl start mysql   启动
systemctl stop mysql    关闭
systemctl statu mysql   查阅状态

2、再次确定防火墙的问题(我不是这个问题)

sudo ufw status   查看状态
sudo ufw enable   启动
sudo ufw disable  关闭
常用开启端口
sudo ufw allow 22
sudo ufw allow 21
sudo ufw allow 3306
sudo ufw allow 443
sudo ufw allow 80
sudo ufw allow 6379
sudo ufw allow 60100:60200/tcp
开启ip
允许某个IP地址访问本机所有端口
sudo ufw allow from 192.168.0.1

3、查看了监听(发现了问题)ss -nultp | grep 3306

我这里是127.0.0.1 不是0.0.0.0 或者是制定的ip,所以导致问题无法远程链接

本来解决这个问题就是找到my.cnf文件里面的[mysqld]里面加上这么一句就可以的

bind-address=0.0.0.0

找到my.cnf文件用命令find -name my.cnf

但是因为系统升级my.cnf文件存在异常,目前是这个样子的,导致无法加

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

将这个文件删除(my.cnf)

然后在这个位置上重新创建这个文件,内容填充如下

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
bind-address=0.0.0.0
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

重启数据库,解决

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]中的描述,你在使用docker搭建的php项目中遇到了数据库连接问题。你尝试了一些解决方案,但都没有成功解决错误。其中一种解决方案是修改.env配置文件中的host,将host修改为127.0.0.1,但这并没有解决你的问题,仍然报错SQLSTATE\[HY000\] \[2002\] Connection refused。另一种解决方案是将host修改为localhost,但这也没有解决你的问题,报错为sqlstate\[hy000\] \[2002\] No such file or directory。你还尝试了将host变更为本地mysql容器名,但仍然报错SQLSTATE\[HY000\] \[2002\] Connection refused。 根据引用\[2\]和引用\[3\]中的描述,你还遇到了MySQL启动错误的问题,报错为Starting MySQL.2018-07-02T02:33:49.152186Z mysqld_safe error: log-error set to '/usr/local/mysql/logs/mysqld.log', however file don't exists. Create writable for user 'mysql'。这个错误可能与MySQL的日志文件路径设置有关。 综上所述,你遇到的错误是SQLSTATE\[HY000\] \[2002\] Connection refused。这可能是由于数据库连接配置不正确或者数据库服务未正确启动导致的。你可以检查数据库连接配置是否正确,并确保数据库服务已正确启动。如果问题仍然存在,你可以尝试查看数据库服务的日志文件,以获取更多的错误信息来解决问题。 #### 引用[.reference_title] - *1* [php连接mysql报错连接超时:报错SQLSTATE[HY000] [2002] Connection timed out问题的一个解决思路](https://blog.csdn.net/qq_45756298/article/details/126352342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [[乐意黎原创]MySQL Error: SQLSTATE[HY000] [2002] Connection refused及server quit without updating PID...](https://blog.csdn.net/aerchi/article/details/80805888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值