[MySQL] Daily record sharing

Optimize debugging

1. Slow log:

Configure slow log: http://www.cnblogs.com/luyucheng/p/6265594.html
pt-query-digest detailed slow query log: http://www.ywnds.com/?p=8179

grammar

1. group_concat:

Grouping query shows the use of functions CONCAT and GROUP_CONCAT in groups according to grouping fields

Environment configuration

1. MySQL database security hardening

1. MySQL import sql error Got a packet bigger than'max_allowed_packet' bytes

When testing whether the mysql backup file can be restored normally, about to restore the backup file to another server instance, a series of errors occurred.
The errors are as follows:
[Err] 1153 - Got a packet bigger than 'max_allowed_packet' bytes [Err] 1046 - No database selected [Err] 1231 - Variable 'collation_connection' can't be set to the value of 'NULL'
………………

  • Reason:
    Mainly the first error. The imported data packet is larger than the max_allowed_packet size set by the system.
    The restored instance version is: mysql 5.7.13-log The
    default max_allowed_packet size is: 4M
    In mysql, 1 sql statement is sent to the MySQL server, 1 row of data is sent to the client, or the binary log is sent from the master to the slave. As a package, (mysql 5.7 maximum package is 1GB). In my backup script, some table fields are of longtext type, and the inserted value is relatively long, which caused the error.

  • Solution:

    Since 4M is not enough, set 20 MB.

    View the current max_allowed_packet size:

    show variables like 'max_allowed_packet';  
    show variables where Variable_name = 'max_allowed_packet';
    

    Set global variables on the command line, the new connection will take effect immediately: (write byte size in windows)
    set global max_allowed_packet = 20*1024*1024;

    At the same time, add the following parameters in the configuration file:max_allowed_packet = 20M

2. mysqli::real_connect(): Headers and client library minor version mismatch

yum remove php-mysql
yum install php-mysqlnd

// 重启mysql服务
systemctl start mysqld

// 第一个检索的“password”后面root@localhost: 后面跟着的密码应该就是默认密码了。
grep password /var/log/mysqld.log 

3. Unable to start MySQL/MariaDB after changing the innodb_log_file parameter: log file ./ib_logfile0 is of different size

solution

4. Unable to update Plesk: Aria engine is not enabled or did not start

solution

5. MySQL connect to IPv6 database

Project configuration: [ipv6 address] (enclosed in square brackets)

On the server: --bind-address=ipv6地址
To support dual stack, it's OK--bind-address= ::

6. View MySQL log

 # 查看mysql启动失败的日志,从日志内容寻找解决方案
	cat /var/log/mysqld.log | tail -30 

7. Configure the master-slave database

  • Failed to load slave replication state from table mysql.gtid_slave_pos: 1932: Table 'mysql.gtid_slave_pos' doesn't exist in engine
    solution

  • Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

    第一步: 空格问题
    	看主库 MySQL配置文件(/etc/my.cnf)中 log-bin=/var/lib/mysql/binlog.index  是否有空格
    第二步: 看文件
    	查看binlog.index ,或者说查看/var/lib/mysql 的文件情况
    第三步: 从库操作
        mysql > stop slave;
        mysql > reset slave;
        mysql > start slave;
    
  • Reset master and slave: https://www.cnblogs.com/sunyuxun/archive/2012/09/13/2683338.html

  • ERROR 1201 (HY000): Could not initialize master info structure for ''; more error messages can be found in the MariaDB error log
    Reset slave library: reset slave;-> start slave;-> show slave status\G;OK

  • MySQL master-slave inconsistency situation and solutions
    https://blog.csdn.net/hardworking0323/article/details/81046408?utm_source=blogxgwz0

  • slave 2013 lost connection to MySQL server at 'reading initial communication packet

    [mysqld] 段增加一个启动参数 
    
    skip-name-resolve 
    
  • Error ‘Can’t find any matching row in the user table’ on query. Default database: ‘’. Query: ‘GRANT CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON . TO ‘mysqld’@’%’ WITH GRANT OPTION’

    删除mysqld 的 从库 权限
    revoke replication slave, replication client on *.* from 'mysqld'%';
    报错: error 1290 (hy000) the mysql server is running with the --skip-grant-tables
    执行: flush privileges;
    重新执行: revoke replication slave, replication client on *.* from 'mysqld'%';
    再执行 :  flush privileges;
    
    

8. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

https://www.cnblogs.com/tongxiaoda/p/7873478.html
检测配置

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | STRONG |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.02 sec)

mysql> set password=password('abc');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password=password('mysql2017');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password=password('mysql@)!&');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password=password('MY@)!&sql2017');
Query OK, 0 rows affected, 1 warning (0.00 sec)

关于validate_password_policy-密码强度检查等级:

Policy          Tests Performed
0 or LOW    Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file

9. mysql is not case sensitive by default

