Guest User

Untitled

a guest
May 17th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.90 KB | None | 0 0
  1. ''' <summary>
  2.     ''' This function generates and returns the values for a My.ini file for a LabTech server.
  3.     ''' This function should be called during Server Creation or from within the UI and followed by writing the file restarting the MySQL server.
  4.     ''' Each variable is done so for optimal performance using mathematical structures.
  5.     ''' Comments before each variable explain the optimal math and pertinent information.
  6.     ''' </summary>
  7.     ''' <param name="MemoryInMB"> The value in MB of Memory allocated to the server.</param>
  8.     ''' <remarks>
  9.     ''' </remarks>
  10.     Private Shared Function generateIniWithoutPaths(ByVal MemoryInMB As Integer) As String
  11.         Dim temp As String = Nothing
  12.         'Populate our Name Value Collections once so that they're not repopulating for each action below.
  13.         Dim serverVariables As NameValueCollection = LTSettings.MySQLInfo.ServerVariables
  14.         Dim serverStatus As NameValueCollection = LTSettings.MySQLInfo.ServerStatus
  15.         Dim innodbStatus As NameValueCollection = LTSettings.MySQLInfo.InnoDBEngineStatus
  16.  
  17.         'Non Memory related or partial memory related variables.
  18.         Dim innodblogfilesize As Integer = CInt(CInt(serverVariables.GetValues("innodb_log_file_size")(0)) / 1048576)
  19.         Dim basedir As String = """" & CStr(serverVariables.GetValues("basedir")(0)) & """"
  20.         Dim datadir As String = """" & Replace(CStr(serverVariables.GetValues("datadir")(0)), "/", "\") & """"
  21.         Dim innodbdatahomedir As String = CStr(serverVariables.GetValues("innodb_data_home_dir")(0))
  22.  
  23.         ' Overhead memory buffers. These are fixed buffers that we don't change in size.
  24.         ' With great power, and large memory buffers, comes great responsibility.
  25.         ' Be careful when making changes to these variables.
  26.         Dim sbuffersize As Integer = 2
  27.         Dim jbuffersize As Integer = 4
  28.         Dim rbuffersize As Integer = 4
  29.         Dim rrbuffersize As Integer = 1
  30.         Dim querycachesize As Integer = 64
  31.         Dim innodbamempoolsize As Integer = 20
  32.         Dim innodblogbuffersize As Integer = CInt(innodblogfilesize / 2)
  33.  
  34.         Dim MemOverheadTotals As Integer = sbuffersize + _
  35.                                             jbuffersize + _
  36.                                             rbuffersize + _
  37.                                             rrbuffersize + _
  38.                                             querycachesize + _
  39.                                             innodbamempoolsize + _
  40.                                             innodblogbuffersize
  41.  
  42.         ' Calculate the memory left for dynamic variables, less the overhead.
  43.         Dim MemLessOverhead As Integer = MemoryInMB - MemOverheadTotals
  44.  
  45.         ' Dynamic memory buffers. These change based upon amount of resources available to the system.
  46.         Dim msbuffersize As Integer = CInt(Math.Floor(MemLessOverhead * 0.05))
  47.         Dim kbuffersize As Integer = CInt(Math.Floor(MemLessOverhead * 0.1))
  48.         Dim innodbbufferpoolsize As Integer = CInt(Math.Round(MemLessOverhead * 0.7))
  49.         Dim ttablesize As Integer = CInt(Math.Floor(MemLessOverhead * 0.15))
  50.  
  51.         'for max_connections, always increment (never decrease) this number. The formula below ensures that.
  52.         ' Max(Current maximum connections, max((max used connections + 10), (max used connections * 1.1))
  53.         Dim max_connections As Integer = Math.Max(CInt(serverVariables.GetValues("max_connections")(0)), (Math.Max((CInt(serverStatus.GetValues("max_used_connections")(0)) + 10), (CInt(CInt(serverStatus.GetValues("max_used_connections")(0)) * 1.1)))))
  54.  
  55.         LTLogger.WriteLine("Memory for MySQL: " & MemoryInMB & "MB")
  56.  
  57.         temp = "[client]" & vbCrLf
  58.         temp &= "port=3306" & vbCrLf
  59.         temp &= "default-character-set=utf8" & vbCrLf & vbCrLf
  60.  
  61.         temp &= "[mysql]" & vbCrLf
  62.         temp &= "default-character-set=utf8" & vbCrLf & vbCrLf
  63.  
  64.         temp &= "[mysqld]" & vbCrLf
  65.         temp &= "port=3306" & vbCrLf
  66.         temp &= "character-set-server=utf8" & vbCrLf
  67.         temp &= "default-storage-engine=INNODB" & vbCrLf & vbCrLf
  68.  
  69.         temp &= "# It is not recommended to change these settings." & vbCrLf
  70.         temp &= "skip-external-locking" & vbCrLf
  71.         temp &= "transaction-isolation=REPEATABLE-READ" & vbCrLf
  72.         temp &= "wait_timeout=900" & vbCrLf
  73.         temp &= "interactive_timeout=7200" & vbCrLf
  74.         temp &= "max_allowed_packet=1G" & vbCrLf
  75.         temp &= "myisam_max_sort_file_size=10G" & vbCrLf
  76.         temp &= "innodb_file_per_table" & vbCrLf
  77.         temp &= "innodb_flush_log_at_trx_commit=2" & vbCrLf
  78.         temp &= "innodb_log_files_in_group=2" & vbCrLf
  79.         temp &= "innodb_thread_concurrency=0" & vbCrLf
  80.         temp &= "innodb_log_file_size=" & innodblogfilesize & "M" & vbCrLf
  81.         temp &= "innodb_log_buffer_size=" & innodblogbuffersize & "M" & vbCrLf
  82.         temp &= "tmp_table_size=" & ttablesize & "M" & vbCrLf & vbCrLf
  83.  
  84.         temp &= "## These settings total to equal MySQL's memory usage. Use caution when modifying." & vbCrLf
  85.         temp &= "## Setting them too high can mean your MySQL server won't start." & vbCrLf
  86.         temp &= "key_buffer_size=" & kbuffersize & "M" & vbCrLf
  87.         temp &= "myisam_sort_buffer_size=" & msbuffersize & "M" & vbCrLf
  88.         temp &= "read_buffer_size=" & rbuffersize & "M" & vbCrLf
  89.         temp &= "read_rnd_buffer_size=" & rrbuffersize & "M" & vbCrLf
  90.         temp &= "sort_buffer_size=" & sbuffersize & "M" & vbCrLf
  91.         temp &= "join_buffer_size=" & jbuffersize & "M" & vbCrLf
  92.  
  93.         temp &= "innodb_additional_mem_pool_size=" & innodbamempoolsize & "M" & vbCrLf
  94.         temp &= "query_cache_size=" & querycachesize & "M" & vbCrLf & vbCrLf
  95.  
  96.         temp &= "# This setting is part of the memory total, but should be modified if performance is slower." & vbCrLf
  97.         temp &= "# The biggest performance gains you can get are by setting innodb_buffer_pool_size" & vbCrLf
  98.         temp &= "# equal to the size of your database." & vbCrLf
  99.         temp &= "#" & vbTab & "(as long as you have the free memory)" & vbCrLf
  100.         temp &= "innodb_buffer_pool_size=" & Math.Min(8192, innodbbufferpoolsize) & "M" & vbCrLf & vbCrLf
  101.  
  102.  
  103.         ' Done memory usage variables. Begin editable variables.
  104.         temp &= "# These variables should be increased if performance is slower." & vbCrLf
  105.         temp &= "max_connections=" & Math.Max(300, max_connections) & vbCrLf
  106.         temp &= "table_open_cache=" & (max_connections * 2) & vbCrLf
  107.         temp &= "thread_cache_size=32" & vbCrLf & vbCrLf
  108.  
  109.  
  110.         'MySQL version specific section.
  111.         If CDbl(Left(serverVariables.GetValues("Version")(0), 3)) > 5.1 Then
  112.             ' MySQL 5.5 and above.
  113.             temp &= "# These variables are for MySQL 5.5 and above only" & vbCrLf
  114.             temp &= "innodb_read_io_threads=32" & vbCrLf
  115.             temp &= "innodb_write_io_threads=24" & vbCrLf & vbCrLf
  116.             If Math.Round(MemoryInMB * 0.6) > 1024 Then
  117.                 temp &= "innodb_buffer_pool_instances=" & Math.Floor(innodbbufferpoolsize / 1024) & vbCrLf & vbCrLf
  118.             End If
  119.         ElseIf CDbl(Left(serverVariables.GetValues("Version")(0), 3)) <= 5.1 Then
  120.             temp &= "# These variables are for MySQL versions 5.1 and below only" & vbCrLf
  121.             temp &= "innodb_file_io_threads=32" & vbCrLf & vbCrLf
  122.         Else
  123.             Throw New InvalidOperationException("The version was unknown. Please run this on the LabTech server.")
  124.         End If
  125.  
  126.         ' Directories for the Install - Use whatever was already in place.
  127.         temp &= "# These are the paths to where the MySQL data is stored." & vbCrLf
  128.         temp &= "basedir = " & basedir & vbCrLf
  129.         temp &= "datadir = " & datadir & vbCrLf
  130.         If Not innodbdatahomedir = "" And innodbdatahomedir IsNot Nothing Then
  131.             temp &= "innodb_data_home_dir= """ & innodbdatahomedir & """" & vbCrLf
  132.         End If
  133.  
  134.  
  135.         Return temp
  136.  
  137.     End Function
Add Comment
Please, Sign In to add comment