什么是Mysqli连接?

This is the what I can visualise at the moment:

MySQL server is running, which is an application that listens on a port (something I'm not 100% clear on at the moment).

The server parses PHP code, part of which involves sending input to the MySQL server (which is a completely separate program via specific ports (code written in the mysqli class?),

which then returns output that the mysqli class interprets? and these messages are interpreted into errors, or an established connection, or a successful query, etc?

Is this a reasonably correct view of MySQL/PHP communication?

MySQL is a program which runs persistently in the background somewhere and manages data storage. It offers an interface to the outside world via a socket connection, on which it accepts SQL queries which prompt it to store new data or return existing data. To connect to this socket and send SQL queries over it, you need to speak a specific protocol that MySQL expects; it's just technical minutiae of how exactly to talk to MySQL over that socket. mysqli is one of the PHP libraries which can speak that protocol and thereby offers PHP code a method to talk to MySQL in SQL queries, abstracting the specifics of the socket protocol away. The mysql library (now old and deprecated) and PDO can do the same thing, they just "look" different on the PHP side (i.e. their PHP code interfaces are different from mysqli).

The (mostly) complete chain is:

  • browser speaks to web server via HTTP-over-TCP-socket
  • web server loads PHP module which loads and executes your code
  • your code instructs the PHP mysqli module to establish a socket connection to MySQL
  • your code instructs mysqli to send SQL queries over the established connection to MySQL (SQL-over-TCP-socket or SQL-over-UNIX-socket)
  • your code does whatever is does with the result, spitting out a response to the web server
  • the web server returns PHP's output to the browser over the open HTTP connection