-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLConfig.cpp
More file actions
1249 lines (1112 loc) · 40.9 KB
/
MySQLConfig.cpp
File metadata and controls
1249 lines (1112 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MySQLConfig.h"
#include "QString"
#include "QDialog"
#include "QDir"
#include "QFileDialog"
#include "QMessageBox"
#include "Utility.h"
#include "qinputdbpassword.h"
#include "QWaitCursor.h"
#include <QMenu>
QString g_strServerType[] = { QObject::tr("Master Server"),QObject::tr("Slave Server") };
QString g_strLogMode[] = { QObject::tr("FILE"),QObject::tr("TABLE"), QObject::tr("FILE,TABLE") };
MySQLConfig::MySQLConfig(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
//setWindowIcon(QIcon(QString::fromUtf8(":/MySQLConfig/dialog-ok-2.ico")));
IcoConnect[0].addFile(QString::fromUtf8(":/MySQLConfig/close.png"), QSize(), QIcon::Normal);
IcoConnect[1].addFile(QString::fromUtf8(":/MySQLConfig/open.png"), QSize(), QIcon::Normal);
pWidgetPtrServer = new vector<QWidget*>(
{
ui.lineEdit_ServerID,
ui.checkBox_Logerror,
//ui.lineEdit_Logerror,
ui.comboBox_ServerType,
ui.lineEdit_Logbin,
//ui.comboBox_LogMode,
ui.checkBox_LongQueryTime,
//ui.lineEdit_LongQueryTime,
ui.lineEdit_ServerPort,
ui.checkBox_ExternAccess,
//ui.lineEdit_AccessPassword,
ui.checkBox_Generallog,
//ui.lineEdit_Generallog,
ui.checkBox_SlowQuerylog,
//ui.lineEdit_SlowQuerylog,
ui.checkBox_Expiredlogdays,
//ui.lineEdit_Expiredlogdays,
});
pWidgetPtrMaster = new vector<QWidget*>(
{
ui.comboBox_SourceDB,
ui.comboBox_IgnoredDB,
ui.lineEdit_ReplicationAccount,
ui.lineEdit_ReplicationPassword,
ui.lineEdit_LogbinFile,
ui.lineEdit_LogbinPosition,
ui.listWidget_SlaveHosts,
//ui.pushButton_ApplySettings
});
pWidgetPtrSlave = new vector<QWidget*>(
{
//ui.lineEdit_MasterlogFile,
//ui.lineEdit_MasterlogFilePos,
ui.lineEdit_MasterHost,
ui.lineEdit_MasterHost_Port,
ui.lineEdit_MasterAccount,
ui.lineEdit_MasterPassword,
ui.lineEdit_ReplicationAccount_Slave,
ui.lineEdit_ReplicationPassword_Slave,
//ui.checkBox_IORunning,
//ui.checkBox_SQLRunning,
ui.lineEdit_Relaylog,
ui.lineEdit_RelaylogIndex,
//ui.pushButton_ApplySettings
});
int nItemCount = ui.comboBox_LogMode->count();
for (int nIndex = 1; nIndex < nItemCount; nIndex++)
{
ui.comboBox_LogMode->setItemData(nIndex, QVariant(0), Qt::UserRole - 1);
ui.comboBox_LogMode->setItemData(nIndex, QBrush(QColor(192, 192, 192)), Qt::BackgroundRole);
ui.comboBox_LogMode->setItemData(nIndex, QBrush(QColor(Qt::black)), Qt::ForegroundRole);
}
//connect(ui.comboBox_SourceDB, SIGNAL(hidingPopup()), this, SLOT(OnComboboxSourceDBHidePopup()));
// connect(ui.pushButtonStart, SIGNAL(clicked()), this, SLOT(OnStart()));
}
void MySQLConfig::OnStart()
{
QWaitCursor Wait;
std::string strService = "MySQL.T";
CWinService Service;
SERVICE_STATUS_PROCESS ssStatus;
char *szServiceStatus[] = {
"",
"Stopped ",
"Start_Pending",
"Stop_Pending",
"Running",
"Continue_Pending",
"Pause_Pending",
"Paused"
};
int nResult = Service.Stop(strService.c_str(), ssStatus);
qDebug("Stop Result = %d\tServiceStatus = %s.\n", nResult, szServiceStatus[ssStatus.dwCurrentState]);
nResult = Service.Start(strService.c_str(), ssStatus);
qDebug("Start Result = %d\tServiceStatus = %s.\n", nResult, szServiceStatus[ssStatus.dwCurrentState]);
}
void MySQLConfig::OnComboboxSourceDBHidePopup()
{
//QStandardItemModel* pModel = (QStandardItemModel *)ui.comboBox_IgnoredDB->model();
//QStandardItem* pItem = pModel->item(2);
//pItem->setFlags(0);
//auto& Index = pModel->index(2, 0);
//
//pModel->setData(Index, QVariant(0), Qt::UserRole - 1);
//pModel->setData(Index, QBrush(QColor(192, 192, 192)), Qt::BackgroundRole);
//pModel->setData(Index, QBrush(QColor(Qt::black)), Qt::ForegroundRole);
}
MySQLConfig::~MySQLConfig()
{
if (pMySQLIni)
delete pMySQLIni;
if (pMySQLSettings)
delete pMySQLSettings;
if (pWidgetPtrServer)
delete pWidgetPtrServer;
if (pWidgetPtrMaster)
delete pWidgetPtrMaster;
if (pWidgetPtrSlave)
delete pWidgetPtrSlave;
}
bool MySQLConfig::GetMySQLService(std::string strMySQL_Path, std::string& strMySQLService, ServiceStatus &nServiceStatus)
{
CWinService ServiceMgr;
ServiceInformationArray SvrConfigArray;
ServiceMgr.GetAllServiceInformation(SvrConfigArray);
QString strMySQLD_Path = QString("%1\\bin\\Mysqld.exe").arg(strMySQL_Path.c_str());
int nIndex = 0;
auto itFind = find_if(SvrConfigArray.begin(), SvrConfigArray.end(), [strMySQLD_Path,nIndex](ServiceInformationPtr p) mutable
{
QString strBinPath = p->ServiceConfig.lpBinaryPathName;
return strBinPath.contains(strMySQLD_Path, Qt::CaseInsensitive);
});
// if the MySQL Service is installed and it's Prescess path equal to m_strMySQLPath,and it is running ,then stop it !
if (itFind != SvrConfigArray.end())
{
strMySQLService = (*itFind)->EnumServiceStatus.lpServiceName;
nServiceStatus = (ServiceStatus)(*itFind)->EnumServiceStatus.ServiceStatus.dwCurrentState;
return true;
}
else
return false;
}
void MySQLConfig::on_pushButton_Browse_clicked()
{
QDir dir;
QString strPath = QFileDialog::getExistingDirectory(this, tr("Select the Directory of MySQL"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
strPath.replace("/", "\\");
QString strINI = QString("%1\\My.ini").arg(strPath);
if (!QFileInfo::exists(strINI))
{
QMessageBox::critical(nullptr, tr("Error"), tr("Can't find My.ini in the selected directory!"), QMessageBox::Abort);
return;
}
strMySQLPath = strPath;
ui.lineEdit_InstalledPath->setText(strPath);
/*QWaitCursor Wait;*/
LoadSettings(strPath);
ui.pushButton_Connect->setEnabled(true);
}
bool MySQLConfig::TestMySQLService(QString& strService, ServiceStatus& nServiceStatus)
{
unsigned short nMySQLPort = ui.lineEdit_ServerPort->text().toShort();
strMySQLPath = ui.lineEdit_InstalledPath->text();
std::string strMySQLService;
QString strMessage;
bool bFoundMySQLService = GetMySQLService(strMySQLPath.toStdString(), strMySQLService, nServiceStatus);
if (!bFoundMySQLService)
{
QMessageBox::critical(nullptr, tr("Error"), tr("MySQL Service is not installed,Please install it!"), QMessageBox::Ok);
return false;
}
QTcpSocket TcpClient;
TcpClient.connectToHost("127.0.0.1", nMySQLPort);
if (!TcpClient.waitForConnected(100))
{// the MySql Service not start
CWinService Service;
SERVICE_STATUS_PROCESS ssStatus;
if (!Service.Start(strMySQLService.c_str(), ssStatus) ||
ssStatus.dwCurrentState == Running)
{
strMessage = QString(tr("Failed in starting %1 Service,Please check the service setting!")).arg(strMySQLService.c_str());
QMessageBox::critical(nullptr, tr("Error"), strMessage, QMessageBox::Ok);
return false;
}
}
strService = strMySQLService.c_str();
return true;
}
void MySQLConfig::on_pushButton_Connect_clicked()
{
try
{
QString strService;
ServiceStatus nServiceStatus;
if (!TestMySQLService(strService, nServiceStatus))
return;
unsigned short nMySQLPort = ui.lineEdit_ServerPort->text().toShort();
strMySQLPath = ui.lineEdit_InstalledPath->text();
CMySQLAgent DBConnector;
QInputDBPassword InpudDlg(strMySQLPath, nMySQLPort, this);
if (InpudDlg.exec() == QDialog::Accepted)
{
QWaitCursor Wait;
strDBPassword = InpudDlg.GetPassword();
int nResult = DBConnector.Connect("127.0.0.1", "root", strDBPassword.toStdString().c_str()); // Access denied,may Account or password error
if (!nResult)
{
ui.pushButton_Connect->setIcon(IcoConnect[1]);
for each (auto var in *pWidgetPtrServer)
var->setEnabled(true);
// get some information of mysql;
CMyResult res = DBConnector.Query("show databases");
if (res.RowCount())
{
ui.comboBox_SourceDB->clear();
ui.comboBox_IgnoredDB->clear();
do
{
char* pDatabase = res["Database"];
ui.comboBox_SourceDB->AddItem(pDatabase);
ui.comboBox_IgnoredDB->AddItem(pDatabase);
} while (++res);
}
res = DBConnector.Query("show variables like 'server_id'");
if (res.RowCount())
{
char* pVarName = res["Variable_name"];
if (strcmp(pVarName, "server_id") != 0)
{
// set global server_id=2;
char* pServerID = res["Value"];
if (pServerID)
ui.lineEdit_ServerID->setText(pServerID);
}
}
res = DBConnector.Query("show master status");
if (res.RowCount())
{
char* pLogbin = res["File"];
int nLogPos = res["Position"];
char* pDBList = res["Binlog_Do_DB"];
char* pIgnoreDBList = res["Binlog_Ignore_DB"];
ui.lineEdit_LogbinFile->setText(pLogbin);
ui.lineEdit_LogbinPosition->setText(QString("%1").arg(nLogPos));
if (strlen(pDBList))
{
QStringList Dblist = QString(pDBList).split(',');
for each (auto var in Dblist)
ui.comboBox_SourceDB->SetItemCheck(var);
}
if (strlen(pIgnoreDBList))
{
QStringList IgnoreDBList = QString(pIgnoreDBList).split(',');
for each (auto var in IgnoreDBList)
ui.comboBox_SourceDB->SetItemCheck(var);
}
}
res = DBConnector.Query("show slave status");
if (res.RowCount())
{
char* pMasterHost = res["Master_Host"];
char* pMasterUser = res["Master_User"];
char* pMasterPort = res["Master_Port"];
char* pMasterlogFile = res["Master_Log_File"];
char* pReadMasterPos = res["Read_Master_Log_Pos"];
char* pRelaylogFile = res["Relay_Log_File"];
//int nRelaylogPos = res["Relay_log_Pos"];
char* pRelayMasterLogFile = res["Relay_Master_Log_File"];
char* pSlaveIORunning = res["Slave_IO_Running"];
char* pSlaveSQLRunning = res["Slave_SQL_Running"];
ui.lineEdit_Relaylog->setText(pMasterHost);
if (pMasterHost)
ui.lineEdit_MasterHost->setText(pMasterHost);
if (pMasterPort)
ui.lineEdit_MasterHost_Port->setText(pMasterPort);
if (pMasterUser)
ui.lineEdit_ReplicationAccount_Slave->setText(pMasterUser);
/* if (pMasterlogFile)
ui.lineEdit_MasterlogFile->setText(pMasterlogFile);
if (pReadMasterPos)
ui.lineEdit_MasterlogFilePos->setText(pReadMasterPos);*/
if (pRelaylogFile)
ui.lineEdit_Relaylog->setText(pRelaylogFile);
}
res = DBConnector.Query("show variables like '%%relay_log_index%%'");
if (res.RowCount())
{
char* VarName = res["Variable_name"];
char* pRelaylogIndex = res["Value"];
if (strcmp(VarName,"relay_log_index") != 0 && pRelaylogIndex)
{
ui.lineEdit_RelaylogIndex->setText(pRelaylogIndex);
}
}
}
else
{
ui.pushButton_Connect->setIcon(IcoConnect[0]);
QString strError = DBConnector.GetErrorMsg();
QMessageBox::critical(nullptr, tr("Error"), strError, QMessageBox::Abort);
return;
}
}
}
catch (CMySQLException& e)
{
QMessageBox::critical(nullptr, tr("Error"), e.what(), QMessageBox::Abort);
}
catch (std::exception& e)
{
QMessageBox::critical(nullptr, tr("Error"), e.what(), QMessageBox::Abort);
}
}
bool MySQLConfig::LoadSettings(QString strMySQLPath)
{
QSettings MySQLSettings(QString("%1\\My.ini").arg(strMySQLPath), QSettings::IniFormat);
ui.lineEdit_Logbin->setEnabled(false);
ui.lineEdit_Generallog->setEnabled(false);
ui.checkBox_Generallog->setChecked(false);
ui.checkBox_Logerror->setChecked(false);
ui.lineEdit_Logerror->setEnabled(false);
ui.lineEdit_SlowQuerylog->setEnabled(false);
ui.checkBox_SlowQuerylog->setChecked(false);
ui.lineEdit_Expiredlogdays->setEnabled(false);
ui.checkBox_Expiredlogdays->setChecked(false);
MySQLSettings.beginGroup("mysqld");
QStringList keyList = MySQLSettings.allKeys();
foreach (QString strKey,keyList)
{
TraceMsgA("Key = %s\tValue = %s\n", strKey.toStdString().c_str(), MySQLSettings.value(strKey).toString().toStdString().c_str());
QVariant varValue;
if (strKey =="server-id")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_ServerID->setText(varValue.toString());
}
varValue = MySQLSettings.value("port");
ui.lineEdit_ServerPort->setText(varValue.toString());
if (strKey =="log-output")
{
varValue = MySQLSettings.value(strKey);
int nLogmode = ui.comboBox_LogMode->findText(varValue.toString());
ui.comboBox_LogMode->setCurrentIndex(nLogmode);
}
//ui.comboBox_LogMode->setEnabled(false);
if (strKey =="log-bin")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_Logbin->setText(varValue.toString());
ui.lineEdit_Logbin->setEnabled(true);
}
if (strKey =="general-log")
{
varValue = MySQLSettings.value(strKey);
bool bGeneralLog = varValue.toBool();
ui.lineEdit_Generallog->setEnabled(bGeneralLog);
ui.checkBox_Generallog->setChecked(bGeneralLog);
}
if (strKey =="general_log_file")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_Generallog->setText(varValue.toString());
}
if (strKey =="log-error")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_Logerror->setText(varValue.toString());
ui.checkBox_Logerror->setChecked(true);
ui.lineEdit_Logerror->setEnabled(true);
}
if (strKey =="long_query_time")
{
ui.checkBox_LongQueryTime->setChecked(true);
ui.lineEdit_LongQueryTime->setEnabled(true);
varValue = MySQLSettings.value(strKey);
ui.lineEdit_LongQueryTime->setText(varValue.toString());
}
if (strKey =="slow-query-log")
{
varValue = MySQLSettings.value(strKey);
bool bSlowQueylog = varValue.toBool();
ui.lineEdit_SlowQuerylog->setEnabled(bSlowQueylog);
ui.checkBox_SlowQuerylog->setChecked(bSlowQueylog);
}
if (strKey =="slow_query_log_file")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_SlowQuerylog->setText(varValue.toString());
}
if (strKey =="expire-logs-days")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_Expiredlogdays->setText(varValue.toString());
ui.lineEdit_Expiredlogdays->setEnabled(true);
ui.checkBox_Expiredlogdays->setChecked(true);
}
if (strKey == "relay-log")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_Relaylog->setText(varValue.toString());
}
if (strKey == "relay-log-index")
{
varValue = MySQLSettings.value(strKey);
ui.lineEdit_RelaylogIndex->setText(varValue.toString());
}
}
MySQLSettings.endGroup();
return true;
}
bool MySQLConfig::SaveSettings(QString strInstalledPath,QString &strMessage)
{
QString strIniFile = QString("%1\\My.ini").arg(strInstalledPath);
if (pMySQLIni)
delete pMySQLIni;
pMySQLIni = new CIniFile(strIniFile.toStdString());
if (!pMySQLIni)
{
strMessage = QString(tr("Failed to Load file '%s'.")).arg(strIniFile);
return false;
}
pMySQLIni->EnterSection("mysqld");
pMySQLIni->WriteKey( "port", nServerPort);
pMySQLIni->WriteKey( "server-id", nServerID);
if (strLogbinFile.size())
pMySQLIni->WriteKey("log-bin", strLogbinFile.toStdString());
else
pMySQLIni->EraseKey("log-bin");
if (bChecklogError)
pMySQLIni->WriteKey("log-error", strLogError.toStdString());
pMySQLIni->WriteKey("log-output", strLogMode.toStdString());
pMySQLIni->WriteKey("general-log", bCheckGenerallog ? 1 : 0);
if (strGeneralLog.size())
pMySQLIni->WriteKey("general_log_file", strGeneralLog.toStdString());
else
pMySQLIni->EraseKey("general_log_file");
pMySQLIni->WriteKey("slow-query-log", bCheckSlowQuerylog ? 1 : 0);
if (strSlowQuerylog.size())
pMySQLIni->WriteKey("slow_query_log_file", strSlowQuerylog.toStdString());
else
pMySQLIni->EraseKey("slow_query_log_file");
if (bChecklongQueryTime)
pMySQLIni->WriteKey("long_query_time", nLongQueryTime);
else
pMySQLIni->EraseKey("long_query_time");
if (bCheckExpiredlogDays)
pMySQLIni->WriteKey("expire-logs-days", nExpiredlogDays);
else
pMySQLIni->EraseKey("expire-logs-days");
if (nServerType == Server_Master)
{
pMySQLIni->WriteKey("binlog-do-db", strSourceDB.toStdString());
if (strIgorenDB.size())
pMySQLIni->WriteKey("binlog-ignore-db", strIgorenDB.toStdString());
}
else if (nServerType == Server_Slave)
{
pMySQLIni->WriteKey("relay-log-index", strRelayLogIndex.toStdString());
pMySQLIni->WriteKey("relay-log", strRelayLog.toStdString());
}
pMySQLIni->LeaveSection();
return true;
}
void MySQLConfig::SetDefaultMaster()
{
if (ui.lineEdit_Logbin->text().size() < 1)
ui.lineEdit_Logbin->setText("Master_bin");
ui.checkBox_Generallog->setChecked(false);
if (!ui.lineEdit_Generallog->text().size())
ui.lineEdit_Generallog->setText("General.log");
ui.checkBox_Logerror->setChecked(true);
if (!ui.lineEdit_Logerror->text().size())
ui.lineEdit_Logerror->setText("Master_Error.log");
ui.checkBox_LongQueryTime->setChecked(true);
ui.lineEdit_LongQueryTime->setText("30");
ui.checkBox_SlowQuerylog->setChecked(true);
if (!ui.lineEdit_SlowQuerylog->text().size())
ui.lineEdit_SlowQuerylog->setText("SlowQuery.log");
ui.checkBox_Expiredlogdays->setChecked(true);
if (!ui.lineEdit_Expiredlogdays->text().size())
ui.lineEdit_Expiredlogdays->setText("30");
ui.checkBox_ExternAccess->setChecked(true);
ui.lineEdit_AccessPassword->setText("Mago&Zpmc@2020");
ui.lineEdit_ReplicationAccount->setText("SlaveHost");
ui.lineEdit_ReplicationPassword->setText("Mago&Zpmc@2020");
}
void MySQLConfig::SetDefaultSlave()
{
if (ui.lineEdit_Logbin->text().size() < 1)
ui.lineEdit_Logbin->setText("Slave_bin");
ui.checkBox_Generallog->setChecked(false);
if (!ui.lineEdit_Generallog->text().size())
ui.lineEdit_Generallog->setText("General.log");
ui.checkBox_Logerror->setChecked(true);
if (ui.lineEdit_Logerror->text().size() < 1)
ui.lineEdit_Logerror->setText("Slave_Error.log");
ui.checkBox_LongQueryTime->setChecked(true);
ui.lineEdit_LongQueryTime->setText("30");
ui.checkBox_SlowQuerylog->setChecked(true);
if (ui.lineEdit_SlowQuerylog->text().size() < 1)
ui.lineEdit_SlowQuerylog->setText("SlowQuery.log");
ui.checkBox_Expiredlogdays->setChecked(true);
if (ui.lineEdit_Expiredlogdays->text().size() < 1)
ui.lineEdit_Expiredlogdays->setText("30");
ui.checkBox_ExternAccess->setChecked(true);
ui.lineEdit_AccessPassword->setText("Mago&Zpmc@2020");
ui.lineEdit_MasterAccount->setText("root");
if (ui.lineEdit_Relaylog->text().size() < 1)
ui.lineEdit_Relaylog->setText("Relay_log.bin");
if (ui.lineEdit_RelaylogIndex->text().size() < 1)
ui.lineEdit_RelaylogIndex->setText("Relay_log_Index.bin");
ui.lineEdit_ReplicationAccount_Slave->setText("SlaveHost");
ui.lineEdit_ReplicationPassword_Slave->setText("Mago&Zpmc@2020");
ui.lineEdit_MasterHost_Port->setText("3306");
}
void MySQLConfig::on_comboBox_ServerType_currentIndexChanged(int index)
{
nServerType = (ServerType)index;
ui.comboBox_LogMode->setCurrentIndex(0);
if (nServerType == Server_Master)
{
for each (auto var in *pWidgetPtrMaster)
var->setEnabled(true);
for each (auto var in *pWidgetPtrSlave)
var->setEnabled(false);
ui.pushButton_ApplySettings->setEnabled(true);
SetDefaultMaster();
ui.tabWidget->setCurrentIndex(0);
}
else if (nServerType == Server_Slave)
{
for each (auto var in *pWidgetPtrMaster)
var->setEnabled(false);
for each (auto var in *pWidgetPtrSlave)
var->setEnabled(true);
ui.pushButton_ApplySettings->setEnabled(true);
SetDefaultSlave();
ui.tabWidget->setCurrentIndex(1);
}
}
bool MySQLConfig::ConfigureMaster()
{
QString strMessage;
strSourceDB = ui.comboBox_SourceDB->getText();
if (!strSourceDB.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please select source database for Master Server!"), QMessageBox::Ok);
ui.comboBox_SourceDB->setFocus();
return false;
}
QStringList strSourceDBList = strSourceDB.split(",");
strIgorenDB = ui.comboBox_IgnoredDB->getText();
if (strIgorenDB.size())
{
QStringList strIgorenDBList = strIgorenDB.split(",");
for each (auto var in strIgorenDBList)
{
QStringList Result = strSourceDBList.filter(var, Qt::CaseInsensitive);
if (Result.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("There is some conficts between 'Source Database' and 'Ignored Database',please check them!"), QMessageBox::Abort);
return false;
}
}
}
strReplicationAccount = ui.lineEdit_ReplicationAccount->text();
if (!strReplicationAccount.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Replication Account for Master Server!"), QMessageBox::Ok);
ui.lineEdit_ReplicationAccount->setFocus();
return false;
}
strReplicationPassword = ui.lineEdit_ReplicationPassword->text();
if (!strReplicationPassword.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Replication Password for Master Server!"), QMessageBox::Ok);
ui.lineEdit_ReplicationPassword->setFocus();
return false;
}
int nCount = ui.listWidget_SlaveHosts->count();
if (!nCount)
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Add some slave hosts in Slave Host list!"), QMessageBox::Ok);
ui.listWidget_SlaveHosts->setFocus();
return false;
}
strSlaveHostList.clear();
for (int nRow = 0; nRow < nCount; nRow++)
{
QString strItem = ui.listWidget_SlaveHosts->item(nRow)->text();
if (!strItem.size() || !IsValidIPAddressA(strItem.toStdString().c_str()))
{
strMessage = QString(tr("The %d item in Slave Hosts list is invalid,Please input a Host IP Address as '172.16.20.100'!")).arg(nRow + 1);
QMessageBox::information(nullptr, tr("Information"), strMessage, QMessageBox::Ok);
ui.listWidget_SlaveHosts->setFocus();
return false;
}
strSlaveHostList.push_back(strItem);
}
if (!strSlaveHostList.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Add some slave hosts in Slave Host list!"), QMessageBox::Ok);
ui.listWidget_SlaveHosts->setFocus();
return false;
}
SaveSettings(strInstalledPath, strMessage);
QString strMySQLService;
ServiceStatus nServiceStatus;
QWaitCursor Wait;
TestMySQLService(strMySQLService, nServiceStatus);
CWinService Service;
SERVICE_STATUS_PROCESS ssStatus;
if (strMySQLService.size() )
{
int nResult = 0;
if (nServiceStatus == Running)
{
nResult = Service.Restart(strMySQLService.toStdString().c_str(), ssStatus);
}
else
{
nResult = Service.Start(strMySQLService.toStdString().c_str(), ssStatus);
}
char szError[1024] = { 0 };
if (nResult || ssStatus.dwCurrentState != Running)
{
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nResult,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR)szError,
1024,
NULL);
strMessage = QString(tr("Failed in restarting Service '%1':%2")).arg(strMySQLService).arg(szError);
Wait.Restore();
QMessageBox::critical(nullptr, tr("Information"), strMessage, QMessageBox::Abort);
return false;
}
}
try
{
CMySQLAgent DBConnector;
QString strSQL;
int nResult = DBConnector.Connect("127.0.0.1", "root", strDBPassword.toStdString().c_str());
if (nResult) // return a non zero value indicated a failure!
{
strMessage = DBConnector.GetErrorMsg();
QMessageBox::information(nullptr, tr("Information"), strMessage, QMessageBox::Ok);
return false;
}
if (bCheckExternAccess)
{
strSQL = QString("GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Mago&Zpmc@2020' WITH GRANT OPTION").arg(strDBPassword);
DBConnector.ExecuteSQLString(strSQL.toStdString().c_str());
DBConnector.ExecuteSQLString("flush privileges");
}
// grant all slave host replication privileges
for each (auto strHost in strSlaveHostList)
{
strSQL = QString("GRANT REPLICATION SLAVE ON *.* to '%1'@'%2' IDENTIFIED by '%3'").arg(strReplicationAccount).arg(strHost).arg(strReplicationPassword);
DBConnector.ExecuteSQLString(strSQL.toStdString().c_str());
DBConnector.ExecuteSQLString("flush privileges");
}
CMyResult res = DBConnector.Query("show master status");
if (res.RowCount())
{
char* pLogbin = res["File"];
int nLogPos = res["Position"];
char* pDBList = res["Binlog_Do_DB"];
char* pIgnoreDBList = res["Binlog_Ignore_DB"];
ui.lineEdit_LogbinFile->setText(pLogbin);
ui.lineEdit_LogbinPosition->setText(QString("%1").arg(nLogPos));
if (strlen(pDBList))
{
QStringList Dblist = QString(pDBList).split(',');
for each (auto var in Dblist)
ui.comboBox_SourceDB->SetItemCheck(var);
}
if (strlen(pIgnoreDBList))
{
QStringList IgnoreDBList = QString(pIgnoreDBList).split(',');
for each (auto var in IgnoreDBList)
ui.comboBox_SourceDB->SetItemCheck(var);
}
}
return true;
}
catch (std::exception& e)
{
QMessageBox::information(nullptr, tr("Exception"), e.what(), QMessageBox::Ok);
return false;
}
}
bool MySQLConfig::ConfigureSlave()
{
QString strMessage;
strMasterHost = ui.lineEdit_MasterHost->getIP();
if (!IsValidIPAddressA(strMasterHost.toStdString().c_str()))
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input a valid IP Address for Master Server Host!"), QMessageBox::Ok);
ui.lineEdit_MasterHost->setFocus();
return false;
}
nMasterPort = ui.lineEdit_MasterHost_Port->text().toInt();
if (nMasterPort <= 0 || nMasterPort >= 65535)
{
QMessageBox::information(nullptr, tr("Information"), tr("The Master Server Port must be a value between 1~65535,please input a valid value!"), QMessageBox::Ok);
ui.lineEdit_MasterHost->setFocus();
return false;
}
strMasterAccount = ui.lineEdit_MasterAccount->text();
if (!strMasterAccount.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Master Account for Slave Server!"), QMessageBox::Ok);
ui.lineEdit_MasterAccount->setFocus();
return false;
}
strMasterPassword = ui.lineEdit_MasterPassword->text();
if (!strMasterPassword.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Master Password for Slave Server!"), QMessageBox::Ok);
ui.lineEdit_MasterPassword->setFocus();
return false;
}
strReplicationAccount = ui.lineEdit_ReplicationAccount_Slave->text();
if (!strReplicationAccount.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Replication Account for Slave Server!"), QMessageBox::Ok);
ui.lineEdit_ReplicationAccount_Slave->setFocus();
return false;
}
strReplicationPassword = ui.lineEdit_ReplicationPassword_Slave->text();
if (!strReplicationAccount.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Replication Password for Slave Server!"), QMessageBox::Ok);
ui.lineEdit_ReplicationPassword_Slave->setFocus();
return false;
}
/*strMasterLogFile = ui.lineEdit_MasterlogFile->text();
if (!strMasterLogFile.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Master log File for Slave Server!"), QMessageBox::Ok);
ui.lineEdit_MasterlogFile->setFocus();
return false;
}*/
/*nMasterlogPos = ui.label_MasterlogFilePos->text().toInt();
if (!nMasterlogPos)
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Master log File Pos for Slave Server!"), QMessageBox::Ok);
ui.label_MasterlogFilePos->setFocus();
return false;
}*/
/*if ((!strMasterPassword.size() || strMasterPassword.size()) &&
(strMasterLogFile.size() || !nMasterlogPos))
{
QMessageBox::information(nullptr, tr("Information"), tr("You have neither input the account and password for the Master server nor input the Master log file and Master log Pos,the slave server can't be configuration cannot continue!"), QMessageBox::Ok);
return false;
}*/
strRelayLog = ui.lineEdit_Relaylog->text();
if (!strRelayLog.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Relay log for Slave Server!"), QMessageBox::Ok);
ui.lineEdit_Relaylog->setFocus();
return false;
}
QString strRelayLogIndex = ui.label_RelaylogIndex->text();
if (!strRelayLogIndex.size())
{
QMessageBox::information(nullptr, tr("Information"), tr("Please Input Relay log Index for Slave Server!"), QMessageBox::Ok);
ui.label_RelaylogIndex->setFocus();
return false;
}
SaveSettings(strInstalledPath, strMessage);
QString strMySQLService;
ServiceStatus nServiceStatus;
QWaitCursor Wait;
TestMySQLService(strMySQLService, nServiceStatus);
CWinService Service;
SERVICE_STATUS_PROCESS ssStatus;
if (strMySQLService.size())
{
int nResult = 0;
if (nServiceStatus == Running)
{
nResult = Service.Restart(strMySQLService.toStdString().c_str(), ssStatus);
}
else
{
nResult = Service.Start(strMySQLService.toStdString().c_str(), ssStatus);
}
char szError[1024] = { 0 };
if (nResult || ssStatus.dwCurrentState != Running)
{
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nResult,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR)szError,
1024,
NULL);
strMessage = QString(tr("Failed in restarting Service '%1':%2")).arg(strMySQLService).arg(szError);
Wait.Restore();
QMessageBox::critical(nullptr, tr("Information"), strMessage, QMessageBox::Abort);
return false;
}
}
try
{
CMySQLAgent MasterServer;
CMySQLAgent DBConnector;
QString strSQL;
int nResult = 0;
CMyResult res;
// the user has input account and password for master server ,then get the master log file and log pos by itsself
nResult = MasterServer.Connect(strMasterHost.toStdString().c_str(),
strMasterAccount.toStdString().c_str(),
strMasterPassword.toStdString().c_str(),
nullptr,
nMasterPort);
if (nResult) // return a non zero value indicated a failure!
{
QString strError = DBConnector.GetErrorMsg();
strMessage = QString("Failed in connect to Master Server %1@%2:%3(%4)").arg(strMasterAccount).arg(strMasterHost).arg(nMasterPort).arg(strError);
QMessageBox::information(nullptr, tr("Information"), strMessage, QMessageBox::Ok);
return false;
}
res = MasterServer.Query("show master status");
if (res.RowCount())
{
strMasterLogFile = (char*)res["File"];
ui.lineEdit_MasterlogFile->setText(strMasterLogFile);
nMasterlogPos = res["Position"];
ui.lineEdit_MasterlogFilePos->setText(QString("%1").arg(nMasterlogPos));
}
nResult = DBConnector.Connect("127.0.0.1",
"root",
strDBPassword.toStdString().c_str(),
nullptr,
nServerPort);
if (nResult) // return a non zero value indicated a failure!
{
QString strError = DBConnector.GetErrorMsg();
strMessage = QString("Failed in connect to Server %[email protected]:%2(%3)").arg(strMasterAccount).arg(nMasterPort).arg(strError);
QMessageBox::information(nullptr, tr("Information"), strMessage, QMessageBox::Ok);
return false;
}
if (bCheckExternAccess)
{
strSQL = QString("GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '%1' WITH GRANT OPTION;").arg(strDBPassword);
DBConnector.ExecuteSQLString(strSQL.toStdString().c_str());
DBConnector.ExecuteSQLString("flush privileges");
}
/*
stop slave;
change master to master_host='192.168.58.138',master_port,master_user='SlaveHost',master_password='Mago&Zpmc@2020', master_log_file='Master-bin.000004',master_log_pos=120;
Start slave;
*/
strSQL = QString("change master to master_host='%1',"
"master_port=%2,"
"master_user='%3',"
"master_password='%4', "
"master_log_file='%5',"
"master_log_pos=%6")
.arg(strMasterHost)
.arg(nMasterPort)
.arg(strReplicationAccount)
.arg(strReplicationPassword)
.arg(strMasterLogFile)
.arg(nMasterlogPos);
DBConnector.ExecuteSQLString("stop slave");
DBConnector.ExecuteSQLString(strSQL.toStdString().c_str());
DBConnector.ExecuteSQLString("start slave");
res = DBConnector.Query("show slave status");
if (res.RowCount())
{
char* pSlaveIORunning = res["Slave_IO_Running"];
if (pSlaveIORunning)
ui.lineEdit_SlaveIO_Running->setText(pSlaveIORunning);
//ui.checkBox_IORunning->setChecked(strcmp(pSlaveIORunning, "Yes") == 0);
char* pSlaveSQLRunning = res["Slave_SQL_Running"];
//ui.checkBox_SQLRunning->setChecked(strcmp(pSlaveSQLRunning, "Yes") == 0);
if (pSlaveSQLRunning)
ui.lineEdit_SlaveSQL_Running->setText(pSlaveSQLRunning);
}
return true;
}
catch (std::exception& e)
{
QMessageBox::information(nullptr, tr("Exception"), e.what(), QMessageBox::Ok);
return false;
}
}
void MySQLConfig::on_pushButton_ApplySettings_clicked()
{
if (QMessageBox::warning(nullptr,
tr("Warning"),