-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.php
91 lines (60 loc) · 2.52 KB
/
transfer.php
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
<?php
$config = require 'config.php';
// MySQL veritabanı bağlantı bilgileri
$mysqli = new mysqli(
$config['mysql']['host'],
$config['mysql']['user'],
$config['mysql']['password'],
$config['mysql']['database']
);
// MySQL bağlantı hatası kontrolü
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Redis bağlantısı
$redis = new Redis();
$redis->connect($config['redis']['host'], $config['redis']['port']);
$redis->auth($config['redis']['password']); // Redis parolası
// Belirli bir Redis veritabanını seç
$redis->select($config['redis']['database']); // Redis veritabanı numarası
// Loglama fonksiyonu
function log_message($message) {
echo $message . "\n";
}
// Redis veritabanını temizle
$redis->flushDB(); // Bu tüm Redis veritabanını siler gerek duymazsanız silebilirsiniz
log_message("Redis veritabanı temizlendi.");
// MySQL veritabanındaki tüm tabloları çek
$tables = $mysqli->query("SHOW TABLES");
$tableCount = 0;
while ($table = $tables->fetch_array()) {
$tableCount++;
$tableName = $table[0];
log_message("Tablo işleniyor: $tableName");
// Tablo için birincil anahtar sütununu bul
$primaryKeyResult = $mysqli->query("SHOW KEYS FROM $tableName WHERE Key_name = 'PRIMARY'");
if ($primaryKeyResult->num_rows > 0) {
$primaryKeyRow = $primaryKeyResult->fetch_assoc();
$primaryKey = $primaryKeyRow['Column_name'];
// Tüm satırları çek
$result = $mysqli->query("SELECT * FROM $tableName");
$rowCount = 0;
// Her satırı Redis'e kaydet
while ($row = $result->fetch_assoc()) {
$rowCount++;
// Anahtarın boş olup olmadığını kontrol et
if (isset($row[$primaryKey]) && $row[$primaryKey] != '') {
// Her satırı JSON olarak kaydet
$redis->set("$tableName:" . $row[$primaryKey], json_encode($row));
} else {
log_message("Boş veya geçersiz birincil anahtar değeri: $tableName satır $rowCount");
}
}
log_message("$tableName tablosundan $rowCount satır Redis'e yüklendi.");
} else {
log_message("Tablo $tableName için birincil anahtar bulunamadı.");
}
}
log_message("Toplam işlenen tablo sayısı: $tableCount");
echo "Veriler Redis'e yüklendi!";
?>