MySQL經常使用操做——mysqladmin、鏈接mysql、grant、MyISAM、InnoDB

MySQL經常使用操做

1、設置更改root密碼

開啓mysql服務mysql

[root@ying01 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@ying01 ~]# ps aux|grep mysql
root      1645  0.0  0.0 113308  1600 pts/0    S    08:39   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ying01.pid
mysql     1783 13.5 24.1 1300828 453336 pts/0  Sl   08:39   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/ying01.err --pid-file=/data/mysql/ying01.pid --socket=/tmp/mysql.sock
root      1807  0.0  0.0 112720   980 pts/0    S+   08:39   0:00 grep --color=auto mysql

啓動mysql命令,須要用絕對路徑,或者配置環境;(mysql -uroot 使用root身份啓動)linux

[root@ying01 ~]# mysql -uroot                            //直接啓動,找不到mysql命令
-bash: mysql: 未找到命令
[root@ying01 ~]# ls /usr/local/mysql/bin/mysql           //查看命令路徑
/usr/local/mysql/bin/mysql
[root@ying01 ~]# /usr/local/mysql/bin/mysql -uroot       //用絕對路徑啓動,成功啓動
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit                                              //quit退出
Bye

把mysql命令的目錄,添加到環境變量中,此時能夠直接用:mysql命令sql

