13.1 设置更改root密码
默认 mysql 密码为空,但不安全,因此需要设置密码
启动 mysql 服务
[root@arslinux-01 ~]# ps aux|grep mysqld [root@arslinux-01 ~]# /etc/init.d/mysqld start
将 mysql 命令路径加入PATH(定义全局)
[root@arslinux-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/ [root@arslinux-01 ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin/
将定义的 PATH 加入 /etc/profile,永久生效
[root@arslinux-01 ~]# vim /etc/profile export PATH=$PATH:/usr/local/mysql/bin/ [root@arslinux-01 ~]# source /etc/profile
如果只是更改了profile文件,并没有执行export PATH,那么source /etc/profile可使环境变量生效
mysql 命令,空密码
mysql -u用户名 -p密码 进入mysql
[root@arslinux-01 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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 密码
mysqladmin -u用户名 password '密码'
[root@arslinux-01 ~]# mysqladmin -uroot password 'arslinux' Warning: Using a password on the command line interface can be insecure. [root@arslinux-01 ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
更改 mysql 密码
mysqladmin -u用户名 -p'原密码' password '新密码'
[root@arslinux-01 ~]# mysqladmin -uroot -p'123456' password '1234562' Warning: Using a password on the command line interface can be insecure. [root@arslinux-01 ~]# mysql -uroot -p'123456' Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) [root@arslinux-01 ~]# mysql -uroot -p'1234562' 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 7 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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
不知道 root 密码情况下,更改 mysql 密码
1、编辑 /etc/my.cnf,增加一行 skip-grant,忽略授权,跳过用户名密码
[root@arslinux-01 ~]# vim /etc/my.cnf [mysqld] skip-grant
[root@arslinux-01 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!
[root@arslinux-01 ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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
2、更改 mysql 库里的 user 表
[root@arslinux-01 ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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; 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> update user set password=password('arslinux') where user='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> quit Bye
3、删除 /etc/my.cnf 中的 skip-grant
4、重启 mysql,尝试登录 mysql
[root@arslinux-01 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.m SUCCESS!
[root@arslinux-01 ~]# mysql -uroot -p'arslinux' 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.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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
13.2 连接mysql
连接本机 mysql
[root@arslinux-01 ~]# mysql -uroot -p'123456'
连接远程机器 mysql
[root@arslinux-01 ~]# mysql -uroot -p'123456' -h127.0.0.1 -P3306
使用 socket 连接 mysql(只适合在本机)
[root@arslinux-01 ~]# mysql -uroot -p'123456' -S/tmp/mysql.sock
连接之后继续进行操作(常用于 shell 脚本)
[root@arslinux-01 ~]# mysql -uroot -p'123456' -e "show databases" Warning: Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
13.3 mysql常用命令
1、show databases; 查询数据库
[root@arslinux-01 ~]# mysql -uroot -parslinux 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 5 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
2、use mysql; 切换库
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
3、show tables; 查看库里的表
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)
4、desc tb_name; 查看表里的字段
mysql> desc user; +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Host | char(60) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Password | char(41) | NO | | | | | 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 | | | Reload_priv | enum('N','Y') | NO | | N | | | Shutdown_priv | enum('N','Y') | NO | | N | | | Process_priv | enum('N','Y') | NO | | N | | | File_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 | | | Show_db_priv | enum('N','Y') | NO | | N | | | Super_priv | enum('N','Y') | NO | | N | | | Create_tmp_table_priv | enum('N','Y') | NO | | N | | | Lock_tables_priv | enum('N','Y') | NO | | N | | | Execute_priv | enum('N','Y') | NO | | N | | | Repl_slave_priv | enum('N','Y') | NO | | N | | | Repl_client_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 | | | Create_user_priv | enum('N','Y') | NO | | N | | | Event_priv | enum('N','Y') | NO | | N | | | Trigger_priv | enum('N','Y') | NO | | N | | | Create_tablespace_priv | enum('N','Y') | NO | | N | | | ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | | | ssl_cipher | blob | NO | | NULL | | | x509_issuer | blob | NO | | NULL | | | x509_subject | blob | NO | | NULL | | | max_questions | int(11) unsigned | NO | | 0 | | | max_updates | int(11) unsigned | NO | | 0 | | | max_connections | int(11) unsigned | NO | | 0 | | | max_user_connections | int(11) unsigned | NO | | 0 | | | plugin | char(64) | YES | | mysql_native_password | | | authentication_string | text | YES | | NULL | | | password_expired | enum('N','Y') | NO | | N | | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ 43 rows in set (0.01 sec)
5、show create table tb_name\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', `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '', `ssl_cipher` blob NOT NULL, `x509_issuer` blob NOT NULL, `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
6、select user(); 查看当前用户
mysql> select user(); +--------+ | user() | +--------+ | root@ | +--------+ 1 row in set (0.00 sec)
7、select database(); 查看当前使用的数据库
mysql> select database(); +------------+ | database() | +------------+ | mysql | +------------+ 1 row in set (0.00 sec)
8、create database 库名; 创建库
mysql> create database db1; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
6、create table 表名 (字段); 创建表
mysql> create table tb1(`id` int(4),`name` char(40)); Query OK, 0 rows affected (0.02 sec) mysql> desc tb1; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | char(40) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> show create table tb1\G; *************************** 1. row *************************** Table: tb1 Create Table: CREATE TABLE `tb1` ( `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
7、创建表同时自定义参数设置
mysql> create table tb1(`id` int(4),`name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.03 sec) mysql> show create table tb1\G; *************************** 1. row *************************** Table: tb1 Create Table: CREATE TABLE `tb1` ( `id` int(4) DEFAULT NULL, `name` char(40) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified
8、drop table tb1; 删除表
drop database user; 删除库
mysql> drop table tb1; Query OK, 0 rows affected (0.00 sec) mysql> drop database db1; Query OK, 0 rows affected (0.00 sec)
9、select version(); 查看当前数据库版本
mysql> select version(); +-----------+ | version() | +-----------+ | 5.6.43 | +-----------+ 1 row in set (0.00 sec)
10、show status;查看数据库状态
mysql> show status; +-----------------------------------------------+-------------+ | Variable_name | Value | +-----------------------------------------------+-------------+ | Aborted_clients | 0 | | Aborted_connects | 0 | | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 0 | | Binlog_stmt_cache_disk_use | 0 | | Table_open_cache_hits | 8 | | Table_open_cache_misses | 29 | | Threads_cached | 0 | | Threads_connected | 1 | | Threads_created | 1 | | Threads_running | 1 | | Uptime | 2892 | | Uptime_since_flush_status | 2892 | +-----------------------------------------------+-------------+ 341 rows in set (0.01 sec)
(内容太多,仅展示部分)
11、show variable; 查看所有参数
show variables like 'max_connect%'; 查看指定参数 (%通配)
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/arslinux-01-slow.log | +---------------------+----------------------------------+ 3 rows in set (0.00 sec)
12、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%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 1000 | | max_connections | 151 | +--------------------+-------+ 2 rows in set (0.00 sec)
如果想要永久生效,需要退出到 shell ,vim /etc/my.cnf,定义 max_connect_errors=1000;
13、show processlist;show full processlist; 查看队列(mysql具体在做什么)
mysql> show processlist; +----+------+-----------+-------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+-------+---------+------+-------+------------------+ | 5 | root | localhost | mysql | Query | 0 | init | show processlist | +----+------+-----------+-------+---------+------+-------+------------------+ 1 row in set (0.00 sec) mysql> show full processlist; +----+------+-----------+-------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+-------+---------+------+-------+-----------------------+ | 5 | root | localhost | mysql | Query | 0 | init | show full processlist | +----+------+-----------+-------+---------+------+-------+-----------------------+ 1 row in set (0.00 sec)
show full processlist 显示的最后一列更加完整
·mysql 的命令历史存放在 /root/.mysql_history
13.4 mysql用户管理
·创建用户:grant all on *.* to 'user1' identified by 'passwd';
或指定来源ip:grant all on *.* to 'user1'@'ip' identified by 'passwd';
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
针对 mysql 数据库所有表有权限
grant all on mysql.* to 'user1'@'127.0.0.1' identified by '123456';
针对所有 ip 授权
grant all on *.* to 'user2'@'%' identified by '123456';
针对 socket 授权
grant all on *.* to 'user1'@'localhost' identified by '123456';
mysql> grant all on *.* to 'user1'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@arslinux-01 ~]# mysql -uuser1 -p123456 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.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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>
针对 socket 授权了之后,进入 mysql 不会再提示 Access denied
针对具体权限去授权
grant SELECT,UPDATE,INSERT on db1.* to 'user3'@'192.168.194.1' identified by 'passwd';
针对所有 ip 授权部分库的权限
grant all on db1.* to 'user3'@'%' identified by 'passwd';
查看所有授权 show grants;
mysql> show grants; +-----------------------------------------------------------------------------------------------------------------------+ | Grants for user1@localhost | +-----------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | +-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
查看指定用户授权:show grants for 用户名@ip;
mysql> show grants for user3@192.168.194.1; +------------------------------------------------------------------------------------------------------------------+ | Grants for user3@192.168.194.1 | +------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'user3'@'192.168.194.1' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' | | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user3'@'192.168.194.1' | +------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
针对同一个用户在不通的 ip 上授权:(下框两个GRANT 改ip后再执行一遍)
mysql> GRANT USAGE ON *.* TO 'user3'@'192.168.194.1' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user3'@'192.168.194.2'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for user3@192.168.194.2; +--------------------------------------------------------------------+ | Grants for user3@192.168.194.2 | +--------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'user3'@'192.168.194.2' | | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user3'@'192.168.194.2' | +--------------------------------------------------------------------+ 2 rows in set (0.00 sec)
show grants 常用于复制用户,密码不知道的情况下复制用户
13.5 常用sql语句
查看某个数据库的某个表有多少行
mysql> select count(*) from mysql.user; +----------+ | count(*) | +----------+ | 10 | +----------+ 1 row in set (0.00 sec)
查看某个表的所有内容
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.194.1 Db: db1 User: user3 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.194.2 Db: db1 User: user3 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 count(*) 和 select * 这种操作尽量避免,数据太大的表,比较耗费时间),myisam 引擎快,innodb 引擎慢
显示字段,一个或多个
mysql> select db from mysql.db; +---------+ | db | +---------+ | test | | test\_% | | db1 | | db1 | +---------+ 4 rows in set (0.00 sec) mysql> select db,user from mysql.db; +---------+-------+ | db | user | +---------+-------+ | test | | | test\_% | | | db1 | user3 | | db1 | user3 | +---------+-------+ 4 rows in set (0.00 sec)
模糊查询
mysql> select * from mysql.db where host like '192.168.%'\G; *************************** 1. row *************************** Host: 192.168.194.1 Db: db1 User: user3 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.194.2 Db: db1 User: user3 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.01 sec) ERROR: No query specified
插入数据
mysql> insert into db1.tb1 values (1,'abc'); Query OK, 1 row affected (0.00 sec) mysql> select * from db1.tb1; +------+------+ | id | name | +------+------+ | 1 | abc | +------+------+ 1 row in set (0.00 sec)
更改表里的数据信息
update 表名 set name=名称 where id = 数值; 改名称
update 表名 set id = 数值 where name=名称; 改id
mysql> select * from db1.tb1; +------+------+ | id | name | +------+------+ | 1 | aaa | | 2 | xyz | | 3 | uvw | +------+------+ 3 rows in set (0.00 sec) mysql> update db1.tb1 set name='aaa' where id=2; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from db1.tb1; +------+------+ | id | name | +------+------+ | 1 | aaa | | 2 | aaa | | 3 | uvw | +------+------+ 3 rows in set (0.00 sec) mysql> update db1.tb1 set id=4 where name='uvw'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from db1.tb1; +------+------+ | id | name | +------+------+ | 1 | aaa | | 2 | aaa | | 4 | uvw | +------+------+ 3 rows in set (0.00 sec)
根据条件删除表
mysql> delete from db1.tb1 where id=2; Query OK, 1 row affected (0.00 sec) mysql> select * from db1.tb1; +------+------+ | id | name | +------+------+ | 1 | aaa | | 4 | uvw | +------+------+ 2 rows in set (0.00 sec)
清空表的所有数据(不用逐条删除),保留表
mysql> truncate table db1.tb1; Query OK, 0 rows affected (0.02 sec) mysql> select * from db1.tb1; Empty set (0.00 sec)
删除表(数据加壳全删除)
mysql> drop table db1.tb1; Query OK, 0 rows affected (0.01 sec) mysql> select * from db1.tb1; ERROR 1146 (42S02): Table 'db1.tb1' doesn't exist
删除数据库
mysql> drop database db1; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
13.6 mysql数据库备份恢复
mysqldump -u用户名 -p密码 数据库 > 备份数据文件路径 备份库
[root@arslinux-01 ~]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql Warning: Using a password on the command line interface can be insecure.
mysql -u用户名 -p密码 数据库 < 备份数据文件路径 恢复库
[root@arslinux-01 ~]# mysql -uroot -p123456 -e "create database mysql2" Warning: Using a password on the command line interface can be insecure.
[root@arslinux-01 ~]# mysql -uroot -parslinux mysql2 < /tmp/mysql.sql Warning: Using a password on the command line interface can be insecure.
[root@arslinux-01 ~]# mysql -uroot -p123456 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 13 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysql2 | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> use mysql2; 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_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 | | slave_worker_info | | slow_log | | tables_priv | | tb1 | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 29 rows in set (0.00 sec)
mysqldump -u用户名 -p密码 数据库 表名 > 备份数据路径 备份表
[root@arslinux-01 ~]# mysqldump -uroot -p123456 mysql user > /tmp/user.sql Warning: Using a password on the command line interface can be insecure.
mysqldump -u用户名 -p密码 数据库 < 备份数据路径 恢复表
[root@arslinux-01 ~]# mysql -uroot -p123456 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.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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; 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> drop table user; Query OK, 0 rows affected (0.00 sec) 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 | | tb1 | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | +---------------------------+ 28 rows in set (0.00 sec) mysql> quit Bye
[root@arslinux-01 ~]# mysql -uroot -p123456 mysql < /tmp/user.sql Warning: Using a password on the command line interface can be insecure.
[root@arslinux-01 ~]# mysql -uroot -p123456 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 18 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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; 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 | | tb1 | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 29 rows in set (0.00 sec)
mysqldump -u用户名 -p密码 -A > 备份数据路径 备份所有库
[root@arslinux-01 ~]# mysqldump -uroot -parslinux -A > /tmp/mysql_all.sql Warning: Using a password on the command line interface can be insecure.
mysqldump -u用户名 -p密码 -d 数据库 > 备份数据路径 备份表结构
[root@arslinux-01 ~]# mysqldump -uroot -parslinux -d mysql2 > /tmp/mysql2.sql Warning: Using a password on the command line interface can be insecure.
不需要数据,只要语句
备份库,先库后表,恢复只需要写库
使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份 http://zhangguangzhi.top/2017/08/23/innobackex%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql
课堂笔记
1、MySQL重要性
2、再谈MySQL、MariaDB趋势
https://www.cnblogs.com/ivictor/pp/9807284.html
mariadb的版本 https://downloads.mariadb.org
3、学习重心:安装、配置(各种参数调整,尤其是buffer cache,binlog,慢查询日志)、基本操作(包括备份、恢复)、主从、集群
masql 调优:http:/www.aminglinux.com/bbs/thread-5758-1-1.html
同学分享mysql调优经历:http://www.apelearn.com/bbs/thread-11281-1-1.html
面试可能会被问到的点:
什么是事务?事务的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094
myisam 和 innodb 引擎对比: http://www.pureweber.com/article/myisam-vs-innodb/
几篇和mysql主从有关的文章 关键词 GTID
https://blog.csdn.net/xiaoyi23000/aritcle/details/80521423
https://blog.csdn.net/u013399093/article/details/70568837
https://www.cnblogs.com/abobo/p/4242417.html
mysql参数调整
http://isky000.com/database/mysql-performance-tuning-cache-paramter
转载于:https://blog.51cto.com/11530642/2398254