mysql MySqlConnection判断mysql是否连接成功and查看连接数和状态等

MySqlConnection conn = new MySqlConnection(connstr) //新建一个MySqlConnection数据库连接

1、mysql> show status like '%connect%';

   Connections,试图连接到(不管是否成功)MySQL服务器的连接数。
   Max_used_connections,服务器启动后已经同时使用的连接的最大数量。
   Threads_connected,当前的连接数。

2、mysql> show variables like '%connect%';

   max_connections,最大连接数。

3、修改max_connections

   在配置文件(my.cnf或my.ini)在最下面,天加一句:
   max_connections=32000
   然后,用命令重启:/etc/init.d/mysqld restart
   虽然这里写的32000,
实际MySQL服务器允许的最大连接数16384;
   添加了最大允许连接数,对系统消耗增加不大。

4、mysql> show processlist;
显示当前正在执行的MySQL连接。

[sql]  view plain  copy
  1. mysql> show status like '%connect%';  
  2. +-----------------------------------------------+---------+  
  3. | Variable_name                                 | Value   |  
  4. +-----------------------------------------------+---------+  
  5. | Aborted_connects                              | 45643   |  
  6. | Connection_errors_accept                      | 0       |  
  7. | Connection_errors_internal                    | 0       |  
  8. | Connection_errors_max_connections             | 2275689 |  
  9. | Connection_errors_peer_address                | 15      |  
  10. | Connection_errors_select                      | 0       |  
  11. | Connection_errors_tcpwrap                     | 0       |  
  12. | Connections                                   | 145379  |  
  13. | Max_used_connections                          | 152     |  
  14. | Performance_schema_session_connect_attrs_lost | 0       |  
  15. | Ssl_client_connects                           | 0       |  
  16. | Ssl_connect_renegotiates                      | 0       |  
  17. | Ssl_finished_connects                         | 0       |  
  18. | Threads_connected                             | 7       |  
  19. +-----------------------------------------------+---------+  
  20. 14 rows in set (0.00 sec)  
  21.   
  22. mysql> show variables like '%connect%';  
  23. +-----------------------------------------------+-----------------+  
  24. | Variable_name                                 | Value           |  
  25. +-----------------------------------------------+-----------------+  
  26. | character_set_connection                      | utf8            |  
  27. | collation_connection                          | utf8_general_ci |  
  28. | connect_timeout                               | 10              |  
  29. | disconnect_on_expired_password                | ON              |  
  30. | init_connect                                  |                 |  
  31. | max_connect_errors                            | 100             |  
  32. | max_connections                               | 151             |  
  33. | max_user_connections                          | 0               |  
  34. | performance_schema_session_connect_attrs_size | 512             |  
  35. +-----------------------------------------------+-----------------+  
  36. rows in set (0.00 sec)  
  37.   
  38. mysql> show processlist;  
  39. +--------+------+-----------------+------+---------+------+-------+------------------+  
  40. | Id     | User | Host            | db   | Command | Time | State | Info             |  
  41. +--------+------+-----------------+------+---------+------+-------+------------------+  
  42. | 145367 | root | localhost       | NULL | Query   |    0 | init  | show processlist |  
  43. | 145373 | root | localhost:58275 | push | Sleep   |  179 |       | NULL             |  
  44. | 145374 | root | localhost:58276 | push | Sleep   |  179 |       | NULL             |  
  45. | 145375 | root | localhost:58278 | push | Sleep   |  179 |       | NULL             |  
  46. | 145376 | root | localhost:58280 | push | Sleep   |    0 |       | NULL             |  
  47. | 145377 | root | localhost:58279 | push | Sleep   |    2 |       | NULL             |  
  48. | 145378 | root | localhost:58282 | push | Sleep   |    0 |       | NULL             |  
  49. +--------+------+-----------------+------+---------+------+-------+------------------+  
  50. rows in set (0.00 sec)  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MySqlConnection时,可能会遇到无法并发连接的问题,这通常是由于连接池设置不当或数据库配置不当引起的。以下是一些可能导致并发连接问题的常见原因和解决方法: 1. 连接池设置不当 在使用连接池时,如果连接池的最大连接数设置过低,可能会导致并发连接问题。可以通过增加连接池的最大连接数来解决这个问题。例如: ``` MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); builder.Server = "localhost"; builder.UserID = "root"; builder.Password = "password"; builder.Database = "test"; builder.MaximumPoolSize = 100; MySqlConnection connection = new MySqlConnection(builder.ToString()); ``` 在上面的代码中,我们将连接池的最大连接数设置为100,这可以根据应用程序的需要进行调整。 2. 数据库配置不当 在MySQL中,有一些配置项可以影响并发连接的性能,例如max_connections、innodb_buffer_pool_size等。如果这些配置项设置不当,也可能会导致并发连接问题。可以通过修改这些配置项来解决这个问题。例如: ``` SET GLOBAL max_connections = 1000; SET GLOBAL innodb_buffer_pool_size = 2G; ``` 在上面的代码中,我们将最大连接数设置为1000,将InnoDB缓冲池大小设置为2G。 需要注意的是,修改MySQL的全局配置项需要有足够的权限,否则会提示“Access denied”错误。 总之,解决并发连接问题需要综合考虑多个因素,包括连接池设置、数据库配置等。可以根据具体情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值