Mysql binary Connection method detailed _mysql

Source: Internet
Author: User

Connect using MySQL Binary

You can use MySQL binary to enter the MySQL command prompt to connect to the MySQL database.

Instance

The following is a simple example of connecting to a MySQL server from the command line:

Copy Code code as follows:

[root@host]# mysql-u Root-p
Enter password:******

The mysql> Command Prompt window appears when the login succeeds, and you can execute any SQL statements on it.

After the above command executes, the login success output is as follows:

Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 2854760 to server version:5.0.9
Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.

In the above example, we use the root user login to the MySQL server, of course, you can also use other MySQL user login.

If the user has sufficient permissions, any user can perform SQL operations in the MySQL Command Prompt window.

You can exit the mysql> Command Prompt window by using the Exit command, as follows:

Copy Code code as follows:

Mysql> exit
Bye

Use PHP script to connect to MySQL

PHP provides the mysql_connect () function to connect to the database.

The function has 5 parameters that return the connection identity after the successful link to MySQL, and the failure returns FALSE.

Grammar

Copy Code code as follows:

Connection mysql_connect (Server,user,passwd,new_link,client_flag);

Parameter description:

Parameters Description
Server

Optional. Specify the server to connect to.

You can include a port number, such as "Hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If the PHP instruction mysql.default_host is undefined (default), the default value is ' localhost:3306 '.

User Optional. User name. The default value is the user name of the server process owner.
passwd Optional. Password. The default value is a blank password.
New_link Optional. If mysql_connect () is invoked the second time with the same argument, no new connection will be established, and the connection ID that is already open will be returned. The parameter New_link changes this behavior and causes mysql_connect () to always open a new connection, even when mysql_connect () was previously invoked with the same parameters.
Client_flag

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

  • MYSQL_CLIENT_SSL-Using SSL encryption
  • Mysql_client_compress-Using Compression protocol
  • Mysql_client_ignore_space-Allow the interval after the function name
  • Mysql_client_interactive-interactive timeout inactive time before the connection is allowed to close

You can use PHP's Mysql_close () function to disconnect from the MySQL database.

This function has only one parameter for the mysql_connect () function to create the MySQL connection identifier returned after successful connection.

Grammar

BOOL Mysql_close (Resource $link _identifier);

This function closes a non-persistent connection to the MySQL server associated with the specified connection identity. If Link_identifier is not specified, the last open connection is closed.

Tip: You typically do not need to use mysql_close (), because an open non-persistent connection shuts down automatically after the script has finished executing.

Note: mysql_close () does not close persistent connections established by Mysql_pconnect ().

Instance

You can try the following examples to connect to your MySQL server:

 
 

The above is a small set to introduce the MySQL binary connection method detailed, I hope to help everyone!

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.