MySQL 学习笔记

简介

MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统) 应用软件之一。

Server version: 5.7.17 MySQL Community Server (GPL)

连接

登录
mysql -u用户名 -p用户密码 (如果连接远端机器加上参数 -h 远端机器IP  -P 端口)
# 例子
mysql -h 127.0.0.1 -P 3306 -uroot -ppassword
登出
exit

用户权限管理

新建用户
mysql> create user 'blazehu'@'127.0.0.1' identified by 'readonly';
Query OK, 0 rows affected (0.01 sec)
查看用户
mysql> select Host, User from mysql.user;
+-----------+-----------+
| Host | User |
+-----------+-----------+
| % | root |
| 127.0.0.1 | blazehu |
| 127.0.0.1 | root |
| localhost | mysql.sys |
| localhost | root |
+-----------+-----------+
5 rows in set (0.00 sec)
删除用户
mysql> drop user 'blazehu'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
用户赋权

使用 grant 增加新用户设置用户权限等 (可以直接对 mysql.user 用户表操作,来修改用户密码等)

mysql> grant select, update, insert, delete on test.* to 'blazehu'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看用户权限
mysql> show grants for 'blazehu'@'127.0.0.1';
+---------------------------------------------------------------------------+
| Grants for blazehu@127.0.0.1 |
+---------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'blazehu'@'127.0.0.1' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'blazehu'@'127.0.0.1' |
+---------------------------------------------------------------------------+
2 rows in set (0.00 sec)
用户重命名
mysql> rename user 'blazehu'@'127.0.0.1' to 'blaze'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
修改密码
mysql> update mysql.user set authentication_string=password('123456') where user='blaze';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1

mysql> set password for 'blaze'@'127.0.0.1'=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

也可以使用 mysqladmin 命令来修改密码。

数据库基础操作

显示数据库列表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
创建数据库
mysql> create database test default character set utf8;;
Query OK, 1 row affected (0.00 sec)
选择数据库
mysql> use test
Database changed
删除数据库
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)

数据表基础操作

显示数据表
mysql> show tables;
+-----------------------------------+
| Tables_in_test |
+-----------------------------------+
| account_permission |
| account_userpermission |
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| authtoken_token |
| django_admin_log |
| django_cas_ng_proxygrantingticket |
| django_cas_ng_sessionticket |
| django_content_type |
| django_migrations |
| django_session |
+-----------------------------------+
15 rows in set (0.00 sec)
查看数据库表结构
mysql> desc auth_user;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| password | varchar(128) | NO | | NULL | |
| last_login | datetime(6) | YES | | NULL | |
| is_superuser | tinyint(1) | NO | | NULL | |
| username | varchar(150) | NO | UNI | NULL | |
| first_name | varchar(30) | NO | | NULL | |
| last_name | varchar(150) | NO | | NULL | |
| email | varchar(254) | NO | | NULL | |
| is_staff | tinyint(1) | NO | | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
| date_joined | datetime(6) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
删除数据表
mysql> drop table auth_user;
Query OK, 0 rows affected (0.02 sec)
查看建表语句
mysql> show create table auth_user \G;
*************************** 1. row ***************************
Table: auth_user
Create Table: CREATE TABLE `auth_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`password` varchar(128) NOT NULL,
`last_login` datetime(6) DEFAULT NULL,
`is_superuser` tinyint(1) NOT NULL,
`username` varchar(150) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(150) NOT NULL,
`email` varchar(254) NOT NULL,
`is_staff` tinyint(1) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`date_joined` datetime(6) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

ERROR:
No query specified
数据库表增删改查
插入: insert into tablename values("", "");
删除: delete from tablename where ...;
更新: update tablename set a="" where ...;
查找: select * from tablename where ...;
创建索引
mysql> alter table auth_user add key test__name(name) using btree;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

其他常用命令

查看数据库变量信息
mysql> show variables like "character%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
查看数据库大小
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from information_schema.TABLES where table_schema='test';
+--------+
| data |
+--------+
| 0.56MB |
+--------+
1 row in set (0.01 sec)
查看数据表大小
mysql> SELECT TABLE_NAME, round(DATA_LENGTH/1024/1024,2) as data ,TABLE_ROWS FROM information_schema.TABLES WHERE TABLE_SCHEMA='test' order by data desc limit 10;
+---------------------------+------+------------+
| TABLE_NAME | data | TABLE_ROWS |
+---------------------------+------+------------+
| django_session | 4.45 | 410 |
| cloud_hostmaintenanceuser | 1.52 | 7648 |
| storage_cephbucket | 1.52 | 1284 |
| redis_draft_service_log | 0.52 | 990 |
| kubemetrics_nodeinfos | 0.42 | 1235 |
| saltstack_saltgrainsitems | 0.30 | 1409 |
| auth_user_groups | 0.23 | 3055 |
| storage_glusterbrick | 0.22 | 1328 |
| saltstack_addonversion | 0.17 | 1283 |
| auth_user | 0.14 | 1171 |
+---------------------------+------+------------+
10 rows in set (0.01 sec)
修改默认分隔符

默认分隔符为 “;”

delimiter //
MySQL 主从,跳过一个错误
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;

通过脚本操作数据库

mysql -h host -P port -uUsername -pPassword -e "command"

备份和还原

备份数据库
mysqldump 命令
# 包括数据库中的数据
root@demo:/# mysqldump -uroot -ppassword test > test.sql

# 不包括数据库中的数据
root@demo:/# mysqldump -uroot -ppassword -d test > test.sql

# 数据库中的某张表(包含数据)
root@demo:/# mysqldump -uroot -ppassword test auth_user > test.sql

# 数据库中的某张表(不包含数据)
root@demo:/# mysqldump -uroot -ppassword -d test auth_user > test.sql
还原数据库
mysql 命令
root@demo:/# mysql -uroot -ppassword test < test.sql
source 命令
mysql> source test.sql;
Query OK, 1 row affected (0.00 sec)
备份数据表
mysql> create table auth_user_bak as select * from auth_user;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
热备工具

qpress 解压工具
percona-xtrabackup 热备工具

相关问题

在做数据库主从和备份的时候发现 dump 失败 ?

mysqldump 失败的原因可能是 my.cnf “#innodb_force_recovery=1” 参数没有注释掉,这个参数放开会导致数据库不能做 insert、update、dump 数据库等操作。

Ubuntu 12.04 安装 mysql-python 提示找不到 mysql_config ?

安装 libmysqlclient-dev 库: sudo apt-get install libmysqlclient-dev 。安装完成之后在 /etc/mysql 下会生成默认的 my.cnf 配置文件 ,系统优先读取该路径下的配置文件信息,这时候需要注意检查配置文件是否正确。

通过 shell watch mysql 的一些参数,发现 mysql 连接断开,报错 too many connection ?

watch 每隔2秒钟 连接一次 mysql 导致数据库连接过多,数据库自动断开连不上。

参考资料