helloworld

目前分類:資料庫 (45)

瀏覽方式: 標題列表 簡短摘要

1. 下載mysql image
# docker pull mysql:8

2. 查看目前的images 有哪些
# docker images

3. 啟動 mysql,把容器的3306 port 對應到本機的 3306 port ,初始化密碼為 Lab3306
# docker run --name labdb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Lab3306 -d mysql:8

4. 查看容器執行狀態
# docker ps -a 

5. 進入mysql 的容器中
# docker exec -it labdb bash

6. 跳出容器,但不關閉容器
ctrl-p + q

7. docker start 再次回到容器的 shell 中,後面可以接 CONTAINER ID 或是 NAMES
# docker start bb0ab4e9b251
或是
# docker start labdb 

如果是要透過本機連線的話,先安裝mysql client 
# apt install -y default-mysql-client

# mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.002 sec)

MySQL [(none)]>
 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

# mysqldump -h mysqldb -u root -p information_schema
mysqldump: Got error: 1044: Access denied for user 'root'@'test' to database 'information_schema' when using LOCK TABLES

解決方式
加上 --single-transaction
或是 --skip-locl-tables 參數

如:
# mysqldump --single-transaction -h mysqldb -u root -p information_schema
# mysqldump --skip-lock-tables -h mysqldb -u root -p information_schema

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

在 PXC 的環境中,如果 replication 失敗的時候,會在 mysql 的目錄下產生檔名為 GRA_*.log 的紀錄
例如,有一個sql 指令是要移除一個 table ,但是 table 不存在... 等

要如何檢視 GRA_*.log 

1. 下載 GRA-header,並解壓縮
https://www.percona.com/blog/wp-content/uploads/2012/12/GRA-header.zip

2. 將 GRA-header 和欲檢視的  GRA_*.log 合併在同一個檔案
cp GRA-header > GRA_8_5660914-bin.log
cat GRA_8_5660914.log >> GRA_8_5660914-bin.log

3. 透過 mysqlbinlog 檢視
mysqlbinlog -vvv GRA_8_5660914-bin.log

4. 如果出現以下錯誤訊息
mysqlbinlog: [ERROR] unknown variable 'default-character-set=utf8mb4'

5. 可以加 --no-defaults 參數略過錯誤
mysqlbinlog --no-defaults GRA_8_5660914-bin.log


 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

參考資料:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

# import the MongoDB public GPG Key
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

# 如果系統顯示未安裝 gnupg 套件,請先安裝
sudo apt-get install gnupg

# Create a list file for MongoDB
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

# Reload local package database
sudo apt-get update

# Install the MongoDB packages.
# 未指定版本,將會安裝最新版,本文目前為 5.0.3
sudo apt-get install -y mongodb-org

# 另可指令特定版本
sudo apt-get install -y mongodb-org=5.0.2 mongodb-org-database=5.0.2 mongodb-org-server=5.0.2 mongodb-org-shell=5.0.2 mongodb-org-mongos=5.0.2 mongodb-org-tools=5.0.2
 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

參考資料:
https://www.postgresql.org/download/linux/ubuntu/

PostgreSQL是開源的物件-關聯式資料庫資料庫管理系統,在類似BSD授權與MIT授權的PostgreSQL授權下發行。

透過 PostgreSQL Apt Repository 安裝最新版的 PostgreSQL 

# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':

sudo apt-get -y install postgresql
 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

錯誤訊息:
[Fri Jun 11 13:48:51 2021] [error] [client xx.xx.xx.xx] FastCGI: server "/usr/local/sbin/php-fpm" stderr: PHP message: PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1370 execute command denied to user 'user1'@'192.168.0.%' for routine 'mydatabase1.myprocedure1'' in /var/www/html/xxx.php:88, referer: http://myip.pass.tw/example.php

解決方式:
GRANT EXECUTE ON PROCEDURE mydatabase1.myprocedure1    TO 'user1'@'192.168.0.%';
flush privileges;

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