[root@ying01 ~]# echo $PATH                                   //如下爲系統默認的環境變量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/tmp/:/root/bin
[root@ying01 ~]# 
[root@ying01 ~]# export PATH=$PATH:/usr/local/mysql/bin/      //把mysqld的目錄添加到環境變量
[root@ying01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/tmp/:/root/bin:/usr/local/mysql/bin/
[root@ying01 ~]# mysql -uroot                                //此時直接使用
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit                                      
Bye

把mysql命令的目錄,添加到環境變量中;只是臨時有效,要永久生效,須要配置文件/etc/profileshell

[root@ying01 ~]# vim /etc/profile

export PATH=$PATH:/usr/local/mysql/bin/         //在配置文件中,添加此行語句


[root@ying01 ~]# source /etc/profile            //加載配置文件
[root@ying01 ~]# mysql -uroot -p                //此時就能夠直接使;-P 使用密碼
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye

設置密碼登陸;mysqladmin -uroot password 'www123'數據庫

[root@ying01 ~]# mysqladmin -uroot password 'www123'       //設置密碼爲 www123
Warning: Using a password on the command line interface can be insecure.
[root@ying01 ~]# mysql -uroot -p           //此時使用mysql命令,須要輸入剛纔設置的密碼;
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye

修改密碼: mysqladmin -uroot -p 'www123' password 'www1234' www1234 是修改後的密碼;vim

[root@ying01 ~]# mysqladmin -uroot -p 'www123' password 'www1234'
Enter password: 
mysqladmin: Unknown command: 'www123'
[root@ying01 ~]# mysqladmin -uroot -p'www123' password 'www1234'
Warning: Using a password on the command line interface can be insecure.
[root@ying01 ~]# mysql -uroot -p'www1234'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye

在密碼丟失的狀況下,怎麼進入?bash

第一步:先mysql的主配置文件my.cnf, 新增下面語句服務器

[root@ying01 ~]# vim /etc/my.cnf

[mysqld]
skip-grant                       //新增語句
datadir=/data/mysql
socket=/tmp/mysql.sock

第二步:重啓mysqld服務,就直接能夠以root用戶身份進去:mysql -uroot架構

[root@ying01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@ying01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use mysql;

第三步:使用庫的命令:use mysqlless

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>

查找用戶的密碼所在表

mysql> select password from user;
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *65753B9F60E54A740174E63A9E5E5F9774637276 |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+
6 rows in set (0.00 sec)

第四步:修改密碼: update user set password=password('yinglinux') where user='root';

mysql> update user set password=password('yinglinux') where user='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> quit
Bye

第五步:退出mysql庫後,再次編輯先mysql的主配置文件my.cnf;把以前添加的那一行取消;

[root@ying01 ~]# vim /etc/my.cnf


[mysqld]
#skip-grant               //把這行取消
datadir=/data/mysql
socket=/tmp/mysql.sock

第六步:重啓mysql服務,輸入新的密碼,就完成密碼重置

[root@ying01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@ying01 ~]# mysql -uroot -pyinglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

2、鏈接MySQL

鏈接mysql的幾種方法:

  • 直接鏈接:mysql -uroot -pyinglinux
[root@ying01 ~]# mysql -uroot -pyinglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye
  • 指定IP,端口鏈接: mysql -uroot -pyinglinux -h127.0.0.1 -P3306
[root@ying01 ~]# mysql -uroot -pyinglinux -h127.0.0.1 -P3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye
  • 經過sock鏈接:mysql -uroot -pyinglinux -S/tmp/mysql.sock
[root@ying01 ~]# ps aux |grep mysql  //先查看sock
root      4813  0.0  0.0 113308  1632 pts/0    S    15:55   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ying01.pid
mysql     4951  0.3 24.3 1366628 455836 pts/0  Sl   15:55   0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/ying01.err --pid-file=/data/mysql/ying01.pid --socket=/tmp/mysql.sock
root      5039  0.0  0.0 112720   980 pts/0    S+   16:01   0:00 grep --color=auto mysql
[root@ying01 ~]# mysql -uroot -pyinglinux -S/tmp/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye
  • shell腳本里面使用:mysql -uroot -pyinglinux -e "show databases"
[root@ying01 ~]# mysql -uroot -pyinglinux -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@ying01 ~]#

3、 MySQL經常使用命令

  • 顯示當前庫;
mysql> show databases;   
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
  • 切換到某個數據庫
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
  • 顯示當前庫的全部的數據表
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql>
  • 查看字段
mysql> desc db;
+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host                  | char(60)      | NO   | PRI |         |       |
| Db                    | char(64)      | NO   | PRI |         |       |
| User                  | char(16)      | NO   | PRI |         |       |
| Select_priv           | enum('N','Y') | NO   |     | N       |       |
| Insert_priv           | enum('N','Y') | NO   |     | N       |       |
| Update_priv           | enum('N','Y') | NO   |     | N       |       |
| Delete_priv           | enum('N','Y') | NO   |     | N       |       |
| Create_priv           | enum('N','Y') | NO   |     | N       |       |
| Drop_priv             | enum('N','Y') | NO   |     | N       |       |
| Grant_priv            | enum('N','Y') | NO   |     | N       |       |
| References_priv       | enum('N','Y') | NO   |     | N       |       |
| Index_priv            | enum('N','Y') | NO   |     | N       |       |
| Alter_priv            | enum('N','Y') | NO   |     | N       |       |
| Create_tmp_table_priv | enum('N','Y') | NO   |     | N       |       |
| Lock_tables_priv      | enum('N','Y') | NO   |     | N       |       |
| Create_view_priv      | enum('N','Y') | NO   |     | N       |       |
| Show_view_priv        | enum('N','Y') | NO   |     | N       |       |
| Create_routine_priv   | enum('N','Y') | NO   |     | N       |       |
| Alter_routine_priv    | enum('N','Y') | NO   |     | N       |       |
| Execute_priv          | enum('N','Y') | NO   |     | N       |       |
| Event_priv            | enum('N','Y') | NO   |     | N       |       |
| Trigger_priv          | enum('N','Y') | NO   |     | N       |       |
+-----------------------+---------------+------+-----+---------+-------+
22 rows in set (0.00 sec)
  • 查看建表語句並列出,命令後加\G
mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
  `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
  `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  ......
  `x509_subject` blob NOT NULL,
  `max_questions` int(11) unsigned NOT NULL DEFAULT '0',
  `max_updates` int(11) unsigned NOT NULL DEFAULT '0',
  `max_connections` int(11) unsigned NOT NULL DEFAULT '0',
  `max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
  `plugin` char(64) COLLATE utf8_bin DEFAULT 'mysql_native_password',
  `authentication_string` text COLLATE utf8_bin,
  `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.00 sec)

ERROR: 
No query specified
  • 查看當前是哪一個用戶
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> quit 
Bye
  • 查看在mysql下,使用的命令歷史
[root@ying01 ~]# less .mysql_history


_HiStOrY_V2_
use\040mysql;
select\040*\040from\040user;
show\040databases
show\040databases;
use\040mysql;
show\040tables;
desc\040db;
show\040create\040table\040user\134G;
select\040user();
.mysql_history (END)
  • 查看當前所使用的數據庫
mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> use mysql;       //切換到mysql庫下
Database changed 
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)
  • 建立一個新庫
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> use db1;                    //切換到表明db1庫
Database changed
  • 建立一個新表
