我有时候在linux shell写些针对于mysql数据库的操作,shell脚本要多次运行mysql -e,但这样的话,每次都要连接数据库,很浪费资源,有没有办法维持一个长连接并以后一直用这个长连接呢?

解决方案 »

  1.   

    想直接用mysql -e来保持长连接不可以
    考虑别的方式吧
      

  2.   

    $ mysql test -e "select * from tb;desc user;show tables;"
    +------+------+
    | rid  | con  |
    +------+------+
    |    1 | NULL |
    |    2 | NULL |
    |    2 | NULL |
    |    3 | NULL |
    |    4 | NULL |
    |    4 | NULL |
    |    4 | NULL |
    +------+------+
    +----------+----------------------+------+-----+---------+-----------
    | Field    | Type                 | Null | Key | Default | Extra
    +----------+----------------------+------+-----+---------+-----------
    | userid   | smallint(6) unsigned | NO   | PRI | NULL    | auto_incre
    | username | varchar(20)          | NO   |     | NULL    |
    +----------+----------------------+------+-----+---------+-----------
    +---------------------------+
    | Tables_in_test            |
    +---------------------------+
    | a                         |
    | animals                   |
    | b                         |
    | cmplcate                  |
    | columns_priv              |
    | contest_skill             |
    | db                        |
    | dd                        |
    | event                     |
    | feeder_update             |
    | forum                     |
    | func                      |
    | general_log               |
    | gss_documen_cate          |
    | gss_documentation         |
    | help_category             |
    | help_keyword              |
    | help_relation             |
    | help_topic                |
    | host                      |
    | human_employee_master     |
    | item                      |
    | ndb_binlog_index          |
    | notice                    |
    | pet                       |
    | plugin                    |
    | proc                      |
    | procs_priv                |
    | product                   |
    | pyk                       |
    | search                    |
    | servers                   |
    | shop                      |
    | slow_log                  |
    | stock                     |
    | student                   |
    | system_user               |
    | t                         |
    | t1                        |
    | t2                        |
    | t7974                     |
    | t_edition                 |
    | t_rank                    |
    | ta                        |
    | taa                       |
    | tab                       |
    | tables_priv               |
    | tb                        |
    | test                      |
    | text                      |
    | time_zone                 |
    | time_zone_leap_second     |
    | time_zone_name            |
    | time_zone_transition      |
    | time_zone_transition_type |
    | treenodes                 |
    | tt                        |
    | tt1t                      |
    | ttt                       |
    | tttt                      |
    | type                      |
    | url                       |
    | user                      |
    | viewlog                   |
    +---------------------------+
    你可以向我这样执行多条语句