目录
一. mysql连接
1.1 启动/连接问题
1.2 连接方式
1.3 远程连接(含创建用户+授权)
1.4 客户端连接工具
1.5 查看有哪些用户远程连接MySQL
二、初步了解MySQL
2.1 简单的sql语句
2.2 存储引擎
2.3 字符集
2.3.1 继承
2.4 DBMS
三、MySQL进程之间的关系
一. mysql连接
1.1 启动/连接问题
mysql服务启动不了,如何解决?
1.看mysql的配置文件,是否错误的配置
2.日志--》有用的信息--》找ERROR提醒
从windows里不能连接到linux里的mysql,如何解决?如果判断故障?
1.windows的ip设置问题--》windows的网络问题
ping linux服务器的ip地址 ,检查网络是否通畅
linux 服务器的网络问题
2.linux里的防火墙是否开启
iptables -L(查看防火墙规则)
service firewalld stop(关闭防火墙)
3.检查下mysql服务是否开启
ps aux|grep mysqld
4.检查下端口号是否修改
netstat -anlput|grep mysqld
5.连接的用户是否有授权
grant
6.云服务器的安全组(使用的是云服务器)
1.2 连接方式
1.文件socket
只能在本机
是实现一台电脑里的不同进程之间通信的文件
[root@wudang-mysql-2 mysql]# mysql -uroot -p'Sanchuang1234#' -S /data/mysql/mysql.sock
在/etc/my.cnf配置文件里打开如下配置,在使用mysql客户端命令连接的时候,就不需要接-S指定socket文件的路径了
/etc/my.cnf存放mysql的配置文件
[client]
socket=/data/mysql/mysql.sock
2.网络socket
是ip+port --》格式ip:port 192.168.0.163:3309
网络中通过ip地址找到对方 ,实现不同的电脑之间的不同的进程之间的通信的
[root@mysql-2 ~]# mysql -h 192.168.0.163 -P3309 -ucali -p'Sanchuang1234#'
-h 指定主机名(ip) host
-P port 端口 (大写)
1.3 远程连接(含创建用户+授权)
创建用户:
'fyy'@'%' 表示用户fyy可以从任何地方连接
identified by '123sc' 表示该用户的密码为123 sc
root@(none) 15:57 mysql>create user 'fyy'@'%' identified by '123sc';
Query OK, 0 rows affected (0.00 sec)
授权:
all 表示所有权限 -- 不包括给其他用户授权
on *.* 前一个*表示所有的库,后一个*表示所有的表
root@(none) 15:58 mysql>grant all on *.* to 'fyy'@'%';
Query OK, 0 rows affected (0.00 sec)
查看用户的具体信息(可看到权限)
\G 逐行显示
root@(none) 21:46 mysql>select * from mysql.user\G;
测试是否可以在其他主机连接:
[root@nginx-kafka01 ~]# mysql -h 192.168.182.129 -ufyy -p123sc
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
1.4 客户端连接工具
1. Windows
SQLyog、Navicat、mysql workbench(官方)
2. Linux
mysql
1.5 查看有哪些用户远程连接MySQL
show processlist
root@(none) 23:43 mysql>show processlist;
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+------------------+
| 2 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)
二、初步了解MySQL
MySQL的库本质上是一个目录,表本质上是文件
2.1 简单的sql语句
创建库:
root@(none) 16:14 mysql>create database sc;
Query OK, 1 row affected (0.00 sec)
root@(none) 16:15 mysql>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sc |
| sys |
+--------------------+
5 rows in set (0.00 sec)
查看创建数据库的sql语句:
> 可以看到该数据库指定的字符集(utf8)
root@sc 15:24 mysql>show create database sc;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| sc | CREATE DATABASE `sc` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)
查看:
>在mysql中也可以看到
root@sc 15:26 mysql>system ls /data/mysql;
auto.cnf ibdata1 mysqlsc
ca-key.pem ib_logfile0 mysql.sockserver-cert.pem
ca.pem ib_logfile1 mysql.sock.lockserver-key.pem
client-cert.pem ibtmp1 performance_schemasys
client-key.pem lihua private_key.pemting-mysql.err
ib_buffer_pool localhost.localdomain.err public_key.pemting-mysql.pid
创建表:
root@(none) 16:18 mysql>use sc;
Database changed
root@sc 16:19 mysql>show tables;
Empty set (0.00 sec)
root@sc 16:19 mysql>create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
root@sc 16:20 mysql>show tables;
+--------------+
| Tables_in_sc |
+--------------+
| t1 |
+--------------+
1 row in set (0.00 sec)
查看:
db.opt -- 存放库使用的字符集和校对规则
t1.frm -- 存放t1表的表结构(frame)
t1.idb -- 存放t1表的数据(使用innodb存储引擎)(innodb:一个软件,用来存取数据,在内存和磁盘之间)(innodb data)
[root@localhost mysql]# cd sc
[root@localhost sc]# ls
db.opt t1.frm t1.ibd
查看表结构:
# 第一种
root@sc 16:20 mysql>desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
# 第二种
root@sc 15:23 mysql>show create table t1;
+-------+-------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2.2 存储引擎
mysql体系结构图
存储引擎结构图
存储引擎和表是捆绑的
存储引擎是一个软件,用来把数据从内存写到磁盘或从磁盘读到内存
查看表所使用的存储引擎:
root@sc 16:38 mysql>show create table t1;
+-------+-------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
指定表的存储引擎:
root@sc 16:48 mysql>create table t2(id int,name varchar(20)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
root@sc 16:49 mysql>show create table t2;
+-------+-------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
t2.frm 存放t2的表结构
t2.MYD 存放数据,存储引擎为myisam
t2.MYI 存放索引(index)
[root@localhost sc]# ls
db.opt t1.frm t1.ibd t2.frm t2.MYD t2.MYI
查看MySQL含有的存储引擎:
root@sc 16:37 mysql>show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
四种常见的存储引擎:
1. innodb存储引擎的特点:支持事务,支持行级别的锁,支持外键
2. memory :适合出报表,不需要永久保存的数据,临时表,退出即删除
3. csv
4. myisam
2.3 字符集
字符集指的是某个范围字符集的编码规则
查询你的MySQL支持哪些字符集
# 很多公司使用utf8mb4
root@sc 15:27 mysql>show character set;
| Charset | Description | Default collation | Maxlen |
# yum安装默认是拉丁
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
utf8_general_ci 其中ci为case ignore,字符排序时不区分大小写
[root@ting-mysql lihua]# cat db.opt
default-character-set=utf8
default-collation=utf8_general_ci
# collation 校对规则
(排序时)
查看你正在使用哪些字符集
# show variables 查看mysql中的所有变量
# like 类似于过滤,%:通配符
root@(none) 14:27 mysql>show variables like '%CHARACTER%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)
查看系统使用的字符集
[root@ting-mysql ~]# locale
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
查看某个库正在使用的字符集
root@(none) 14:35 mysql>show create database lihua;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| lihua | CREATE DATABASE `lihua` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
2.3.1 继承
继承(默认字符集):服务器-库-表-列(库继承服务器的字符集,……)
【注:只有文本类型的数据,会牵扯到字符集(varchar、char、text)】
也可以指定字符集,指定的字符集优先级高于继承
# 指定库的字符集(命令使用的是简写)
root@(none) 14:35 mysql>create database xiaoxi default charset=utf8mb4;
Query OK, 1 row affected (0.00 sec)
root@(none) 14:42 mysql>use xiaoxi;
Database changed
root@xiaoxi 14:42 mysql>show tables;
Empty set (0.00 sec)
root@xiaoxi 14:42 mysql>create table t1(id int, name varchar(20));
Query OK, 0 rows affected (0.00 sec)
# 创建表不指定字符集,默认继承库
root@xiaoxi 14:42 mysql>show create table t1;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
# 指定字符集
root@xiaoxi 14:43 mysql>create table t2(id int) default character set=utf8;
Query OK, 0 rows affected (0.01 sec)
# 指定字符集的优先级高于默认继承的字符集
root@xiaoxi 14:44 mysql>show create table t2;
+-------+--------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2.4 DBMS
database management system
mysql就是一个DBMS:数据库管理系统(管理数据库的软件)
DBMA的使用目的:
1. 文件数量过多,查询不方便
2. 跨机器查询不方便
3. 并发的控制
三、MySQL进程之间的关系
mysqld_safe是mysqld的父进程,mysqld起了26个线程
mysqld_safe会监督mysqld,一旦mysqld挂掉,就会立刻重启一个mysqld
[root@ting-mysql ~]# ps aux|grep mysqld
root 988 0.0 0.0 115744 1848 ? S 22:45 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ting-mysql.pid
mysql 1820 2.4 11.6 1543940 216756 ? Sl 22:57 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=ting-mysql.err --open-files-limit=8192 --pid-file=/data/mysql/ting-mysql.pid --socket=/data/mysql/mysql.sock --port=3306
root 1850 0.0 0.0 112828 984 pts/0 S+ 22:58 0:00 grep --color=auto mysqld
[root@ting-mysql ~]# kill -9 1820
[root@ting-mysql ~]# ps aux|grep mysqld
root 988 0.0 0.0 115744 1848 ? S 22:45 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ting-mysql.pid
mysql 1866 20.0 11.3 1412936 211772 ? Sl 22:58 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=ting-mysql.err --open-files-limit=8192 --pid-file=/data/mysql/ting-mysql.pid --socket=/data/mysql/mysql.sock --port=3306
root 1896 0.0 0.0 112828 988 pts/0 S+ 22:58 0:00 grep --color=auto mysqld