mysql> create table t1(`id` int(4), `name` char(40)); //建立一個新建表
Query OK, 0 rows affected (0.06 sec)
mysql> show create table t1\G;                        //顯示新建立的表
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1              //注意和下面對比
1 row in set (0.00 sec)

ERROR: 
No query specified
  • 刪除一個表;
mysql> drop table t1;                   //清空此表
Query OK, 0 rows affected (0.01 sec)
  • 在建表的同時,還能夠指定 ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
Query OK, 0 rows affected (0.27 sec)

mysql> show create table t1\G;                                                
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8        //注意此處CHARSET值的變化,與上面
1 row in set (0.01 sec)

ERROR: 
No query specified
  • 查看數據庫的版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.36    |
+-----------+
1 row in set (0.00 sec)
  • 查看MySQL的當前狀態
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 2           |
| Binlog_cache_disk_use                         | 0           |
| Binlog_cache_use                              | 0           |
| Binlog_stmt_cache_disk_use                    | 0           |
| Binlog_stmt_cache_use                         | 0           |
| Bytes_received                                | 1234        |
| Bytes_sent                                    | 21089       |
| Com_admin_commands                            | 0           |
  • 查看MySQL的參數

其中不少參數都是能夠在/etc/my.cnf中定義,部分參數可在線編輯

mysql> show variables;
  • 模糊查找某參數
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql>  show variables like 'slow%';
+---------------------+-----------------------------+
| Variable_name       | Value                       |
+---------------------+-----------------------------+
| slow_launch_time    | 2                           |
| slow_query_log      | OFF                         |
| slow_query_log_file | /data/mysql/ying01-slow.log |
+---------------------+-----------------------------+
3 rows in set (0.00 sec)
  • 修改某項參數:set global max_connect_errors=1000;
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'max_connect_errors';  //查找修改後的參數,此時已經改變
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
+--------------------+-------+
1 row in set (0.00 sec)
  • 查看當前MySQL服務器隊列
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 10 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 10 | root | localhost | db1  | Query   |    0 | init  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+

mysql>

4、 MySQL用戶管理

4.1 建立用戶及登陸

建立一個普通用戶user1,並受權;

mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456a';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to 'user1'@'%' identified by '123456a'; //百分號%代替主機IP
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

以上語句釋義:

  • all表示全部的權限(如讀、寫、查詢、刪除等操做);建立user用戶並授予其全部權限.
  • 第一個星號:表示全部數據庫,第二個星號:表示全部表;好比:mysql.table
  • 這裏的user1特指localhost上的user1,用戶和主機的IP之間有一個@符號
  • identified by :設定密碼,用單引號括起來。

此時能夠使用user1用戶身份,並使用密碼:123456a登陸mysql,可是不可以登陸;

[root@ying01 ~]# mysql -uuser1 -p123456a
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)

只能加上參數-h:mysql -uuser1 -p123456a -h127.0.0.1

[root@ying01 ~]# mysql -uuser1 -p123456a -h127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye

那麼不使用IP,登陸mysql,那就須要把IP,改成localhost;

進入 root用戶下, 把IP 改成 localhost

[root@ying01 ~]# mysql -uroot -pyinglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> grant all on *.* to 'user1'@'localhost' identified by '123456a';  //把IP改成localhost
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

此時user1用戶,不須要指定IP登陸;

[root@ying01 ~]# mysql -uuser1 -p123456a 
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> exit
Bye

4.2 用戶受權

進入root用戶下,查看當前用戶受權

[root@ying01 ~]# mysql -uroot -pyinglinux

