Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of Connection Manager in Mysql

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces what is the role of Connection Manager in Mysql, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Number of threads connected

Mysql supports both single-threaded and multithreaded connections. If it is single-threaded, only one connection can connect to the Mysql at a time

Other connections will be suspended. If it is multithreaded, multiple connection can be connected to the server at the same time.

You can set the number of threads connected by setting the startup parameters of the server:

Mysqld.exe-thread-handling=no-threads

Mysqld.exe-thread-handling=one-thread-per-connection

How does the server use parameters to choose which way to use? Take a look at the branch code in the server:

# ifdef EMBEDDED_LIBRARY

One_thread_scheduler & thread_scheduler)

# else

If (global_system_variables.thread_handling name)-1

Break

}

If you are interested in parameter initialization, you can take a look at the function get_options, which will not be explained in detail here. Let's take a look at the source code of one_thread_scheduler and one_thread_per_connection_scheduler and see what they have done.

Void one_thread_scheduler (scheduler_functions* func)

{

Func- > max_threads= 1

# ifndef EMBEDDED_LIBRARY

Func- > add_connection= handle_connection_in_main_thread

# endif

Func- > init_new_connection_thread= init_dummy

Func- > end_thread= no_threads_end

}

Void one_thread_per_connection_scheduler (scheduler_functions* func)

{

Func- > max_threads= max_connections

Func- > add_connection= create_thread_to_handle_connection

Func- > end_thread= one_thread_per_connection_end

}

It turns out that the parameters of scheduler_functions in a structure are set, but these parameters are just function pointers, that is to say, in a specific call.

You just need to call add_connection or end_thread, and you don't need to know which function is called, which is probably a variant polymorphism.

two。 Initialize the network configuration

Network configuration is relatively simple, that is, set ports, create sockets, bind ports, and listen on ports. The implementation is all focused on the network_init function, because this function is accurate

There is really nothing to say, if you are not familiar with socket, you can go to the Internet to search for relevant knowledge. The corresponding pseudo code is given directly here:

Network_init

{

Set_ports; / / set the port number, # define MYSQL_PORT 3306

Socket;// creates sockets

Bind; / / bind port number

Listen;// snooping port number

}

3. Mode of connection

There are many other ways to communicate between processes, not just SOCKET. Mysql supports three connection modes: namepipe, socket, and shared memory

That is, how pipes, sockets, and shared memory are named. These three ways can coexist. Only sockets are used by default.

TCP/IP socket is the connection mode provided by MySQL on any platform, and it is also the most widely used way in the network. This method is established on the TCP/IP connection

A network-based connection request, typically the client is on one server, while the MySQL instance is on another server, and the two machines are connected through an TCP/IP network.

For example, I can request an instance of MySQL under a remote Linux server under a Windows server.

In Windows 2000, Windows XP, Windows 2003, and Windows Vista and later Windows operating systems, if the two need to communicate

If the process of the letter is on the same server, then named pipes can be used, and named pipes can also be used for local connections after SQL Server database installation by default. In the MySQL database

The-- enable-named-pipe option needs to be enabled in the configuration file. In versions of MySQL 4.1 and later, MySQL also provides a way to connect to shared memory in the configuration file.

Add-- shared-memory. If you want to use shared memory, the Mysql client must also use the-protocol=memory option when connecting.

You can set it at startup through the following parameters.

Mysqld.exe-enable-named-pipe

Mysqld.exe-shared-memory

In addition to setting parameters at startup, you can also set them by modifying the MY.INI file. Let's take a look at the branch function handle_connections_methods that selects the connection method in:

Handle_connections_methods ()

{

If (hPipe! = INVALID_HANDLE_VALUE)

{

Handler_count++

If (& pthread_create & hThread,&connection_attrib)

Handle_connections_namedpipes, 0))

{

Sql_print_warning ("Can't create thread to handle named pipes")

Handler_count--

}

}

If (have_tcpip & &! opt_disable_networking)

