当前位置:   article > 正文

MySQL 连接不上的常见问题_mysql连不上

mysql连不上

1.windows的ip问题
    ping Linux服务器的ip,检查网络是否通畅
    Linux服务器的网络问题
2.连接的用户是否有授权
    grant 
3.linux里的防火墙是否开启
    iptables -L
    service firewalld stop
4.检查下mysql服务是否开启
    ps aux|grep mysqld
5.检查下端口号是否修改
    netstat -anlput|grep mysqld
6.云服务器的安全组


1.windows里检查,打开cmd,ping Linux服务器ip,检查网络。

 Linux服务器检查,ping百度、windows机器等,检查网络。

  1. [root@mysql mysql]# ping www.baidu.com
  2. PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
  3. 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=128 time=62.2 ms
  4. 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=128 time=72.0 ms
  5. 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=3 ttl=128 time=26.5 ms
  6. 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=4 ttl=128 time=24.9 ms
  7. 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=5 ttl=128 time=25.9 ms
  8. 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=6 ttl=128 time=31.4 ms
  9. [root@mysql mysql]# ping 192.168.1.117
  10. PING 192.168.1.117 (192.168.1.117) 56(84) bytes of data.
  11. 64 bytes from 192.168.1.117: icmp_seq=1 ttl=128 time=0.658 ms
  12. 64 bytes from 192.168.1.117: icmp_seq=2 ttl=128 time=1.71 ms
  13. 64 bytes from 192.168.1.117: icmp_seq=3 ttl=128 time=1.84 ms
  14. 64 bytes from 192.168.1.117: icmp_seq=4 ttl=128 time=0.708 ms

文件socket:实现一台电脑里的不同进程之间通信的文件
网络socket:ip+端口( 实现跨宿主机之间通信)

  1. [root@mysql etc]# cat my.cnf
  2. [mysqld_safe]
  3. [client]
  4. #socket=/data/mysql/mysql.sock
  5. [mysqld]
  6. socket=/data/mysql/mysql.sock
  7. port = 3306
  8. open_files_limit = 8192
  9. innodb_buffer_pool_size = 512M
  10. character-set-server=utf8
  11. [mysql]
  12. auto-rehash
  13. prompt=\u@\d \R:\m  mysql>
  14. [root@mysql ~]# mysql -uroot -p123456sc
  15. mysql: [Warning] Using a password on the command line interface can be insecure.
  16. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
  17. # 使用文件socket
  18. [root@mysql ~]# mysql -uroot -p123456sc  -S /data/mysql/mysql.sock
  19. mysql: [Warning] Using a password on the command line interface can be insecure.
  20. Welcome to the MySQL monitor.  Commands end with ; or \g.
  21. Your MySQL connection id is 3
  22. Server version: 5.7.41 MySQL Community Server (GPL)
  23. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  24. Oracle is a registered trademark of Oracle Corporation and/or its
  25. affiliates. Other names may be trademarks of their respective
  26. owners.
  27. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  28. root@(none) 12:12  mysql>

[root@mysql ~]# mysql -h 192.168.102.139 -P3306 -uroot -p'your passwd'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1130 (HY000): Host 'mysql' is not allowed to connect to this MySQL server

创建一个用户并且授权

  1. # 连接MySQL
  2. [root@mysql ~]# mysql -uroot -p'your passwd'
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 7
  6. Server version: 5.7.41 MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. # 创建一个用户
  13. root@(none) 18:47  mysql>create user 'hanwl'@'%' identified by '123456sc';
  14. Query OK, 0 rows affected (0.00 sec)
  15. # 给这个用户授权
  16. root@(none) 18:48  mysql>grant all on *.* to 'hanwl'@'%';
  17. Query OK, 0 rows affected (0.00 sec)
  18. 'hanwl'@'%'
  19. % 是通配符,代表任意的字符串
  20. grant 是mysql里的授权命令
  21. all 代表授予所有的权限 select、insert、update、delete等
  22. on *.* 第1个* 代表库,第2个*代表库
  23. to 'hanwl'@'%' 给具体的用户
  24. [root@mysql ~]# mysql -h 192.168.102.139 -P3306 -uhanwl -p123456sc
  25. mysql: [Warning] Using a password on the command line interface can be insecure.
  26. Welcome to the MySQL monitor.  Commands end with ; or \g.
  27. Your MySQL connection id is 9
  28. Server version: 5.7.41 MySQL Community Server (GPL)
  29. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  30. Oracle is a registered trademark of Oracle Corporation and/or its
  31. affiliates. Other names may be trademarks of their respective
  32. owners.
  33. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  34. hanwl@(none) 18:49  mysql>