# mysqldump -h 10.1.1.100 -u root -p mydb
Enter password:
-- MySQL dump 10.13  Distrib 5.7.xx-xx, for Linux (x86_64)
--
-- Host: 10.1.1.100    Database: mydb
-- ------------------------------------------------------
-- Server version       5.0.xx-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!50717 SELECT COUNT(*) INTO @rocksdb_has_p_s_session_variables FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'session_variables' */;
/*!50717 SET @rocksdb_get_is_supported = IF (@rocksdb_has_p_s_session_variables, 'SELECT COUNT(*) INTO @rocksdb_is_supported FROM performance_schema.session_variables WHERE VARIABLE_NAME=\'rocksdb_bulk_load\'', 'SELECT 0') */;
/*!50717 PREPARE s FROM @rocksdb_get_is_supported */;
/*!50717 EXECUTE s */;
/*!50717 DEALLOCATE PREPARE s */;
/*!50717 SET @rocksdb_enable_bulk_load = IF (@rocksdb_is_supported, 'SET SESSION rocksdb_bulk_load = 1', 'SET @rocksdb_dummy_bulk_load = 0') */;
/*!50717 PREPARE s FROM @rocksdb_enable_bulk_load */;
/*!50717 EXECUTE s */;
/*!50717 DEALLOCATE PREPARE s */;

--
-- Not dumping tablespaces as no INFORMATION_SCHEMA.FILES table on this server
--

然後就沒了

解決方式

mysqldump 時加入  --compatible 參數
如: 
mysqldump -h 10.1.1.100 -u root -p
--compatible=mysql40 mydb mytable
 

-- MySQL dump 10.13  Distrib 5.7.xx-xx, for Linux (x86_64)
--
-- Host: 10.1.1.100    Database: mydb
-- ------------------------------------------------------
-- Server version       5.0.xx-log
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO,MYSQL40' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!50717 SELECT COUNT(*) INTO @rocksdb_has_p_s_session_variables FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'performance_schema' AND TABLE_NAME = 'session_variables' */;
/*!50717 SET @rocksdb_get_is_supported = IF (@rocksdb_has_p_s_session_variables, 'SELECT COUNT(*) INTO @rocksdb_is_supported FROM performance_schema.session_variables WHERE VARIABLE_NAME=\'rocksdb_bulk_load\'', 'SELECT 0') */;
/*!50717 PREPARE s FROM @rocksdb_get_is_supported */;
/*!50717 EXECUTE s */;
/*!50717 DEALLOCATE PREPARE s */;
/*!50717 SET @rocksdb_enable_bulk_load = IF (@rocksdb_is_supported, 'SET SESSION rocksdb_bulk_load = 1', 'SET @rocksdb_dummy_bulk_load = 0') */;
/*!50717 PREPARE s FROM @rocksdb_enable_bulk_load */;
/*!50717 EXECUTE s */;
/*!50717 DEALLOCATE PREPARE s */;

--
-- Not dumping tablespaces as no INFORMATION_SCHEMA.FILES table on this server
--

--
-- Table structure for table `mytable`
--

