In my case I had a huge database (4.5 GB) imported from 5.7 to 8.0 and when running wp search-replace the error MySQL server has gone away occurred on wp-cli's queries on huge tables. So I've changed the /etc/mysql/conf.d/custom.cnf to the following:
[mysqld]
datadir = /var/lib/mysql/
socket = /var/run/mysqld/mysqld.sock
pid_file = /var/run/mysqld/mysqld.pid
tmp_table_size = 256M
max_heap_table_size = 256M
max_connections = 2000
open_files_limit = 100000
innodb_buffer_pool_instances = 8
# since 8.0 takes care of:
# innodb_buffer_pool_size
# innodb_log_file_size
# innodb_log_files_in_group
# innodb_flush_method
#https://dev.mysql.com/doc/refman/8.0/en/innodb-dedicated-server.html:
#Only consider enabling innodb_dedicated_server if the MySQL instance resides on a dedicated server where it can use all available system resources. For example, consider enabling if you run MySQL Server in a Docker container or dedicated VM that only runs MySQL. Enabling innodb_dedicated_server is not recommended if the MySQL instance shares system resources with other applications.
innodb_dedicated_server = On
innodb_buffer_pool_dump_at_shutdown = 1
innodb_buffer_pool_load_at_startup = 1
max_connections = 500
Since then not only the import of the sql file went 10 times faster (only 10 minutes by making advantage of the full machine's 50-60 GB memory), but also the wp-cli's queries now run without any issues.
Though, the key setting that helped me to get rid of the error (innodb_dedicated_server = On) is only applicable on docker containers and VPSs / dedicated servers running only MySQL. If the mysql server is running along other servers (PHP, Nginx etc) on one machine / in one container then you need to adjust the four values that innodb_dedicated_server is taking care of by yourself.