添加一条防火墙规则

  1. [root@mysql ~]# iptables -A INPUT -p tcp --dport 3306 -j DROP
  2. [root@mysql ~]# iptables -L -n -v
  3. Chain INPUT (policy ACCEPT 77 packets, 4484 bytes)
  4.  pkts bytes target     prot opt in     out     source               destination         
  5.     7   700 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:3306
  6. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  7.  pkts bytes target     prot opt in     out     source               destination         
  8. Chain OUTPUT (policy ACCEPT 48 packets, 3792 bytes)
  9.  pkts bytes target     prot opt in     out     source               destination         
  10. [root@mysql ~]# mysql -h 192.168.102.139 -P3306 -uhanwl -p123456sc
  11. mysql: [Warning] Using a password on the command line interface can be insecure.

去除刚添加的防火墙规则

  1. [root@mysql ~]# iptables -D INPUT -p tcp --dport 3306 -j DROP
  2. [root@mysql ~]# mysql -h 192.168.102.139 -P3306 -uhanwl -p123456sc
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 10
  6. Server version: 5.7.41 MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. hanwl@(none) 18:53  mysql>

mysql服务是否开启

看进程

  1. [root@mysql etc]# ps aux|grep mysqld
  2. root      10231  0.0  0.0  11824  1600 ?        S    12:41   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
  3. mysql     10387  0.1 13.2 1763324 246660 ?      Sl   12:41   0:23 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mysql.err --open-files-limit=8192 --pid-file=/data/mysql/mysql.pid --socket=/data/mysql/mysql.sock --port=3306
  4. root      10598  0.0  0.0 112832   988 pts/2    S+   16:31   0:00 grep --color=auto mysqld

看端口

  1. [root@mysql etc]# netstat -anlput|grep mysqld
  2. tcp6       0      0 :::3306                 :::*                    LISTEN      10387/mysqld        
  3. tcp6       0      0 192.168.102.139:3306    192.168.102.1:60719     ESTABLISHED 10387/mysqld  

直接进入MySQL

  1. [root@mysql ~]# mysql -uroot -p'Sanchuang1234#'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 5.7.37 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. root@(none) 18:47 mysql>

看日志

  1. [root@mysql mysql]# tail slave.err
  2. 2023-07-22T10:41:12.871873Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 143, Error_code: 2003
  3. 2023-07-22T10:42:15.913241Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 144, Error_code: 2003
  4. 2023-07-22T10:43:18.950869Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 145, Error_code: 2003
  5. 2023-07-22T10:44:21.983706Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 146, Error_code: 2003
  6. 2023-07-22T10:45:25.014526Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 147, Error_code: 2003
  7. 2023-07-22T10:46:28.043188Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 148, Error_code: 2003
  8. 2023-07-22T10:47:31.062752Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 149, Error_code: 2003
  9. 2023-07-22T10:47:34.379215Z 7 [Note] Access denied for user 'root'@'localhost' (using password: YES)
  10. 2023-07-22T10:48:34.081550Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 150, Error_code: 2003
  11. 2023-07-22T10:49:37.122049Z 1 [ERROR] Slave I/O for channel '': error connecting to master 'screpl@192.168.102.139:3306' - retry-time: 60 retries: 151, Error_code: 2003

看服务状态状态

  1. [root@mysql mysql]# systemctl status mysqld
  2. ● mysqld.service - LSB: start and stop MySQL
  3. Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
  4. Active: active (running) since 六 2023-07-22 15:22:47 CST; 3h 28min ago
  5. Docs: man:systemd-sysv-generator(8)
  6. Process: 6641 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
  7. Tasks: 32
  8. Memory: 272.4M
  9. CGroup: /system.slice/mysqld.service
  10. ├─6663 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/slave...
  11. └─6996 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/...
  12. 7月 22 15:22:43 slave systemd[1]: Starting LSB: start and stop MySQL...
  13. 7月 22 15:22:47 slave mysqld[6641]: Starting MySQL.... SUCCESS!
  14. 7月 22 15:22:47 slave systemd[1]: Started LSB: start and stop MySQL.

云服务器 --》检查安全组

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/89827
推荐阅读
相关标签
  

闽ICP备14008679号