DROP TABLE IF EXISTS `mytable`;
CREATE TABLE `mytable` (
........
 

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

如果懶得查 log 到底是哪一筆 commit 出錯
可以用以下懶人排除法

SLAVE STOP;

SET GLOBAL sql_slave_skip_counter = 1;

SLAVE START;

上述做法即是跳過有問題的 commit , 直接往下繼續同步

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

環境:
CentOS 8.x
MySQL Server 5.x

在編譯的過程中出現以下錯誤
mysqld.cc:6503:1: error:narrowing conversion of 「18446744073709551615」 from 「long unsigned int」 to 「longlong」 {aka 「long long int」} inside { } [-Wnarrowing]

解決方式:
在原來的 ./configure 指令後面 加上 CXXFLAGS="-std=gnu++98"

如:
./configure --prefix=/usr/local/mysql  --with-mysqld-user=mysql --localstatedir=/home/mysql CXXFLAGS="-std=gnu++98"

再重新 make 就不會出現錯誤了

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

mysql error log 顯示以下訊息
210203  9:20:44 [ERROR] /usr/local/mysql/libexec/mysqld: Table './mydb/mytable1' is marked as crashed and should be repaired
210203  9:20:44 [Warning] Checking table:   './mydb/mytable1'

解決方式
# mysqlcheck -u root -p --repair mydb mytable1

其他使用方式
Usage: mysqlcheck [OPTIONS] database [tables]
OR     mysqlcheck [OPTIONS] --databases DB1 [DB2 DB3...]
OR     mysqlcheck [OPTIONS] --all-databases

預設值
  -r, --repair        Can fix almost anything except unique keys that aren't unique.
 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

修復 mysql replication 失敗筆記

因為 master 資料庫無預警電源異常
導致 mysql 主從同步失敗

show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State:
                Master_Host: 192.168.x.x
                Master_User: repl
                Master_Port: 3306
              Connect_Retry: 3
            Master_Log_File: MYDB-Master101-bin.001031
        Read_Master_Log_Pos: 329886971
             Relay_Log_File: MYDB-Slave130-relay-bin.003254
              Relay_Log_Pos: 98
      Relay_Master_Log_File: MYDB-Master101-bin.001031
           Slave_IO_Running: No
          Slave_SQL_Running: Yes
            Replicate_Do_DB: db1,db2,db3
        Replicate_Ignore_DB: mysql,test
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 0
                 Last_Error:
               Skip_Counter: 0
        Exec_Master_Log_Pos: 329886971
            Relay_Log_Space: 98
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: NULL
1 row in set (0.00 sec)

slave 資料庫的錯誤訊息
210202 15:39:14 [Note] Slave I/O thread: connected to master 'repl@192.168.x.x:3306',  replication started in log 'MYDB-Master101-bin.001031' at position 329886971
210202 15:39:14 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236)
210202 15:39:14 [ERROR] Got fatal error 1236: 'Client requested master to start replication from impossible position' from master when reading data from binary log
210202 15:39:14 [Note] Slave I/O thread exiting, read up to log 'MYDB-Master101-bin.001031', position 329886971

原因:
因為重啟 master 資料庫後,會重新產生一個新檔名的 binlog file
但是 slave 資料庫不知道,還繼續往下要拉資料回來

解決方式
先停掉 slave 同步
slave stop;

重新指向新的bin log
CHANGE MASTER TO MASTER_LOG_FILE='MYDB-Master101-bin.001032',MASTER_LOG_POS=0;

重新同步
slave start;

重新再看一次同步狀態,沒意外的話應該就正常了
show slave status\G;

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

當你還有舊版mysql server 需要維護
用 phpmysqladmin 登入時,卻出現 "您應升級到 MySQL 5.5.0 或更新版本" 

先確認一下 mysql server 版號
進入mysql ,輸入以下sql
SHOW VARIABLES LIKE "%version%";

如果你的mysql server 版本是 5.1 的話

編輯 config.inc.php
$cfg['MysqlMinVersion'] = array(
    'internal' => 50100,
    'human' => '5.1.0'
);

存檔,重新再登入即可
 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

mysqldump 在備份資料庫時預設不會把 trigger 也備份出來
如果要備份的話
利用下面語法

mysqldump -h servername -u root -pxxxx --no-data --routines --triggers --all-databases > mydb-schema.sql

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

以下情境為單機模式
port: tcp/27017
資料庫路徑: /var/lib/mongodb

1. 在shell 啟動一個mongod 服務
# mongod --port 27017 --dbpath /var/lib/mongodb

2. 另開一個shell 登入mongodb,預設沒有認證功能,直接登入
# mongo --port 27017

3. 在mongo shell 新增資料庫管理員

> use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

4. 停用並重啟mongodb 時加入 --auth 參數

> db.adminCommand( { shutdown: 1 } )

 

# mongod --auth --port 27017 --dbpath /var/lib/mongodb

 

 

如果是呼叫設定檔,請在設定檔加入以下參數

security:
    authorization: enabled

5. 測試用資料庫管理員帳號登入

# mongo --port 27017  --authenticationDatabase "admin" -u "myUserAdmin" -p

6. 新增一般使用者

