mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql相关的知识,希望对你有一定的参考价值。

mysql用户和权限管理


库和表级别:TABLE or DATABASE

ALTER

CREATE 

CREATE VIEW

DROP

INDEX

SHOW VIEW

GRANT OPTION:能够把自己获得的权限赠经其他用户一个副本;


数据操作:

SELECT

INSERT

DELETE

UPDATE


字段级别:

SELECT(col1,col2,...)

UPDATE(col1,col2,...)

INSERT(col1,col2,...)


所有有限:ALL PRIVILEGES, ALL


元数据数据库:mysql.

授权表:

db, host, user

columns_priv, tables_priv, procs_priv, proxies_priv

-------------------------------------------------------------------------------------------

用户账号:

‘USERNAME‘@‘HOST‘:

@‘HOST‘:主机名;

IP地址或Network; 

通配符:%, _: 172.16.%.%

---------------------------------------------------------------------------------------

创建用户:CREATE USER ‘USERNAME‘@‘HOST‘ [IDENTIFIED BY ‘password‘];


查看用户获得的授权:SHOW GRANTS FOR ‘USERNAME‘@‘HOST‘


用户重命名:RENAME USER old_user_name TO new_user_name


删除用户:DROP USER ‘USERNAME‘@‘HOST‘

-------------------------------------------------------------------------------------------

修改密码:

(1) SET PASSWORD FOR ""@"" password(‘your_password‘)

(2) UPDATE mysql.user SET password=PASSWORD(‘your_password‘) WHERE user="" and host="";

              flush privileges;

(3) mysqladmin password your_password

mysqladmin [OPTIONS] command command....

-u, -h, -p


忘记管理员密码的解决办法:

(1) 启动mysqld进程时,为其使用:--skip-grant-tables(用户不被验证登陆) --skip-networking(禁止远程)

(2) 使用UPDATE命令修改管理员密码

            (3) 关闭mysqld进程,移除上述两个选项,重启mysqld;


密码修改

[[email protected] ~]# mysqladmin password redhat


[[email protected] ~]# mysql -p

Enter password: 

ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

[[email protected] ~]# vim /etc/rc.d/init.d/mysqld

case "$mode" in

  ‘start‘)

    # Start daemon


    # Safeguard (relative paths, core dumps..)

    cd $basedir


    echo $echo_n "Starting MySQL"

    if test -x $bindir/mysqld_safe

    then

      # Give extra arguments to mysqld with the my.cnf file. This script

      # may be overwritten at next upgrade.

      $bindir/mysqld_safe --skip-grant-tables --skip-networking --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

 

[[email protected] ~]# vim /etc/rc.d/init.d/mysqld 

[[email protected] ~]# service mysqld restart

Shutting down MySQL..                                      [确定]

Starting MySQL..                                           [确定]


[[email protected] ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 10.0.13-MariaDB-log Source distribution


Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.


Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


MariaDB [(none)]> update mysql.user set password=password("aaa") where user=‘root‘ and host=‘localhost‘;

Query OK, 1 row affected (0.04 sec)

Rows matched: 1  Changed: 1  Warnings: 0


MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)


MariaDB [(none)]> quit

Bye

[[email protected] ~]# mysql -p aaa

Enter password: 

ERROR 1049 (42000): Unknown database ‘aaa‘

[[email protected] ~]# mysql -paaa

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 6

Server version: 10.0.13-MariaDB-log Source distribution


Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.


Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


MariaDB [(none)]> 


[[email protected] ~]# vim /etc/rc.d/init.d/mysqld

case "$mode" in

  ‘start‘)

    # Start daemon


    # Safeguard (relative paths, core dumps..)

    cd $basedir


    echo $echo_n "Starting MySQL"

    if test -x $bindir/mysqld_safe

    then

      # Give extra arguments to mysqld with the my.cnf file. This script

      # may be overwritten at next upgrade.      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

      wait_for_ready; return_value=$?


[[email protected] ~]# service mysqld restart

Shutting down MySQL..                                      [确定]

Starting MySQL..                                           [确定]


----------------------------------------------------------------------------------

授权:GRANT

GRANT priv_type[,...] ON [{table|function|procedure}] db.{table|routine} TO ‘USERNAME‘@‘HOST‘ [IDENTIFIED BY ‘password‘]

[REQUIRE SSL] [WITH with_option]


with_option:

   GRANT OPTION

 | MAX_QUERIES_PER_HOUR count

 | MAX_UPDATES_PER_HOUR count

 | MAX_CONNECTIONS_PER_HOUR count

 | MAX_USER_CONNECTIONS count


取消授权:REVOKE

REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level FROM user [, user] ...



------------------------------------------------------------------------------

查询缓存:

快速查询,跳过解析,分析。

注意:更新多缓存没意义。

   查询语句任何字符上的不同,都会导致缓存不能命中;


哪此查询可能不会被缓存?

查询中包含UDF、存储函数、用户自定义变量、临时表、mysql库中系统表、或者包含列级权限的表、有着不确定值的函数(Now()); 


查询缓存相关的服务器变量:

注意:修改以下参数,全部缓存重新开始。

MariaDB [(none)]> show global variables like ‘query%‘;

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

| Variable_name                | Value    |

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

| query_alloc_block_size       | 8192     |

| query_cache_limit            | 1048576  |

| query_cache_min_res_unit     | 4096     |

| query_cache_size             | 16777216 |

| query_cache_strip_comments   | OFF      |

| query_cache_type             | ON       |

| query_cache_wlock_invalidate | OFF      |

| query_prealloc_size          | 8192     |

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

8 rows in set (0.00 sec)


query_cache_min_res_unit: 查询缓存中内存块的最小分配单位;

较小值会减少浪费,但会导致更频繁的内存分配操作;

较大值会带来浪费,会导致碎片过多;

            查询的平均大小:查询做大值+查询最小值/查询次数

query_cache_limit:能够缓存的最大查询结果;

对于有着较大结果的查询语句,建议在SELECT中使用SQL_NO_CACHE

query_cache_size:查询缓存总共可用的内存空间;单位是字节,必须是1024的整数倍;

query_cache_type: ON, OFF, DEMAND(在SELECT中使用SQL_CACHE)

query_cache_wlock_invalidate:如果某表被其它的连接锁定,是否仍然可以从查询缓存中返回结果;默认值为OFF,表示可以在表被其它连接淘宝的场景中继续从缓存返回数据;ON则表示不允许;


技术分享


查询相关的状态变量

SHOW GLOBAL STATUS LIKE ‘Qcache%‘;

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

| Variable_name           | Value    |

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

| Qcache_free_blocks  空闲内存块    | 1        |

| Qcache_free_memory      | 16759688 |

| Qcache_hits             | 0        |

| Qcache_inserts   可缓存语句的次数  | 0        |

| Qcache_lowmem_prunes  查询缓存内存太小,清理缓存次数 | 0        |

| Qcache_not_cached    可缓存没有缓存   | 0        |

| Qcache_queries_in_cache 缓存空间缓存下来的个数| 0        |

| Qcache_total_blocks     | 1        |

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


缓存命中率的评估:Qcache_hits/(Qcache_hits+Com_select)


以上是关于mysql的主要内容,如果未能解决你的问题,请参考以下文章

MySQLMySQL 一些 使用 案例

MySQL的常用操作更改root密码连接MySQLMySQL常用的命令

MySQLMysql必会语句

MySQLMysql必会语句

MySQLMysql必会语句

mysqlmysql优化