When mysql is connected using mysqli function in PHP, the following error occurs: warning: mysqli: real_connect () :( hy.

Source: Internet
Author: User
This article mainly introduces how to use mysqli functions in PHP to connect to mysql. the following error occurs: warning: mysqli: real_connect () :( hy0001040 ):... for more information, see the background: when I replace mysql with mysqli, there are too many connections, which is actually not because I moved the php sock file location, because these socket modifications are not completely modified, so the too connector connections, from mysql show processlist does not find a connection, in fact, using tshark to capture the package is estimated to see (http://justwinit.cn/post/7458 ), I didn't send a request, but I guess it was reported by the mysqli client. Although this problem is not a problem, I tried to reinstall Php for a long time, it turns out that the path is wrong, and the client of mysqli prompts that the number of connections is too large, leading to incorrect direction. As follows:

[Root @ iZ25z0ugwgtZ etc] # grep-r "mysql. sock ". /. /php. ini: pdo_mysql.default_socket =/data/runsock/mysqlsock/mysql. sock. /php. ini:; mysql. default_socket =/tmp/mysql. sock. /php. ini: mysql. default_socket =/data/runsock/mysqlsock/mysql. sock. /php. ini: mysqli. default_socket =/data/runsock/mysql. sock // This location is moved to/data/runsock/mysqlsock/mysql. caused by sock.

After modification, remember to restart php-fpm:

[root@iZ25z0ugwgtZ etc]# service php-fpm restartGracefully shutting down php-fpm . doneStarting php-fpm done

______________________ The troubleshooting points are as follows _______________________________

Warning: mysqli: real_connect (): (hy000/1040): too connector connections in:

Scenario:Manually compile and install mysql, and set the installation location. php connects to mysql in localhost mode.

Cause analysis:All mysql files after manual compilation and installation are under the specified directory or data directory. by default, php only searches for/temp/mysql. the sock file cannot be found.

Solution:

1. create a soft link for the sock file

Ln-s/data/mysqldb/mysql. sock/tmp/mysql. sock;

Or

2. modify the default mysql. sock connection address of php.

Mysql. default_socket =/data/mysqldb/mysql. sock

3. tcp socket connection

Mysql ('127. 0.0.1 ', 'username', 'passwod ');

The following describes the PHP mysql_connect () function.

Definition and usage

The mysql_connect () function opens a non-persistent MySQL connection.

Syntax

mysql_connect(server,user,pwd,newlink,clientflag)

Parameters Description
Server

Optional. Specifies the server to be connected.

It can include the port number, for example, "hostname: port", or the path to the local socket, for example, ":/path/to/socket" for localhost ".

If the PHP command mysql. default_host is not defined (the default value is true), the default value is 'localhost: 3306 '.

User Optional. User name. The default value is the user name of the server process owner.
Pwd Optional. Password. The default value is null.
Newlink Optional. If the same parameter is used to call mysql_connect () for the second time, a new connection is not established, but an opened connection id is returned. The new_link parameter changes this behavior and makes mysql_connect () always open a new connection, even when mysql_connect () was previously called with the same parameter.
Clientflag

Optional. The client_flags parameter can be a combination of the following constants:

  • MYSQL_CLIENT_SSL-SSL encryption
  • MYSQL_CLIENT_COMPRESS-use the compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE-interval after the function name is allowed
  • MYSQL_CLIENT_INTERACTIVE-allow interaction timeout and inactivity before closing the connection

Return value

If the connection succeeds, a MySQL connection id is returned. if the connection fails, FALSE is returned.

Tips and comments

Note: When the script ends, the connection to the server is closed, unless mysql_close () has been explicitly called before.
Tip: to create a persistent connection, use the mysql_pconnect () function.

Example

<? Php $ con = mysql_connect ("localhost", "mysql_user", "mysql_pwd"); if (! $ Con) {die ('could not connect: '. mysql_error () ;}// some code... mysql_close ($ con);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.