• mysql常用操作


    mysql --help 

    Default options are read from the following files in the given order:

    /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf

    备份和还原

    backup

    mysqldump -u root -p --databases test mysql > D:ackup.sql

     

    restore

    mysql -u root -p  [dbname]  < C:ackup.sql

    启动时指定配置文件

    mysqld [--defaults-file=/etc/my.cnf]

    # which mysqld
    /usr/local/mysql/bin/mysqld
    # /usr/local/mysql/bin/mysqld --verbose --help |grep -A 1 'Default options'
     
     
     

    查看当前引擎

    show engines;

     

    查看配置参数

    show variables like '%open%'; 

    show status like 'open%';

    配置table_open_cache

    set global table_open_cache=10240;

    查看当前状态及连接数

    • mysql> status; show full processlist; 
    • Workbench中的Client Connections
    • ...

    执行SQL

    mysql> . file_name 

    创建用户并授权

    create user 'username'@'where' identified by 'aaa7B2249'; 

    grant all on pcom.* to 'pcom'@'192.168.0.0/255.255.0.0' identified by 'aaa7B2249';

    修改密码

    方法一: 
    (适用于管理员或者有全局权限的用户重设其它用户的密码)
    进入命令行模式
    mysql -u root
    -p
     
    mysql>use mysql;
    mysql> UPDATE user SET
    password=PASSWORD("new password") WHERE user='username';
    mysql> FLUSH
    PRIVILEGES;
    mysql> quit;
     
    方法二:
    mysql -u root -p 
    mysql>use
    mysql; 
    mysql> SET PASSWORD FOR   username=PASSWORD('new
    password');
    mysql> QUIT
     
     
     方法三:
     mysqladmin -u root "old
    password" "new password"
     
    注:new password请输入你想要设置的密码。

    方法四:

    mysql> use mysql; 
    mysql> update user set password=password('123') where user='root' and host='localhost'; 
    mysql> flush privileges;  

    字符集设置 

    my.ini

      

    # 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.

    [client]
    default-character-set=utf8

    [mysql]
    default-character-set=utf8

    [mysqld]
    character-set-server=utf8

    #日志配置

    log-error="C:/D/lamp/log/error.log"  

    general-log=1  

    general-log-file="C:/D/lamp/log/mysql.log"  

    long_query_time=3  

    slow_query_log=1  

    slow-query-log_file="C:/D/lamp/log/slowquery.log"  

    # 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 = D:mysql-5.6.32-winx64
    datadir = D:mysql-5.6.32-winx64data
    # port = .....
    # server_id = .....

    # 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

    ===================================================================================================

    1 连接 Connections

    经常会遇见”mysql: error 1040: too many connections”的情况,一种是访问量确实很高,mysql服务器抗不住,这个时候就要考虑增加从服务器分散读压力,另外一种情况是mysql配置文件中max_connections值过小: 

    mysql> show variables like ‘max_connections‘;

    +-----------------+-------+

    | variable_name  | value   |

    +-----------------+-------+

    | max_connections | 256  |

    +-----------------+-------+ 

    这台mysql服务器最大连接数是256,然后查询一下服务器响应的最大连接数:

     mysql> show global status like ‘max_used_connections‘; 

    mysql服务器过去的最大连接数是245,没有达到服务器连接数上限256,应该没有出现1040错误,比较理想的设置是

     max_used_connections / max_connections * 100% ≈ 85% 

    最大连接数占上限连接数的85%左右,如果发现比例在10%以下,mysql服务器连接数上限设置的过高了。

    2  线程 Thread

    mysql> show global status like ‘thread%‘;

    +-------------------+-------+

    | variable_name   |   value |

    +-------------------+-------+

    | threads_cached  |    46  |

    | threads_connected | 2   |

    | threads_created | 570   |

    | threads_running  | 1     |

    +-------------------+-------+ 

    如果我们在mysql服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。

    threads_created表示创建过的线程数,如果发现threads_created值过大的话,表明mysql服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,

    查询服务器 thread_cache_size 配置: 

    mysql> show variables like ‘thread_cache_size‘;

    +-------------------+-------+

    | variable_name   | value   |

    +-------------------+-------+

    | thread_cache_size | 64   |

    +-------------------+-------+ 

    示例中的服务器还是挺健康的。

    3  缓存 cache

    3.1 文件打开数

    mysql> show global status like ‘open_files‘;

    +---------------+-------+

    | variable_name | value |

    +---------------+-------+

    | open_files   | 1410  |

    +---------------+-------+

     mysql> show variables like ‘open_files_limit‘;

    +------------------+-------+

    | variable_name   | value |

    +------------------+-------+

    | open_files_limit    | 4590 |

    +------------------+-------+ 

    比较合适的设置:open_files / open_files_limit * 100% <= 75%

    3.2 数据表

    3.2.1 打开数 open_tables

    mysql> show global status like ‘open%tables%‘;

    +---------------+-------+

    | variable_name | value   |

    +---------------+-------+

    | open_tables  | 919  |

    | opened_tables | 1951 |

    +---------------+-------+ 

    open_tables: 打开表的数量

    opened_tables: 打开过的表数量

    如果 opened_tables 数量过大,说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小,我们查询一下服务器table_cache值: 

    mysql> show variables like ‘table_cache‘;

    +---------------+-------+

    | variable_name | value |

    +---------------+-------+

    | table_cache    | 2048  |

    +---------------+-------+ 

    比较合适的值为: 

    open_tables / opened_tables * 100% >= 85% 

    open_tables / table_cache * 100% <= 95%

     3.2.2 临时表 tmp_table

    mysql> show global status like ‘created_tmp%‘;

    +-------------------------+---------+

    | variable_name      | value   |

    +-------------------------+---------+

    | created_tmp_disk_tables | 21197  |

    | created_tmp_files    | 58    |

    | created_tmp_tables    | 1771587 |

    +-------------------------+---------+ 

    每次创建临时表,created_tmp_tables 增加,如果是在磁盘上创建临时表,created_tmp_disk_tables也增加,created_tmp_files表示mysql服务创建的临时文件文件数,比较理想的配置是: 

    created_tmp_disk_tables / created_tmp_tables * 100% <= 25% 

    比如上面的服务器 created_tmp_disk_tables / created_tmp_tables * 100% = 1.20%,应该相当好了。我们再看一下mysql服务器对临时表的配置: 

    mysql> show variables where variable_name in (‘tmp_table_size‘, ‘max_heap_table_size‘);

    +---------------------+-----------+

    | variable_name    | value      |

    +---------------------+-----------+

    | max_heap_table_size | 268435456 |

    | tmp_table_size    | 536870912 |

    +---------------------+-----------+ 

    只有 256mb 以下的临时表才能全部放内存,超过的就会用到硬盘临时表。 

    3.2.3 表锁情况

    mysql> show global status like ‘table_locks%‘;

    +-----------------------+-----------+

    | variable_name     | value    |

    +-----------------------+-----------+

    | table_locks_immediate | 490206328 |

    | table_locks_waited  | 2084912  |

    +-----------------------+-----------+

     table_locks_immediate 表示立即释放表锁数, 

    table_locks_waited 表示需要等待的表锁数, 

    如果 table_locks_immediate / table_locks_waited > 5000,最好采用innodb引擎,因为innodb是行锁而myisam是表锁,对于高并发写入的应用innodb效果会好些。 

    示例中的服务器 table_locks_immediate / table_locks_waited = 235,myisam就足够了。

    3.2.4 表扫描情况

    mysql> show global status like ‘handler_read%‘;

    +-----------------------+-------------+

    | variable_name     | value     |

    +-----------------------+-------------+

    | handler_read_first  | 5803750     |

    | handler_read_key   | 6049319850  |

    | handler_read_next   | 94440908210 |

    | handler_read_prev   | 34822001724 |

    | handler_read_rnd   | 405482605  |

    | handler_read_rnd_next | 18912877839 |

    +-----------------------+-------------+

     各字段解释参见 http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html ,调出服务器完成的查询请求次数: 

    mysql> show global status like ‘com_select‘;

    +---------------+-----------+

    | variable_name | value      |

    +---------------+-----------+

    | com_select     | 222693559 |

    +---------------+-----------+ 

    计算表扫描率: 

    表扫描率 = handler_read_rnd_next / com_select 

    如果表扫描率超过 4000,说明进行了太多表扫描,很有可能索引没有建好,增加 read_buffer_size 值会有一些好处,但最好不要超过8mb。 

    3.3 key_buffer_size

    key_buffer_size是对myisam表性能影响最大的一个参数,下面一台以myisam为主要存储引擎服务器的配置: 

    mysql> show variables like ‘key_buffer_size‘; 

    +-----------------+------------+

    | variable_name  | value    |

    +-----------------+------------+

    | key_buffer_size | 536870912 |

    +-----------------+------------+ 

    分配了 512mb 内存给 key_buffer_size ,我们再看一下 key_buffer_size 的使用情况: 

    mysql> show global status like ‘key_read%‘;

    +------------------------+-------------+

    | variable_name     | value     |

    +------------------------+-------------+

    | key_read_requests   | 27813678764 |

    | key_reads       | 6798830    |

    +------------------------+-------------+

     一共有 27813678764个 索引读取请求,有 6798830个 请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的概率: 

    key_cache_miss_rate = key_reads / key_read_requests * 100% 

    比如上面的数据,key_cache_miss_rate为0.0244%,4000个索引读取请求才有一个直接读硬盘,已经很bt了,key_cache_miss_rate在0.1%以下都很好(每1000个请求有一个直接读硬盘),如果key_cache_miss_rate在0.01%以下的话,key_buffer_size分配的过多,可以适当减少。 

    【注意】key_read_buffer 默认值为 8M 。在专有的数据库服务器上,该值可设置为 RAM * 1/4

    mysql服务器还提供了key_blocks_*参数: 

    mysql> show global status like ‘key_blocks_u%‘;

    +------------------------+-------------+

    | variable_name     | value     |

    +------------------------+-------------+

    | key_blocks_unused  | 0       |

    | key_blocks_used    | 413543     |

    +------------------------+-------------+ 

    key_blocks_unused 表示未使用的缓存簇(blocks)数

    key_blocks_used 表示曾经用到的最大的blocks数

    比如这台服务器,所有的缓存都用到了,要么增加 key_buffer_size,要么就是过渡索引了,把缓存占满了。比较理想的设置: 

    key_blocks_used / (key_blocks_unused + key_blocks_used) * 100% ≈ 80%

    3.4 排序使用情况 sort_buffer

    mysql> show global status like ‘sort%‘;

    +-------------------+------------+

    | variable_name   | value    |

    +-------------------+------------+

    | sort_merge_passes | 29    |

    | sort_range    | 37432840  |

    | sort_rows     | 9178691532 |

    | sort_scan     | 1860569   |

    +-------------------+------------+ 

    sort_merge_passes 包括两步。mysql 首先会尝试在内存中做排序,使用的内存大小由系统变量 sort_buffer_size 决定,如果它的大小不够把所有的记录都读到内存中,mysql 就会把每次在内存中排序的结果存到临时文件中,等 mysql 找到所有记录之后,再把临时文件中的记录做一次排序。这再次排序就会增加 sort_merge_passes。实际上,mysql 会用另一个临时文件来存再次排序的结果,所以通常会看到 sort_merge_passes 增加的数值是建临时文件数的两倍。因为用到了临时文件,所以速度可能会比较慢,增加 sort_buffer_size 会减少 sort_merge_passes 和 创建临时文件的次数。但盲目的增加 sort_buffer_size 并不一定能提高速度,见 how fast can you sort data with mysql?(引自http://qroom.blogspot.com/2007/09/mysql-select-sort.html ,貌似被墙) 

    另外,增加read_rnd_buffer_size(3.2.3是record_rnd_buffer_size)的值对排序的操作也有一点的好处,参见:http://www.mysqlperformanceblog.com/2007/07/24/what-exactly-is-read_rnd_buffer_size/

    3.5 查询缓存

    mysql> show global status like ‘qcache%‘;

    +-------------------------+-----------+

    | variable_name     | value    |

    +-------------------------+-----------+

    | qcache_free_blocks    | 22756   |

    | qcache_free_memory | 76764704  |

    | qcache_hits      | 213028692 |

    | qcache_inserts     | 208894227 |

    | qcache_lowmem_prunes | 4010916  |

    | qcache_not_cached  | 13385031  |

    | qcache_queries_in_cache | 43560  |

    | qcache_total_blocks  | 111212    |

    +-------------------------+-----------+

     mysql 查询缓存变量解释: 

    qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。flush query cache会对缓存中的碎片进行整理,从而得到一个空闲块。 

    qcache_free_memory:缓存中的空闲内存。 

    qcache_hits:每次查询在缓存中命中时就增大 

    qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是命中比率。 

    qcache_lowmem_prunes:缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个数字在不断增长,就表示可能碎片非常严重,或者内存很少。(上面的 free_blocks和free_memory可以告诉您属于哪种情况) 

    qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 select 语句或者用了now()之类的函数。 

    qcache_queries_in_cache:当前缓存的查询(和响应)的数量。 

    qcache_total_blocks:缓存中块的数量。 

    我们再查询一下服务器关于query_cache的配置: 

    mysql> show variables like ‘query_cache%‘;

    +------------------------------+-----------+

    | variable_name        | value    |

    +------------------------------+-----------+

    | query_cache_limit      | 2097152  |

    | query_cache_min_res_unit | 4096   |

    | query_cache_size       | 203423744 |

    | query_cache_type     | on     |

    | query_cache_wlock_invalidate | off     |

    +------------------------------+----------+ 

    各字段的解释: 

    query_cache_limit:超过此大小的查询将不缓存 

    query_cache_min_res_unit:缓存块的最小大小 

    query_cache_size:查询缓存大小 

    query_cache_type:缓存类型,决定缓存什么样的查询,示例中表示不缓存 select sql_no_cache 查询

    query_cache_wlock_invalidate:当有其他客户端正在对myisam表进行写操作时,如果查询在query cache中,是否返回cache结果还是等写操作完成再读表获取结果。

    query_cache_min_res_unit的配置是一柄”双刃剑”,默认是4kb,设置值大对大数据查询有好处,但如果你的查询都是小数据查询,就容易造成内存碎片和浪费。

    查询缓存碎片率 = qcache_free_blocks / qcache_total_blocks * 100% 

    如果查询缓存碎片率超过20%,可以用flush query cache整理缓存碎片,或者试试减小query_cache_min_res_unit,如果你的查询都是小数据量的话。 

    查询缓存利用率 = (query_cache_size - qcache_free_memory) / query_cache_size * 100% 

    查询缓存利用率在25%以下的话说明query_cache_size设置的过大,可适当减小;查询缓存利用率在80%以上而且qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。

    查询缓存命中率 = (qcache_hits - qcache_inserts) / qcache_hits * 100% 

    示例服务器 查询缓存碎片率 = 20.46%,查询缓存利用率 = 62.26%,查询缓存命中率 = 1.94%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。

    4 其他

    4.1 read_buffer_size

    4.2 慢查询

    mysql> show variables like ‘%slow%‘;

    +------------------+-------+

    | variable_name    | value  |

    +------------------+-------+

    | log_slow_queries | on   |

    | slow_launch_time | 2    |

    +------------------+-------+

    mysql> show global status like ‘%slow%‘;

    +---------------------+-------+

    | variable_name    | value   |

    +---------------------+-------+

    | slow_launch_threads | 0     |

    | slow_queries    | 4148  |

    +---------------------+-------+ 

    配置中打开了记录慢查询,执行时间超过2秒的即为慢查询,系统显示有4148个慢查询,你可以分析慢查询日志,找出有问题的sql语句,慢查询时间不宜设置过长,否则意义不大,最好在5秒以内,如果你需要微秒级别的慢查询,可以考虑给mysql打补丁:http://www.percona.com/docs/wiki/release:start,记得找对应的版本。 

    打开慢查询日志可能会对系统性能有一点点影响,如果你的mysql是主-从结构,可以考虑打开其中一台从服务器的慢查询日志,这样既可以监控慢查询,对系统性能影响又小。

     

  • 相关阅读:
    kakfa 安全机制
    配置管理
    消费者基本操作
    生产者基本操作
    笔记:类加载器
    主题管理
    记一次学习SpringCloud将zk作为注册中心的bug
    JVM新生代进入老年代、何时触发Full GC?
    JVM调优
    线程池
  • 原文地址:https://www.cnblogs.com/silva/p/5811487.html
Copyright © 2020-2023  润新知