1.Windows下面的配置文件是:my.ini
[mysql]
default-character-set=utf8
[mysqld]
port=3306
default_authentication_plugin=mysql_native_password
basedir=E:/phpStudy/phpstudy_pro/Extensions/MySQL8.0.12/
datadir=E:/phpStudy/phpstudy_pro/Extensions/MySQL8.0.12/data/
character-set-server=utf8
default-storage-engine=MyIsam
max_connections=1000
collation-server=utf8_unicode_ci
init_connect='SET NAMES utf8'
innodb_buffer_pool_size=64M
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=120
innodb_log_buffer_size=4M
innodb_log_file_size=256M
interactive_timeout=120
join_buffer_size=2M
key_buffer_size=32M
log-error="E:/phpStudy/phpstudy_pro/Extensions/MySQL8.0.12/data"
log_error_verbosity=1
max_allowed_packet=16M
max_connections=1000
max_heap_table_size=64M
myisam_max_sort_file_size=64G
myisam_sort_buffer_size=32M
read_buffer_size=512kb
read_rnd_buffer_size=4M
server_id=1
skip-external-locking=on
sort_buffer_size=256kb
table_open_cache=256
thread_cache_size=16
tmp_table_size=64M
wait_timeout=120
[client]
port=3306
default-character-set=utf8
default-strong-engine=INNODB
主从和集群都要修改配置文件
2.Linux系统下面的配置文件是:my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
secure_file_priv=/var/lib/mysql
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
tmp_table_size = 32M
default_authentication_plugin = mysql_native_password
lower_case_table_names = 1
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log_error=/usr/local/mysql/data/error.err
explicit_defaults_for_timestamp = true
max_connections = 1000
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
binlog_expire_logs_seconds = 600000
slow_query_log=1
slow-query-log-file=/usr/local/mysql/data/mysql-slow.log
long_query_time=3
early-plugin-load = ""
innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1
[mysqldump]
quick
max_allowed_packet = 500M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
Bin目录下的mysql程序:
程序 | 描述 |
---|---|
mysqld | mysql服务器进程,只有该程序运行之后,客户端才能连接操作mysql数据库 |
mysql | 连接mysql |
mysqlbinlog | 用于从二进制日志读取语句。在二进制日志文件中包含执行的语句,可用来帮助系统从崩溃中恢复 |
mysqladmin | 用来检索版本、进程、以及服务器的状态信息 |
mysqlcheck | 检查、修复、分析以及优化表的表维护 |
mysqldump | 将mysql数据库转储到一个文件,导出sql数据 |
mysqlshow | 慢查询语句分析 |
错误:
[root@localhost mysql]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
产生原因:无法连接到本地的mysql服务器
解决方法:
[root@localhost mysql]# sudo ./support-files/mysql.server start
Starting MySQL.. SUCCESS!
[root@localhost mysql]# mysql -uroot -p
Data目录:
drwxr-x---. 2 mysql mysql 38 3月 31 04:52 ailili
-rw-r-----. 1 mysql mysql 56 3月 28 15:38 auto.cnf//机器id
-rw-------. 1 mysql mysql 1680 3月 28 15:38 ca-key.pem//证书
-rw-r--r--. 1 mysql mysql 1112 3月 28 15:38 ca.pem//证书
-rw-r--r--. 1 mysql mysql 1112 3月 28 15:38 client-cert.pem
-rw-------. 1 mysql mysql 1680 3月 28 15:38 client-key.pem
-rw-r-----. 1 mysql mysql 14260 4月 1 04:50 error.err//错误日志
-rw-r-----. 1 mysql mysql 49152 4月 1 04:52 #ib_16384_0.dblwr
-rw-r-----. 1 mysql mysql 8437760 3月 28 15:38 #ib_16384_1.dblwr
-rw-r-----. 1 mysql mysql 3531 4月 1 04:35 ib_buffer_pool
-rw-r-----. 1 mysql mysql 10485760 4月 1 04:50 ibdata1//共享表空间文件
-rw-r-----. 1 mysql mysql 67108864 4月 1 04:52 ib_logfile0//事务重铸
-rw-r-----. 1 mysql mysql 67108864 3月 28 15:38 ib_logfile1//事务回滚
-rw-r-----. 1 mysql mysql 12582912 4月 1 04:50 ibtmp1
drwxr-x---. 2 mysql mysql 187 4月 1 04:50 #innodb_temp
-rw-r-----. 1 mysql mysql 5 4月 1 04:50 localhost.localdomain.pid//mysql进程id的文件
drwxr-x---. 2 mysql mysql 143 3月 28 15:38 mysql
-rw-r-----. 1 mysql mysql 179 3月 28 15:38 mysql-bin.000001
-rw-r-----. 1 mysql mysql 1205 3月 29 04:11 mysql-bin.000002
-rw-r-----. 1 mysql mysql 179 3月 29 04:21 mysql-bin.000003
-rw-r-----. 1 mysql mysql 987 3月 31 03:54 mysql-bin.000004
-rw-r-----. 1 mysql mysql 34891869 4月 1 04:35 mysql-bin.000005
-rw-r-----. 1 mysql mysql 179 4月 1 04:35 mysql-bin.000006
-rw-r-----. 1 mysql mysql 156 4月 1 04:50 mysql-bin.000007//二进制日志
-rw-r-----. 1 mysql mysql 133 4月 1 04:50 mysql-bin.index//索引
-rw-r-----. 1 mysql mysql 25165824 4月 1 04:50 mysql.ibd
-rw-r-----. 1 mysql mysql 1257 4月 1 04:50 mysql-slow.log//慢查询日志文件
drwxr-x---. 2 mysql mysql 8192 3月 28 15:38 performance_schema
-rw-------. 1 mysql mysql 1680 3月 28 15:38 private_key.pem
-rw-r--r--. 1 mysql mysql 452 3月 28 15:38 public_key.pem
-rw-r--r--. 1 mysql mysql 1112 3月 28 15:38 server-cert.pem
-rw-------. 1 mysql mysql 1676 3月 28 15:38 server-key.pem
drwxr-x---. 2 mysql mysql 28 3月 28 15:38 sys
-rw-r-----. 1 mysql mysql 16777216 4月 1 04:52 undo_001//回滚日志
-rw-r-----. 1 mysql mysql 16777216 4月 1 04:52 undo_002
-rw-r--r--. 1 root root 0 3月 28 15:48 vi
myisam:索引与数据是分开存储的
.MYD 存储数据
.MYI 存储索引
INNODB(主键索引):索引与数据是不分开存储
.ibd : 独享空间:一个表一个空间
.ibdatal :共享空间: 很多表共享一个文件
Btree树
3.mysql的日志:
查询日志,慢查询日志,错误日志,二进制日志,事务日志(redo,undo),中继日志
查询日志
mysql的查询日志记录了所有mysql数据库请求的信息。无论这些请求是否得到了正确的执行。默认文件名为hostname.log。默认情况下MySQL查询日志是 关闭的。生产环境,如果开启mysql查询日志,对性能还是有蛮大的影响的。另外很多时候,MySQL慢查询日志基本可以定位那些出现性能问题的SQL,所以MySQL查询日志应用的场景不多。
开启查询日志:
show variables like "%general_log%"; --查询日志是否开启
set global general_log="on"; --设置查询日志的关闭和开启
慢查询日志
MySQL的 慢查询日志是MySQL提供的一种日志记录,它用来记录在mysql中响应时间超过阀值的语句,具体值运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中。long_query_time的默认值为10,意思是运行10s以上的语句。默认情况下,MySQL数据库并不启动慢查询日志,需要我们手动来设置这个参数,当然,如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响。
开启慢查询日志:
slow_query_log=1; --开启慢查询日志
slow-query-log-file=path+文件名 --慢查询日志目录及文件
long_query_time=3; --设置最大等待时间
show variables like "%slow%"; --查询慢查询日志是否开启
mysqlshow
bin目录中的 mysqlshow 是帮助文档,通过mysqlshow --help可以查看参数,进行分析。
mysqlshow Ver 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Shows the structure of a MySQL database (databases, tables, and columns).
Usage: mysqlshow [OPTIONS] [database [table [column]]]
If last argument contains a shell or SQL wildcard (*,?,% or _) then only
what's matched by the wildcard is shown.
If no database is given then all matching databases are shown.
If no table is given, then all matching tables in database are shown.
If no column is given, then all matching columns and column types in table
are shown.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
The following groups are read: mysqlshow client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
--login-path=# Read this path from the login file.
--bind-address=name IP address to bind to.
-c, --character-sets-dir=name
Directory for character set files.
--default-character-set=name
Set the default character set.
--count Show number of rows per table (may be slow for non-MyISAM
tables).
-C, --compress Use compression in server/client protocol.
-#, --debug[=name] Output debug log. Often this is 'd:t:o,filename'.
--debug-check Check memory and open file usage at exit.
--debug-info Print some debug info at exit.
--default-auth=name Default authentication client-side plugin to use.
--enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
-?, --help Display this help and exit.
-h, --host=name Connect to host.
-i, --status Shows a lot of extra information about each table.
-k, --keys Show keys for table.
-p, --password[=name]
Password to use when connecting to server. If password is
not given, it's solicited on the tty.
--plugin-dir=name Directory for client-side plugins.
-P, --port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-t, --show-table-type
Show table type column.
-S, --socket=name The socket file to use for connection.
--server-public-key-path=name
File path to the server public RSA key in PEM format.
--get-server-public-key
Get server public key
--ssl-mode=name SSL connection mode.
--ssl-ca=name CA file in PEM format.
--ssl-capath=name CA directory.
--ssl-cert=name X509 cert in PEM format.
--ssl-cipher=name SSL cipher to use.
--ssl-key=name X509 key in PEM format.
--ssl-crl=name Certificate revocation list.
--ssl-crlpath=name Certificate revocation list path.
--tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1,
TLSv1.2, TLSv1.3
--ssl-fips-mode=name
SSL FIPS mode (applies only for OpenSSL); permitted
values are: OFF, ON, STRICT
--tls-ciphersuites=name
TLS v1.3 cipher to use.
-u, --user=name User for login if not current user.
-v, --verbose More verbose output; you can use this multiple times to
get even more verbose output.
-V, --version Output version information and exit.
--compression-algorithms=name
Use compression algorithm in server/client protocol.
Valid values are any combination of
'zstd','zlib','uncompressed'.
--zstd-compression-level=#
Use this compression level in the client/server protocol,
in case --compression-algorithms=zstd. Valid range is
between 1 and 22, inclusive. Default is 3.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
--------------------------------- ----------------------------------------
bind-address (No default value)
character-sets-dir (No default value)
default-character-set auto
count FALSE
compress FALSE
debug-check FALSE
debug-info FALSE
default-auth (No default value)
enable-cleartext-plugin FALSE
host (No default value)
status FALSE
keys FALSE
plugin-dir (No default value)
port 3306
show-table-type FALSE
socket /tmp/mysql.sock
server-public-key-path (No default value)
get-server-public-key FALSE
ssl-ca (No default value)
ssl-capath (No default value)
ssl-cert (No default value)
ssl-cipher (No default value)
ssl-key (No default value)
ssl-crl (No default value)
ssl-crlpath (No default value)
tls-version (No default value)
tls-ciphersuites (No default value)
user (No default value)
compression-algorithms (No default value)
zstd-compression-level 3
错误日志
MySQL错误日志是记录MySQL运行过程中较为严重的警告和错误信息,以及MySQL每次启动和关闭的详细信息。错误日志的命名通常为hostname.err。其中,hostname表示服务器主机名。
错误日志开启:
show variables like "%log_error%"; --查看错误日志
log_error = 错误日志路径与文件名 --配置文件中开启错误日志
归档备份:
mv host_name.err host_name.err-old
Mysql -uroot -p
flush logs;
binlog日志
二进制日志主要记录mysql数据库的变化,二进制日志包含所有更新了数据或者潜在更新了数据(如没有匹配到任何行的delete语句),语句以时间的形式保存,描述了数据的更改。二进制日志还包含执行每个更新数据库语句的时间信息,使用二进制日志的主要目的是最大可能的恢复数据库,也可以用来做主从复制。
开启二进制日志:
show variables like "%log_bin%"; --查看二进制日志是否开启
log_bin = 二进制日志路径+文件名 --设置二进制日志路径
server-id =1; --mysql服务id,必须
max_binlog_size = 100m; --单个日志文件大小
二进制日志的作用-数据恢复
根据数据节点恢复:./mysqlbinlog --start-position=968 --stop-position=1205 /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p
根据时间恢复:./mysqlbinlog-start-datatime="2021-11-02 14:00:00" --stop-datetime="2021-11-03" 14:00:00 /usr/local/mysql/data/mysql-bin.000005 | mysql -uroot -p
直接运行:./mysqlbinlog/usr/local/mysql/data/mysql-bin.000005 | mysql -uroot -p
二进制日志的操作
show master status; --查看当前使用的二进制文件
show binlog events in "mysql-bin.000001"; --查询指定的二进制文件信息
show binary logs; --查询当前mysql所有的二进制文件
show binlog events; --查询第一个日志文件
restet master;--清空所有的二进制文件
错误:
错误原因:已经有这个用户了
解决方法:直接运行创建库的操作