{

Handler_count++

If (& pthread_create & hThread,&connection_attrib)

Handle_connections_sockets, 0))

{

Sql_print_warning ("Can't create thread to handle TCP/IP")

Handler_count--

}

}

If (opt_enable_shared_memory)

{

Handler_count++

If (& pthread_create & hThread,&connection_attrib)

Handle_connections_shared_memory, 0))

{

Sql_print_warning ("Can't create thread to handle shared memory")

Handler_count--

}

}

}

Since we don't know much about the communication mode between namepipe and memory share, we only study the communication mode of socket here. As you can see from the code, handle_connections_sockets is the setting of socket, so let's take a look at it.

4.socket management to create a new thread socket management is actually relatively simple, directly giving its pseudo code:

Handle_connections_sockets

{

Select; / / Monitor socket file descriptor

New_socket = accept;// handles incoming client connections

Thd = new THD; create the THD class

Vio_tmp = vio_new (new_socket,VIO_TYPE_TCPIP, 0); / / initialize the VIO structure

My_net_init (& thd- > net, vio_tmp); / / initialize the net structure of thd

Create_new_thread (thd); / / create a new thread for this connection. If it is in single-thread mode, a new thread will not be created.

}

First, the select function monitors the socket port. If there is a connection, it accepts the connection from the client through the accept function, then creates a new THD class, sets all the connection parameters to the parameters of the THD class, and finally calls the create_new_thread function, which is the key point. Let's go into this function and see what we've done.

Create_new_thread

{

The number of + + connection_count;// global connections is self-increasing

Thread_count++; / / the number of global threads is self-increasing

Thread_scheduler.add_connection (thd); / / actually create a thread

}

So easy, first set the number of global connections + 1, the number of global threads + 1, and then call the add_connection function, which we set up the connection in the first step above

One of the parameters set in one_thread_scheduler and one_thread_per_connection_scheduler in the number of threads. The difference between the two is whether the

A new thread to handle the incoming connection. One_thread_scheduler is single-threaded, and there are no new threads. We focus on one_thread_per_connection_scheduler, whose add_connection function is create_thread_to_handle_connection:

Create_thread_to_handle_connection (THD * thd)

{

Thread_created++

Threads.append (thd); / / the number of threads created is incremented and added to the threads linked list

Pthread_create (& thd- > real_id,&connection_attrib

Handle_one_connection

(void*) thd); / / this is where the thread is really created, and the function is handle_one_connection

}

It can be seen that the last call to the pthread_create function, this function is to create a new thread, the new thread processing function is handle_one_connection.

5. New threading process

The new threading function is handle_one_connection, where a new connection is processed separately by a newly created thread. Let's take a look at one of them

How to deal with it.

Handle_one_connection (void * arg)

{

For (;;)

{

Lex_start (thd); / / initialize the lexical analysis structure

Login_connection (thd); / / user authentication, failure and error report

Prepare_new_connection_state (THD* thd); / / Initialize THD to handle queries

While (! net- > error & & net- > vio! = 0 & & / Loop processing command

! (thd- > killed = = THD::KILL_CONNECTION)

{

If (do_command (thd))

Break; / / processing failed to jump out

}

End_connection (thd); / / close the connection

Close_connection (thd, 0,1)

Thread_scheduler.end_thread (thd,1); / / end thread

Return 0

}

}

First, the structure of lexical analysis is initialized, and then the user is authenticated. After successful authentication, the commands sent by the client are executed through the do_command loop.

6. Summary

The flow of the whole connection manager is very clear, single-threaded connections are rarely used, mostly in a multi-threaded way. Thread buffering is actually involved in multithreaded connections.

The concept of a pool, that is, if a connection is disconnected, the created thread will not be destroyed, but will be placed in the buffer pool and wait for the next new connection to arrive.

The buffer pool looks for free threads, and if so, use them directly, and if not, create new threads to manage the connection. These contents belong to Thread Manage.

About what the role of Connection Manager in Mysql is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report