mysql的配置文件为/etc/my.cnf
配置文件查找次序:如果存在多个配置文件且多个配置文件中均有设定,则最后找到的配置文件生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/mysql常用配置文件参数:
参数说明port = 3306设置监听端口socket = /tmp/mysql.sock指定套接字文件位置basedir = /usr/local/mysql指定MySQL的安装路径datadir = /data/mysql指定MySQL的数据存放路径pid-file = /data/mysql/mysql.pid指定进程ID文件存放路径user = mysql指定MySQL以什么用户的身份提供服务skip-name-resolve禁止MySQL对外部连接进行DNS解析使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则MySQL将无法正常处理连接请求mysql差异备份 开启MySQL服务器的二进制日志功能
[root@localhost ~]# vi /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve server-id=10 log-bin=mysql_bi对数据库进行完全备份
[root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 16 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | yt | | yx | | yy | +--------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]> show tables from yy; +--------------+ | Tables_in_yy | +--------------+ | basic_info | | info | +--------------+ 2 rows in set (0.00 sec) MariaDB [(none)]> //完全备份 [root@localhost ~]# mysqldump -uroot -p123123 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-202010211406.sql [root@localhost ~]# ls all-201902211406.sql anaconda-ks.cfg [root@localhost ~]# //增加新内容 mysql> insert student (name,age) values ('lili',25),('xiaozhang',26); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from student; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | | 7 | lisi | NULL | | 8 | chenshou | 10 | | 9 | wangqu | 3 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | | 12 | lili | 25 | | 13 | xiaozhang | 26 | +----+-------------+------+ 13 rows in set (0.00 sec) MariaDB [yt]> update student set age = 15 where name = 'tom'; Query OK, 1 row affected (0.001 sec) Rows matched: 1 Changed: 1 Warnings: 0模拟误删数据
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | yt | +--------------------+ 5 rows in set (0.00 sec) //删除 [root@localhost ~]# mysql -uroot -p1234 -e 'drop database yt;' mysql: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>刷新创建新的二进制日志
[root@localhost ~]# ll /opt/data/ [root@localhost ~]# ll /opt/data/ total 122972 -rw-r-----. 1 mysql mysql 56 Oct 26 19:16 auto.cnf -rw-------. 1 mysql mysql 1680 Oct 26 19:16 ca-key.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 19:16 ca.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 19:16 client-cert.pem -rw-------. 1 mysql mysql 1680 Oct 26 19:16 client-key.pem -rw-r-----. 1 mysql mysql 353 Oct 26 19:31 ib_buffer_pool -rw-r-----. 1 mysql mysql 12582912 Oct 26 20:21 ibdata1 -rw-r-----. 1 mysql mysql 50331648 Oct 26 20:21 ib_logfile0 -rw-r-----. 1 mysql mysql 50331648 Oct 26 19:16 ib_logfile1 -rw-r-----. 1 mysql mysql 12582912 Oct 26 20:05 ibtmp1 -rw-r-----. 1 mysql mysql 18024 Oct 26 20:19 localhost.localdomain.err drwxr-x---. 2 mysql mysql 4096 Oct 26 19:16 mysql -rw-r-----. 1 mysql mysql 5 Oct 26 20:05 mysql.pid drwxr-x---. 2 mysql mysql 8192 Oct 26 19:16 performance_schema -rw-------. 1 mysql mysql 1676 Oct 26 19:16 private_key.pem -rw-r--r--. 1 mysql mysql 452 Oct 26 19:16 public_key.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 19:16 server-cert.pem -rw-------. 1 mysql mysql 1680 Oct 26 19:16 server-key.pem drwxr-x---. 2 mysql mysql 8192 Oct 26 19:16 sys //刷新创建新的二进制日志 [root@localhost ~]# mysqladmin -uroot -p1234 flush-logs mysqladmin: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# ll /opt/data/ total 122972 -rw-r-----. 1 mysql mysql 56 Oct 26 19:16 auto.cnf -rw-------. 1 mysql mysql 1680 Oct 26 19:16 ca-key.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 19:16 ca.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 19:16 client-cert.pem -rw-------. 1 mysql mysql 1680 Oct 26 19:16 client-key.pem -rw-r-----. 1 mysql mysql 353 Oct 26 19:31 ib_buffer_pool -rw-r-----. 1 mysql mysql 12582912 Oct 26 20:24 ibdata1 -rw-r-----. 1 mysql mysql 50331648 Oct 26 20:24 ib_logfile0 -rw-r-----. 1 mysql mysql 50331648 Oct 26 19:16 ib_logfile1 -rw-r-----. 1 mysql mysql 12582912 Oct 26 20:05 ibtmp1 -rw-r-----. 1 mysql mysql 18024 Oct 26 20:19 localhost.localdomain.err drwxr-x---. 2 mysql mysql 4096 Oct 26 19:16 mysql -rw-r-----. 1 mysql mysql 5 Oct 26 20:05 mysql.pid drwxr-x---. 2 mysql mysql 8192 Oct 26 19:16 performance_schema -rw-------. 1 mysql mysql 1676 Oct 26 19:16 private_key.pem -rw-r--r--. 1 mysql mysql 452 Oct 26 19:16 public_key.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 19:16 server-cert.pem -rw-------. 1 mysql mysql 1680 Oct 26 19:16 server-key.pem drwxr-x---. 2 mysql mysql 8192 Oct 26 19:16 sys恢复完全备份
[root@localhost ~]# mysql -uroot -p1234 <all-202010211406.sql mysql: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# mysql -uroot -p1234 -e 'show databases;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | yt | +--------------------+ [root@localhost ~]#恢复差异备份
[root@localhost ~]# ll /opt/data/ total 110688 -rw-r-----. 1 mysql mysql 56 Oct 26 15:58 auto.cnf -rw-------. 1 mysql mysql 1676 Oct 26 15:58 ca-key.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 15:58 ca.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 15:58 client-cert.pem -rw-------. 1 mysql mysql 1680 Oct 26 15:58 client-key.pem -rw-r-----. 1 mysql mysql 185 Oct 26 17:01 ib_buffer_pool -rw-r-----. 1 mysql mysql 12582912 Oct 26 17:01 ibdata1 -rw-r-----. 1 mysql mysql 50331648 Oct 26 17:01 ib_logfile0 -rw-r-----. 1 mysql mysql 50331648 Oct 26 15:58 ib_logfile1 -rw-r-----. 1 mysql mysql 40175 Oct 26 17:01 localhost.localdomain.err -rw-r-----. 1 mysql mysql 177 Oct 26 16:58 mysql_bi.000001 -rw-r-----. 1 mysql mysql 177 Oct 26 17:01 mysql_bi.000002 -rw-r-----. 1 mysql mysql 177 Oct 26 17:01 mysql_bi.000003 -rw-r-----. 1 mysql mysql 54 Oct 26 17:01 mysql_bi.index -rw-------. 1 mysql mysql 1676 Oct 26 15:58 private_key.pem -rw-r--r--. 1 mysql mysql 452 Oct 26 15:58 public_key.pem -rw-r--r--. 1 mysql mysql 1112 Oct 26 15:58 server-cert.pem -rw-------. 1 mysql mysql 1680 Oct 26 15:58 server-key.pem mysql> show binlog events in 'mysql_bin.000002'; +------------------+-----+----------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+----------------+-----------+-------------+---------------------------------------+ | mysql_bin.000002 | 4 | Format_desc | 10 | 123 | Server ver: 5.7.31-log, Binlog ver: 4 | | mysql_bin.000002 | 123 | Previous_gtids | 10 | 154 | | | mysql_bin.000002 | 154 | Anonymous_Gtid | 10 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql_bin.000002 | 219 | Query | 10 | 294 | BEGIN | | mysql_bin.000002 | 294 | Table_map | 10 | 352 | table_id: 124 (maqiang.student) | | mysql_bin.000002 | 352 | Write_rows | 10 | 420 | table_id: 124 flags: STMT_END_F | | mysql_bin.000002 | 420 | Xid | 10 | 451 | COMMIT /* xid=992 */ | | mysql_bin.000002 | 451 | Anonymous_Gtid | 10 | 516 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql_bin.000002 | 516 | Query | 10 | 591 | BEGIN | | mysql_bin.000002 | 591 | Table_map | 10 | 649 | table_id: 124 (maqiang.student) | | mysql_bin.000002 | 649 | Update_rows | 10 | 715 | table_id: 124 flags: STMT_END_F | | mysql_bin.000002 | 715 | Xid | 10 | 746 | COMMIT /* xid=995 */ | | mysql_bin.000002 | 746 | Anonymous_Gtid | 10 | 811 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql_bin.000002 | 811 | Query | 10 | 912 | drop database maqiang | | mysql_bin.000002 | 912 | Rotate | 10 | 959 | mysql_bin.000003;pos=4 | +------------------+-----+----------------+-----------+-------------+---------------------------------------+ 15 rows in set (0.00 sec) 使用mysqlbinlog恢复差异备份(此时恢复到删除之前的811,在mysql_bin.000002文件里) [root@localhost ~]# mysqlbinlog --stop-position=811 /opt/data/mysql_bin.000002 |mysql -uroot -pmaqiang123 mysql: [Warning] Using a password on the command line interface can be insecure [root@localhost ~]# mysql -e 'show databases;' +--------------------+ | Database | +--------------------+ | information_schema | | linux20200727 | | maqiang | | mysql | | performance_schema | | runtime | | sys | +--------------------+ //使用mysqlbinlog恢复差异备份 [root@localhost ~]# mysql -e 'select * from maqiang.student;' +----+---------+-------+ | id | name | score | +----+---------+-------+ | 1 | tom | 20 | | 2 | jerry | 98 | | 3 | zs | 60 | | 4 | lisi | 90 | | 5 | wnagwu | 87 | | 6 | qianliu | 67 | +----+---------+-------+**