mysql> show grants;           //顯示當前的用戶受權
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*FB4553CE81F8C9590097E0CDB81FC48EC19A6169' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

查看user1用戶受權

mysql> show grants for user1@'127.0.0.1';        //顯示user1用戶受權
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@127.0.0.1                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

在建立一個用戶user2,並查看其受權;

mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.112.1' identified ed by 'passwd';
Query OK, 0 rows affected (0.00 sec)


mysql> show grants for user2@'192.168.112.1';
+------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.112.1                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.112.1' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.112.1'                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

給不一樣IP作同一個用戶的受權,同一密碼

mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.112.2' IDENTIFIED BY PASSWORD  '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0';  //此處的密碼位上面IP112.1的密碼
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for user2@'192.168.112.2';  //查看此IP受權狀況
+------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.112.2                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.112.2' IDENTIFIED BY PASSWORD '*FB4553CE81F8C9590097E0CDB81FC48EC19A6169' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.112.2'                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql>

5、經常使用sql語句

  • 查詢mysql庫中user表的行數:select count(*) from mysql.user
mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       11 |
+----------+
1 row in set (0.00 sec)
  • 查詢mysql庫中db表的全部內容 :select * from mysql.db\G
mysql> select * from mysql.db\G;
*************************** 1. row ***************************
                 Host: %
                   Db: test
                 User: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
*************************** 2. row ***************************
                 Host: %
                   Db: test\_%
                 User: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
*************************** 3. row ***************************
                 Host: 192.168.112.1
                   Db: db1
                 User: user2
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
*************************** 4. row ***************************
                 Host: 192.168.112.2
                   Db: db1
                 User: user2
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
4 rows in set (0.00 sec)

ERROR: 
No query specified

select語句在數據庫和表中對應的引擎不同,其計算統計時間也不一樣,不建議多使用

MyISAM 引擎

mysql> use mysql 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
 
 .....省略
 
  `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.00 sec)

ERROR: 
No query specified

InnoDB 引擎

mysql> use db1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `id` int(4) DEFAULT NULL,
  `name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

  • 查詢單個字段或者多個字段
mysql> select db from mysql.db;  //查找db庫
+---------+
| db      |
+---------+
| test    |
| test\_% |
| db1     |
| db1     |
+---------+
4 rows in set (0.01 sec)

mysql> select db,user from mysql.db;  //查找db庫和user項
+---------+-------+
| db      | user  |
+---------+-------+
| test    |       |
| test\_% |       |
| db1     | user2 |
| db1     | user2 |
+---------+-------+
4 rows in set (0.00 sec)
  • 使用萬能匹配符%,和like進行模糊匹配查詢
mysql> select * from mysql.db where host like '192.168.%'\G;  //IP模糊處理
*************************** 1. row ***************************
                 Host: 192.168.112.1
                   Db: db1
                 User: user2
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
*************************** 2. row ***************************
                 Host: 192.168.112.2
                   Db: db1
                 User: user2
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
2 rows in set (0.00 sec)

ERROR: 
No query specified
  • MySQL中插入數據
mysql> desc db1.t1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from db1.t1;       //db1.t1的內容爲空
Empty set (0.00 sec)

mysql> insert into db1.t1 values (1,'abc');   //在db1庫,t1表中插入:id爲1,name爲abc的值
Query OK, 1 row affected (0.01 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)

