title: mysql on docker: Can't start server : Bind on unix socket: Invalid argument
body: <p>I have docker compose yaml:</p>
  <pre><code>version: &quot;3.8&quot;
  services:
    mysql:
      image: mysql/mysql-server:8.0.27
      container_name: javanotifications_mysql
      volumes:
        - ./persistence/mysql_db:/var/lib/mysql
  </code></pre>
  <p>and after running</p>
  <pre><code>docker compose up
  </code></pre>
  <p>I got</p>
  <pre><code>[Entrypoint] MySQL Docker Image 8.0.27-1.2.6-server
  [Entrypoint] Starting MySQL 8.0.27-1.2.6-server
  2022-04-22T23:45:52.061082Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 1
  2022-04-22T23:45:52.065609Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
  2022-04-22T23:45:52.077805Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  2022-04-22T23:45:52.534236Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  2022-04-22T23:45:52.803994Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
  2022-04-22T23:45:52.804064Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
  2022-04-22T23:45:52.808602Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
  2022-04-22T23:45:52.808689Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
  2022-04-22T23:45:52.818496Z 0 [ERROR] [MY-010270] [Server] Can't start server : Bind on unix socket: Invalid argument
  2022-04-22T23:45:52.818539Z 0 [ERROR] [MY-010258] [Server] Do you already have another mysqld server running on socket: /var/lib/mysql/mysql.sock ?
  2022-04-22T23:45:52.818778Z 0 [ERROR] [MY-010119] [Server] Aborting
  2022-04-22T23:45:54.322278Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.27)  MySQL Community Server - GPL.
  </code></pre>
  <p>and container dies.</p>
  <p>After cleaning &quot;persistence&quot; directory and repeating the process everything is the same</p>
  <p>The only thing that comes to my mind is that it has some problems with /var/lib/mysql/mysql.sock, mysql doesn't like to have it (maybe) in mounted directory.</p>
  <p>Then question comes, how to tell mysql to create it somewhere else in the container - beyond mounted directory /var/lib/mysql/?</p>
  <pre><code>Docker version 20.10.14, build a224086
  MacBook Pro 16-inch, 2019, 2.4 GHz 8-Core Intel Core i9
  macOS Big Sur version 11.6
  </code></pre>
  <p>--------- edit -----</p>
  <p>So based on suggestion provided below by Sergio Santiago i've ended up with this solution, which works perfectly, eliminating this error:</p>
  <pre><code>version: &quot;3.8&quot;
  services:
    mysql:
      image: mysql/mysql-server:8.0.27
      container_name: javanotifications_mysql
      ports:
        - &quot;${MYSQL_PORT}:3306&quot;
      environment:
        MYSQL_ROOT_PASSWORD: ${MYSQL_PASS}
        MYSQL_ROOT_HOST: &quot;%&quot;
      volumes:
        - ./mysql/my.cnf:/etc/my.cnf
        - ./mysql_db/${PROJECT_NAME}:/var/data
  </code></pre>
  <p>where mysql/my.cnf have this content</p>
  <pre><code># For advice on how to change settings please see
  # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  
  [mysqld]
  #
  # Remove leading # and set to the amount of RAM for the most important data
  # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  # innodb_buffer_pool_size = 128M
  #
  # Remove leading # to turn on a very important data integrity option: logging
  # changes to the binary log between backups.
  # log_bin
  #
  # Remove leading # to set options mainly useful for reporting servers.
  # The server defaults are faster for transactions and fast SELECTs.
  # Adjust sizes as needed, experiment to find the optimal values.
  # join_buffer_size = 128M
  # sort_buffer_size = 2M
  # read_rnd_buffer_size = 2M
  
  # Remove leading # to revert to previous value for default_authentication_plugin,
  # this will increase compatibility with older clients. For background, see:
  # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
  # default-authentication-plugin=mysql_native_password
  skip-host-cache
  skip-name-resolve
  socket=/var/lib/mysql/mysql.sock
  secure-file-priv=/var/lib/mysql-files
  user=mysql
  
  pid-file=/var/run/mysqld/mysqld.pid
  
  
  
  # modified by US VVV
  # NOTE: original localization of the my.cnf in the image is /etc/my.cnf
  datadir=/var/data
  </code></pre>