> use test
db.createUser(
  {
    user: "myTester",
    pwd:  passwordPrompt(),   // or cleartext password
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

7. 測試一般使用者登入

# mongo --port 27017 -u "myTester" --authenticationDatabase "test" -p

8. 測試新增資料

> db.foo.insert( { x: 1, y: 1 } )

參考資訊
https://docs.mongodb.com/manual/tutorial/enable-authentication/

undefined

helloworld 發表在 痞客邦 留言(0) 人氣()

有些時候可能需要調整mysql 原來的編碼,以達到預期的功能
如為了要存放emoji icon時

1. 修改 my.cnf 預設值
[client]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

2. 修改已經存在的 database 預設值
ALTER DATABASE `example_db` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

3. 修改已經存在的 table 預設值
ALTER TABLE `example_db`.`example_table` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ;
但上面這串語法,只會改變table 的預設值,對於已經存在的欄位編碼不會調整

4. 如果要變更欄位編碼,需要再下以下的sql 語法
ALTER TABLE `example_db`.`example_table` MODIFY `example_col` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
或是
ALTER TABLE ``example_db`.`example_table` CHANGE `example_col` `example_col` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 

5. 如果想要table,column 都一起被調整 (強烈建議)
可以直接執行以下語法
ALTER TABLE `example_db`.`example_table` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 


undefined

 

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

$ sudo dnf install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
$ sudo dnf module disable mysql
$ sudo percona-release setup pxc57
$ sudo dnf install Percona-XtraDB-Cluster-57

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

今天發現 Percona XtraDB Cluster 一個node掛掉,準備要把程式切到另一個正常node 的時候
連線到資料庫時出現這個錯誤

WSREP has not yet prepared node for application use

解決方式
登入mysql 後
mysql> SET GLOBAL wsrep_provider_options='pc.bootstrap=YES';
Query OK, 0 rows affected (0.00 sec)

即可

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

準備啟動第二個node 要加入 perconaxtradb cluter 中
但是啟動失敗
看一下 /var/log/mysqld.log
[ERROR] WSREP: Command did not run: wsrep_sst_xtrabackup-v2 --role 'donor' --address ' :4444/xtrabackup_sst//1' --socket '/var/lib/mysql/mysql.sock' --datadir '/home/mysql' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --mysqld-version '5.7.28-31-57'   '' --gtid 'xxxx'

暫時的解法,把 wsrep_sst_method 由 xtrabackup-v2 改成 rsync
vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
# SST method
#wsrep_sst_method=xtrabackup-v2
wsrep_sst_method=rsync

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

> use mydb
switched to db mydb
> db.getCollection('mycollection').find({}).count()
53637
>

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

選擇 CentOS 最小安裝後

# yum -y install epel-release bind-utils net-tools sysstat

# yum -y update

# systemctl disable firewalld

# vi /etc/selinux/config
SELINUX=disabled

# reboot

以上是我習慣裝完OS 後的習慣,僅參考用

接下來才是真的安裝 MongoDB 4.2
編輯 /etc/yum.repos.d/mongodb.repo
# vi /etc/yum.repos.d/mongodb.repo
[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

透過 yum 安裝
# yum -y install mongodb-org

手動啟動服務
# systemctl start mongod.service


設定下次重開機後自動啟用
# systemctl enable mongod.service

檢查安裝的版本
# mongod --version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.1.1 FIPS  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel80
    distarch: x86_64
    target_arch: x86_64

測試可否進入 mongo shell
# mongo

安裝好的 MmongoDB ,預設只允許 localhost 可以登入
如果確認你的網路環境夠安全的話,請修改 /etc/mongod.conf
# vi /etc/mongod.conf
  bindIp: 127.0.0.1 ---> 0.0.0.0

存檔,重新啟動 mongod
# systemctl restart mongod.service

預設 port 為 tcp 27017
# netstat -na | grep LISTEN
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN

undefined

文章標籤

helloworld 發表在 痞客邦 留言(0) 人氣()

1 23
Close

您尚未登入,將以訪客身份留言。亦可以上方服務帳號登入留言

請輸入暱稱 ( 最多顯示 6 個中文字元 )

請輸入標題 ( 最多顯示 9 個中文字元 )

請輸入內容 ( 最多 140 個中文字元 )

reload

請輸入左方認證碼:

看不懂,換張圖

請輸入驗證碼