mysql> insert into db1.t1 values (1,123);
Query OK, 1 row affected (0.00 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
|    1 | 123  |
+------+------+
2 rows in set (0.00 sec)

更改表的某一行: update db1.t1 set name='aaa' where id=1;

mysql> update db1.t1 set name='aaa' where id=1;   //在db1庫,t1表中,把全部id=1,其name設置爲aaa
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from db1.t1;      //查看db1庫,t1表內容
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    1 | aaa  |
+------+------+
2 rows in set (0.00 sec)

mysql> update db1.t1 set id=5 where name='aaa';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    5 | aaa  |
|    5 | aaa  |
+------+------+
2 rows in set (0.00 sec)
  • 刪除數據

用delete刪除表內某些數據;

mysql> delete from db1.t1 where id=5;      //在db1庫,t1表中,刪除id=5的數據
Query OK, 2 rows affected (0.00 sec)

mysql> select * from db1.t1;               //查看錶,此時表清空
Empty set (0.00 sec)

mysql> insert into db1.t1 values (1,123);  //在設置一個新表
Query OK, 1 row affected (0.00 sec)

mysql> select * from db1.t1;               //裏面已經寫了數據
+------+------+
| id   | name |
+------+------+
|    1 | 123  |
+------+------+
1 row in set (0.00 sec)

用truncate,直接清空表內數據;

mysql> truncate db1.t1;
Query OK, 0 rows affected (0.05 sec)

mysql> select * from db1.t1;         //清空數據
Empty set (0.00 sec)

mysql> desc db1.t1;                   //查看錶架構
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
  • drop命令,直接刪除表以及庫的架構,此命令慎用;
mysql> drop table t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.t1;                        //此時已經報錯,表已經不存在
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
mysql> desc db1.t1;
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
mysql> drop database db1;                          //刪除db1
Query OK, 0 rows affected (0.00 sec)

mysql>

6、MySQL數據庫備份恢復

6.1 備份庫和恢復庫

備份數據庫,並把其放到指定目錄

[root@ying01 ~]# mysqldump -uroot -pyinglinux mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
mysqldump: Got error: 1146: Table 'mysql.slave_worker_info' doesn't exist when using LOCK TABLES

此時出現錯誤:mysqldump: Got error: 1146: Table 'mysql.slave_worker_info' doesn't exist when using LOCK TABLES

slave_worker_info 不存在;

那麼 先找到 slave_relay_log_info 相關文件;

在 ls /data/mysql/mysql/目錄下查看;

發現兩個 slave_relay_log_info相關文件,把slave_relay_log_info.frm 刪除或者移走;

[root@ying01 ~]# mv /data/mysql/mysql/slave_worker_info.frm /root/NBA/
[root@ying01 ~]# ls /data/mysql/mysql/slave_worker_info.frm 
ls: 沒法訪問/data/mysql/mysql/slave_worker_info.frm: 沒有那個文件或目錄

再備份一次;

[root@ying01 ~]# mysqldump -uroot -pyinglinux mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@ying01 ~]#

注意:問題已經解決,可是緣由,還須要解析,分析;先暫時,放下;

建立表mysql2數據庫

[root@ying01 ~]# mysql -uroot -pyinglinux -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.

把mysqlbak.sql數據反定向於mysql2

[root@ying01 ~]# mysql -uroot -pyinglinux mysql2 < /tmp/mysqlbak.sql  //注意不是mysqldump
Warning: Using a password on the command line interface can be insecure.

進入mysql2數據庫下

[root@ying01 ~]# mysql -uroot -pyinglinux mysql2
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> select database();         //查看當前數據庫
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)

mysql> show tables;              //查看數據庫下數據表
+---------------------------+
| Tables_in_mysql2          |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
27 rows in set (0.00 sec)

更換到數據庫mysql下,發現數據表和mysql2下的表同樣;

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
27 rows in set (0.00 sec)

mysql> quit
Bye

6.2 備份表和恢復表

[root@ying01 ~]# mysqldump -uroot -pyinglinux mysql user > /tmp/user.sql   //先備份,用mysqldump
Warning: Using a password on the command line interface can be insecure.
[root@ying01 ~]# mysql -uroot -pyinglinux mysql2 < /tmp/user.sql           //再取出,注意不是mysqldump
Warning: Using a password on the command line interface can be insecure.

6.3 備份全部庫

[root@ying01 ~]# mysqldump -uroot -pyinglinux -A > /tmp/mysql_all.sql    //-A,表示所有備份
Warning: Using a password on the command line interface can be insecure.
[root@ying01 ~]# less /tmp/mysql_all.sql

6.4 備份表結構

[root@ying01 ~]# mysqldump -uroot -pyinglinux -d mysql2 > /tmp/mysql2.sql   //使用-d選項
Warning: Using a password on the command line interface can be insecure.
[root@ying01 ~]# less /tmp/mysql2.sql
相關文章
相關標籤/搜索