It is found that collate (proofreading) needs to be set by querying the information. collate rules:

  • _bin: indicates the binary case sensitive collation, which means it is case sensitive
  • _cs: case sensitive collation, case sensitive
  • _ci: case insensitive collation, case-insensitive
    solution:
  1. You can use query conditions binary. such as:select * from TableA where binary columnA ='aaa';
  2. You can modify the collation of this field to binary
  3. When creating a table, set
    create table some_table(
       str char(20) binary 
    )

10. mysql index is too long 1071-max key length is 767 byte

问题
create table: Specified key was too long; max key length is 767 bytes
 
原因
数据库表采用utf8编码,其中varchar(255)的column进行了唯一键索引
而mysql默认情况下单个列的索引不能超过767位(不同版本可能存在差异)
 
于是utf8字符编码下,255*3 byte 超过限制
 
解决
1  使用innodb引擎;
2  启用innodb_large_prefix选项,将约束项扩展至3072byte;
3  重新创建数据库;
 
my.cnf配置:
default-storage-engine=INNODB
innodb_large_prefix=on
 
 
一般情况下不建议使用这么长的索引,对性能有一定影响;

原文链接:https://www.cnblogs.com/littleatp/p/4612896.html

参考文档:
https://dev.mysql.com/doc/refman/5.5/en/innodb-restrictions.html

11. unknown variable ‘bind-address=0.0.0.0’

出现问题的情况
[mysql]
bind-address=::  (127.0.0.1 或者 0.0.0.0)


解决方案:
在 /etc/mysql/my.cnf  中添加
[mysqld]
bind-address=::

12. Solve the problem that MariaDB can log in without a password

1 正常mysql
2 mysql> select user, plugin from mysql.user where plugin = 'mysql_native_password';
3 +-----------+-----------------------+
4 | user      | plugin                |
5 +-----------+-----------------------+
6 | root      | mysql_native_password |
7 +-----------+-----------------------+
8 8 rows in set (0.00 sec)
 

1 不正常的
2 
3 MariaDB [(none)]> select user, plugin from mysql.user;
4 +------+-------------+
5 | user | plugin      |
6 +------+-------------+
7 | root | unix_socket |
8 +------+-------------+
9 1 row in set (0.00 sec)
看到这里应该发现问题了,按照正常的修改就行了

如下:

 1 sudo service mysql stop
 2 sudo mysqld_safe --skip-grant-tables
 3 进去mysql执行如下命令:
 4 MariaDB [(none)]> UPDATE mysql.user SET authentication_string = PASSWORD('mypassword'), plugin = 'mysql_native_password' WHERE User = 'root' AND Host = 'localhost';
 5 MariaDB [(none)]> FLUSH PRIVILEGES;
 6 验证:
 7 MariaDB [(none)]> select user, plugin from mysql.user
 8     -> ;
 9 +------+-----------------------+
10 | user | plugin                |
11 +------+-----------------------+
12 | root | mysql_native_password |
13 +------+-----------------------+
14 1 row in set (0.01 sec)
15 
16 先杀死mysql  kill -9 pid
17 启动:
18 sudo service mysql start
最后验证下:需要密码了

root@ubuntu:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@ubuntu:~# 

13. 报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var /lib/mysql/mysql.sock’ (2)

Generally, when the fault occurs, the user does not switch to the mysql user, which causes permission problems and cannot create the mysql authorization table, so the /tmp/mysql.sock and hostname.pid files cannot be created. So re-authorize and start safely

mysql_install_db     //重建授权表
mysqld_safe &      //mysqld安全启动
systemctl restart apache2        //重启服务器

14. Can’t init tc log

MariaDB Fix “Can’t init tc log” Error
February 27, 2018 at 12:12 · Filed under Software

Imessed up my automation of backups, meaning that after two years my entire VPS had secretly filled up. This lead to MariaDB being unable to initialize. After taking care of the root cause MariaDB still refused to start.

$ sudo tail -3 /var/log/mysql/error.log
2018-02-20 12:07:45 140649776292416 [Note] Recovering after a crash using tc.log
2018-02-20 12:07:45 140649776292416 [ERROR] Can't init tc log
2018-02-20 12:07:45 140649776292416 [ERROR] Aborting

解决
just removing the zero byte `/var/lib/mysql/tc.log` file took care of the problem.
只需删除零字节的“ /var/lib/mysql/tc.log”文件即可解决此问题。

truncate clear all table data

select CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';') from INFORMATION_SCHEMA.TABLES where table_schema in ('db1','db2');

search result:

+------------------------------------------------------------+
| CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';') |
+------------------------------------------------------------+
| truncate TABLE db1.tablename1;                             |
| truncate TABLE db1.tablename2;                             |
| truncate TABLE db1.tablename3;                             |
| truncate TABLE db2.tablename1;                             |
| truncate TABLE db2.tablename2;                             |
| truncate TABLE db2.tablename3;                             |
+------------------------------------------------------------+

Organize the format and execute

truncate TABLE db1.tablename1;
truncate TABLE db1.tablename2;
truncate TABLE db1.tablename3;
truncate TABLE db2.tablename1;
truncate TABLE db2.tablename2;
truncate TABLE db2.tablename3;

Guess you like

Origin blog.csdn.net/qq_22227087/article/details/82591826