配置主從數據庫步驟mysql
1.搭建從庫環境(安裝mysql)linux
2.主庫須要開啓binlog日誌sql
配置文件中配置數據庫
log-bin=mysql-binbash
binlog_format=ROWide
server_id = 1 //該配置爲惟一值,從庫中不能存在相同的值工具
配置後必須重啓mysql才能生效,reload是不生效的ui
在主庫建立同步賬號spa
grant replication slave on *.* to‘test’@’192.168.1.%’ identified by ‘123’;日誌
flush privileges;
3.查看當前主庫的binlog位置
mysql> show master status;
+------------------+-----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 823427318 | | | |
+------------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
保存好備用
4.將主庫數據導出,並導入到從庫中
使用mysqldump
或使用tidb的工具包(推薦很好用,特色速度快)
# 下載 tool 壓縮包 wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.tar.gz wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.sha256 # 檢查文件完整性,返回 ok 則正確 sha256sum -c tidb-enterprise-tools-latest-linux-amd64.sha256 # 解開壓縮包 tar -xzf tidb-enterprise-tools-latest-linux-amd64.tar.gz cd tidb-enterprise-tools-latest-linux-amd64
./bin/mydumper -h 127.0.0.1 -P 3306 -u root -t 16 -F 64 -B test -T t1,t2 --skip-tz-utc -o ./var/test
詳細說明參見:這裏
./bin/loader -h 127.0.0.1 -u root -P 4000 -t 32 -d ./var/test
5.配置從庫
log-bin=mysql-bin //binlog日誌
binlog_format=ROW //binlog日誌
server-id = 81 //集羣中惟一
innodb_file_per_table = ON
skip_name_resolve = ON
relay_log = /log/relay.log //同步日誌存放地址
log-slave-updates = true
replicate-ignore-db=database //用來忽略不一樣步的數據庫
replicate-wild-ignore-table=database.% //用來忽略不一樣步的數據庫的表
replicate-do-db=database //用來指定要同步的數據庫
replicate-do-db=database
replicate-do-db=database
replicate-do-db=database
replicate-wild-do-table=database.% //指定要同步的表
replicate-wild-do-table=database.%
replicate-wild-do-table=database.%
replicate-wild-do-table=database.%
slave-skip-errors=1032,1060,1062,1050 跳過錯誤
read_only=ON
6.啓動同步
鏈接到從庫執行
mysql>CHANGE MASTER TO MASTER_HOST='10.11.11.101',MASTER_USER='repl', MASTER_PASSWORD='repl@110',MASTER_LOG_FILE=‘mysql-bin.000001’,MASTER_LOG_POS=823427318;
logfile和logpos使用上邊(步驟3中)主庫打印出來的mysql-bin.000001和823427318
mysql>start slave
mysql> show slave status\G 查看同步信息
若是遇到錯誤,根據錯誤提示修改從庫數據或數據結果
或者使用
mysql>stop slave 中止同步
mysql>set global sql_slave_skip_counter=1; 設置跳過錯誤
mysql>start slave 開始同步
mysql> show slave status\G 查看同步信息
mysql>reset slave 重置同步
忽略指定錯誤碼的錯誤
slave-skip-errors=1032,1060,1062,1050
配置賬號容許外部訪問方法
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
mysql>FLUSH PRIVILEGES;