当前位置:网站首页>Install MySQL in docker and modify my CNF profile

Install MySQL in docker and modify my CNF profile

2022-06-24 12:20:00 Full stack development dream

Preface

Previous experience MySQL After master-slave replication , Find out database Is very slow to read and write , And it happens all the time MySQL Too few connections , A situation that causes the entire database to crash . By modifying the MySQL Medium my.cnf Profile parameters , In order to achieve MySQL Rational use of the maximum performance of relational database .

This article is divided into three aspects to tell :

One 、Docker install MySQL

 see mysql Mirror image  	
    sudo docker search mysql
 Pull mysql 	
    sudo docker pull mysql  
 # Default to the latest version ( Customizable 5.7)  
 Create a container , And test the , Reference link 
  	sudo docker run --name mysql -e MYSQL\_ROOT\_PASSWORD=123456 -p 3306:3306 -d mysql

Two 、 Connect MySQL

docker Into mysql Container method  	
    1)sudo docker exec -it  Container name  bash
    2)mysql -uroot -p
    3)use mysql
    4)select host, user from user; see root user 
     Judge root Is there a , If there are two, delete them first delete from user where host="%" and user="root";        
     Revise  update user set host = '%' where user = 'root';
     Refresh the permissions  FLUSH PRIVILEGES;    
     Grant remote access  alter user 'root'@'%' identified with mysql\_native\_password by '123456';

3、 ... and 、 Modify configuration file parameters

1. Once the download is complete , Input docker ps Look at the mirror image

2. Start the container :docker run -p 3307:3306 --name mysql -e MYSQL\_ROOT\_PASSWORD=root -d mysql,docker ps You can view the status

3. Get into MySQL In container :docker exec -it Container name /bin/bash(PS: In fact, this one follows Linux It's the same as modifying a file , There are many ways , I use the most stupid one .) Replace local file with container file

4. What I want to change here is MySQL Inside my.cnf file . You can use it first ls Look at the catalog file (PS: ls Command to display the contents of the current directory )

5.tail my.cnfPS:tail The command can be used to view the contents of a file )

6. function vim Code

    vim /etc/mysql/my.cnf

7. Run the above vim After the code, you may find that it did not execute successfully , because docker There is no... In the container vim Editor , So you have to install it yourself vim Editor (vim Details of installation methods can be found at the end of the article appendix

8. Get into my.cnf file

9. Add or modify the configuration according to your own needs , My modified configuration is as follows ( For reference )

10. Enclosed MySQL Some key parameters of database performance optimization

Key parameter one :back\_log

requirement MySQL The number of connections you can have . Be the main MySQL Threads get a lot of connection requests in a short time , It works , Then the main thread takes some time ( Short as it is ) Check the connection and start a new thread .

back_log The value is in MySQL How many requests can be stored in the stack in a short period of time before you temporarily stop answering new requests . Only if you expect a lot of connections in a short time , You need to add it , let me put it another way , It's worth the coming TCP/IP The size of the listening queue for the connection . Your operating system has its own limit on the size of this queue . Try to set back_log Higher than the limit of your operating system will be invalid .

When you look at your host process list , Find a lot 264084 | unauthenticated user | xxx.xxx.xxx.xxx | NULL | Connect | NULL | login | NULL To connect to the process , It's going to increase back_log The value of the . The default value is 50, I changed it to 500.

Key parameter 2 :interactive\_timeout

The number of seconds the server waits for action on an interactive connection before shutting it down . An interactive client is defined as being right mysql_real_connect() Use CLIENT_INTERACTIVE Option customers . The default value is 28800, I changed it to 7200.

Key parameter three :key\_buffer\_size

The index block is buffered and shared by all threads .key_buffer_size Is the buffer size for the index block , Add it to get better indexes ( For all read and multiply Write ), To you Can afford that much . If you make it too big , The system will start to change pages and really slow down . The default value is 8388600(8M), my MySQL The main engine has 2GB Memory , So I changed it to 402649088(400MB).

Key parameter four :max\_connections

The number of customers allowed at the same time . Increase this value by mysqld Number of file descriptors required . This number should be increased , otherwise , You will often see Too many connections error . The default value is 100, I changed it to 1024 .

Key parameter five :record\_buffer

Each thread that performs a sequential scan allocates a buffer of this size for each table it scans . If you do a lot of sequential scanning , You may want to increase the value . The default value is 131072(128K), I changed it to 16773120 (16M)

Key parameter six :sort\_buffer

Each thread that needs to be sorted allocates a buffer of that size . Increase this value to accelerate ORDER BY or GROUP BY operation . The default value is 2097144(2M), I changed it to 16777208 (16M).

Key parameter 7 :table\_cache

Number of open tables for all threads . Increasing this value increases mysqld Number of file descriptors required .MySQL For each uniquely open table, you need 2 File descriptors . The default value is 64, I changed it to 512.

Key parameter eight :thread\_cache\_size

The number of threads that can be reused in . If there is , The new thread gets... From the cache , When disconnected, if there is space , The customer's line is in the cache . If there are many new threads , In order to improve the Performance is acceptable With this variable value . By comparison Connections and Threads_created Variables of state , You can see the effect of this variable . I set it to 80.

Key parameter 9 :wait\_timeout

The number of seconds the server waits for action on a connection before shutting it down . The default value is 28800, I changed it to 7200.

notes : Parameters can be adjusted by modifying /etc/my.cnf File and restart MySQL Realization . This is a more cautious job , The above results are just my opinions , According to the hardware of your own host ( Especially the memory size ) Further modification .

11. Use after modification :wq Preservation

12. Reuse tail Check whether the file is modified correctly

tail /etc/mysql/my.cnf 

13. After the configuration file is modified successfully , You can use it to your heart's content MySQL La !(PS: Because of the limitation of the original design intention of the relational database , In massive data processing and multi table function query, we will encounter I/O Performance bottleneck . So it has distributed 、 High performance 、 High reliability and other characteristics of non relational database (NoSQL) Gradually understood and used by people , It makes up for the congenital deficiency of relational database performance , There's a huge advantage in storing unstructured data . Main stream NoSQL The database has :Redis、MongoDB and HBase etc. )

appendix

vim Download and install the editor

In the use of docker When the container , Sometimes it's not installed inside vim, function vim The prompt says :vim: command not found, This is the time to install vim, When you run the following installation command :

apt-get install vim 

Error Wrong presentation :

Reading package lists… Done 
Building dependency tree 
Reading state information… Done
E: Unable to locate package vim 

according to apt Command to download and install software , We need to be right about apt updated !!!

apt-get update 

The above command is to synchronize  /etc/apt/sources.list and /etc/apt/sources.list.d Index of the source listed in , In this way, we can get the latest software package .

Wait for the update to complete before running the command :

apt-get install vim 
vim /etc/mysql/mysql.conf.d/mysqld.cnf 

1.apt-get install vim

2.apt-get update

3. complete vim Editor installation

原网站

版权声明
本文为[Full stack development dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210603115759922v.html