MySQL/Mariadb数据库维护

Mysql/Mariadb更改root用户密码:

    Alter user ‘root’@’localhost’ IDENTIFIED BY ‘密码’;

查询Mysql版本:

    Select version (); 

Mysql中如何查看某个表使用的引擎

           Show create table 表名;

在其他任何主机上以root身份登录Mysql,赋予任何主机访问Mysql root用户的权限

    Mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ WITH GRANT OPTION(‘root’@’localhost’表示限制其他主机登录MySQL 的 root用户,只允许在Mysql本地服务器登录root用户)

    Mysql>FLUSH PRIVILEGES  //刷新缓存,使修改生效

查看当前Mysql中所有的用户:

    SELECT DISTINCT CONCAT(‘User:’’’,user,’’’@’’’,host,’’’:’) AS query FROM mysql.user;

Mysql中用来显示所有活动进程(以及所有活动进程的线程ID、执行时间)

            SHOW PROCESSLIST

Mysql中显示表的相关信息

show table status like '表名' \G

数据库导出

mysqldump -u 用户名 -p 数据库名 > 导出的数据库脚本名.sql

Mysql内存配置参数:innodb_buffer_pool_size

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

 

[mysqld]

lower_case_table_names=1

character-set-server=utf8

# 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%.(为MySQL中最重要的数据缓存设置的RAM数量(内存数量)。 从专用服务器总RAM(内存)的70%开始,否则为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 = .....

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

"/usr/my.cnf" 29L, 993C                      

以上内容来自Mysql的配置文件/usr/my.cnf,其中有一项配置项#innodb_buffer_pool_size = 128M

用于缓存索引和数据的内存大小,数据读写在内存中非常快, 减少了对磁盘的读写(为Mysql分配内存的目的)。 当数据提交或满足检查点条件后才一次性将内存数据刷新到磁盘中。根据经验,推荐设置innodb-buffer-pool-size为服务器总可用内存的75%,如果服务器的总可用内存为8GB,那么建议设置的innodb_buffer_pool_size为6GB。

Mysql数据库中统计一个库中所有表的行数

    mysql> use information_schema;

    Database changed

    mysql> select table_name,table_rows from tables where TABLE_SCHEMA = 'database name 'order by table_rows desc;

用SUM函数计算指定列值的总和(总计)

  SELECT SUM(quantity) AS items_ordered FROM products WHERE order_num=20005;

计算符合特定条件的行的数目或确定表中行的数目

    COUNT()函数有2种使用方式:

  1. 使用COUNT(*)对表中行的数目进行计算,不管表列中包含的是空值(NULL)还是非空值;

    1. SELECT COUNT(*) AS num_cust FROM customers;

  2. 使用COUNT(column)对特定列中具有值的行进行计数,忽略NULL值;

    1. SELECT COUNT(cust_email) AS num_cust FROM customers;