91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

測試mysql主從配置:實現一主一從讀寫分離

發布時間:2020-07-02 20:04:39 來源:網絡 閱讀:331 作者:wx5d0cd2f748c0c 欄目:建站服務器

一、主從介紹
Mysql主從又叫Replication、AB復制。簡單講就是A與B兩臺機器做主從后,在A上寫數據,另外一臺B也會跟著寫數據,實現數據實時同步。

二、主從作用
1、實時災備,用于故障切換
2、讀寫分離,提供查詢服務
3、備份,避免影響業務

三、在兩臺服務器上都按裝mysql

1、環境準備
關閉防火墻以SELINUX
[root@yanyinglai ~]# systemctl stop firewalld
[root@yanyinglai ~]# systemctl disable firewalld
[root@yanyinglai ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@yanyinglai ~]# setenforce 0

2、安裝mysql
安裝依賴包
[root@yanyinglai ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

3、創建用戶和組
[root@yanyinglai ~]# groupadd -r -g 306 mysql
[root@yanyinglai ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

4、下載二進制格式的mysql軟件包
[root@yanyinglai ~]# cd /usr/src/
[root@yanyinglai src]#wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

5、解壓軟件至/usr/local/
[root@yanyinglai src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@yanyinglai src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@yanyinglai src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 sbin share src
[root@yanyinglai src]# cd /usr/local/
[root@yanyinglai local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@yanyinglai local]# ll

總用量 0
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
lrwxrwxrwx. 1 root root 36 9月 7 22:20 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 9月 7 22:19 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 9月 3 23:02 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src

6、修改目錄/usr/locaal/mysql的屬主屬組
[root@yanyinglai local]# chown -R mysql.mysql /usr/local/mysql
[root@yanyinglai local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月 7 22:20 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

7、添加環境變量
[root@yanyinglai local]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@yanyinglai local]# cd
[root@yanyinglai ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@yanyinglai ~]# . /etc/profile.d/mysql.sh
[root@yanyinglai ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

8、建立數據存放目錄
[root@yanyinglai ~]# cd /usr/local/mysql
[root@yanyinglai mysql]# mkdir /opt/data
[root@yanyinglai mysql]# chown -R mysql.mysql /opt/data/
[root@yanyinglai mysql]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 9月 7 22:25 data

9、初始化數據庫
[root@yanyinglai mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
//這個命令的最后會生成一個臨時密碼,此處密碼是1EbNA-k*BtKo

10、配置mysql
[root@yanyinglai ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@yanyinglai ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@yanyinglai ~]# ldconfig -v

11、生成配置文件
[root@yanyinglai ~]# cat > /etc/my.cnf <<EOF

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

12、配置服務啟動腳本
[root@yanyinglai ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@yanyinglai ~]# sed -ri 's#^(basedir=).#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@yanyinglai ~]# sed -ri 's#^(datadir=).
#\1/opt/data#g' /etc/init.d/mysqld

13、啟動mysql
[root@yanyinglai ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/yanyinglai.err'.
.. SUCCESS!
[root@yanyinglai ~]# ps -ef|grep mysql
root 4897 1 0 22:38 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 5075 4897 6 22:38 pts/2 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yanyinglai.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 5109 4668 0 22:38 pts/2 00:00:00 grep --color=auto mysql
[root@yanyinglai ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25
:
LISTEN 0 128 :::22 :::

LISTEN 0 100 ::1:25 :::
LISTEN 0 80 :::3306 :::

14、修改密碼
使用臨時密碼修改
[root@yanyinglai ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

三、mysql主從配置

1、確保從數據庫與主數據庫的數據一樣。
(1)先在主數據庫創建所需要同步的庫和表
[root@yanyinglai ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. Al

Oracle is a registered trademark of Oracle Corporation and
affiliates. Other names may be trademarks of their respect
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the curr
a、創建數據庫yan
mysql> create database yan;
Query OK, 1 row affected (0.00 sec)
b、創建數據庫lisi
mysql> create database lisi;
Query OK, 1 row affected (0.00 sec)
c、創建數據庫wangwu
mysql> create database wangwu;
Query OK, 1 row affected (0.00 sec)
d、useyan數據庫
mysql> use yan;
Database changed
f、創建一個表tom
mysql> create table tom (id int not null,name varchar(100)not null ,age tinyint);
Query OK, 0 rows affected (11.83 sec)
g、插入tom表中的值
mysql> insert tom (id,name,age) values(1,'zhangshan',20),(2,'wangwu',7),(3,'lisi',23);
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
h、展示tom表
mysql> select * from tom;
+----+--------------+------+
| id | name | age |
+----+--------------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
+----+---------------+-----+
3 rows in set (0.00 sec)

(2)備份主庫
備份主庫時需要另開一個終端,給數據庫上讀鎖,避免在備份期間有其他人在寫入導致數據同步的不一致
a、進入mysql
[root@yanyinglai ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
b、關閉所有打開的表并使用全局讀鎖鎖定所有數據庫的所有表
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.76 sec)
//此鎖表的終端必須在備份完成以后才能退出(退出鎖表失效)
c、備份主庫并將備份文件傳送到從庫
[root@yanyinglai ~]# mysqldump -uroot -p123456 --all-databases > /opt/all-20180907.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
d、列出opt目錄下的文件
[root@yanyinglai ~]# ls /opt/
all-20180907.sql data
g、復制/opt目錄下的all-20180907.sql文件去192.168.1.201的/opt目錄
[root@yanyinglai ~]# scp /opt/all-20180907.sql root@192.168.1.201:/opt/
ECDSA key fingerprint is SHA256:7mLj77SFk7sPkhjpMPfdK3nZ98hOuyP4OKzjXeijSJ0.
ECDSA key fingerprint is MD5:a0:1b:eb:7f:f0:b6:7b:73:97:91:4c:f3:b1:89:d8:ea.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.55.129' (ECDSA) to the list of known hosts.
root@192.168.55.129's password:
all-20180907.sql 100% 784KB 783.3KB/s 00:01

h、解除主庫的鎖表狀態,直接退出交互式界面即可

mysql> quit
Bye

(3)在從庫上恢復主庫的備份并查看是否與主庫的數據保持一致

[root@yanyinglai ~]# mysql -uroot -p123456 < /opt/all-20180907.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yanyinglai ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lisi |
| mysql |
| performance_schema |
| sys |
| wangwu |
| yan |
+--------------------+
7 rows in set (0.18 sec)

mysql> use yan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tom;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
+----+-----------+------+
3 rows in set (0.06 sec)
(4)在主數據庫創建一個同步賬戶授權給從數據使用
[root@yanyinglai ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
a、創建一個用戶repl,密碼
mysql> create user 'repl'@'192.168.1.201' identified by '123456';
Query OK, 0 rows affected (5.50 sec)
b、授予從服務器192.168.1.201復制權限
mysql> grant replication slave on . to 'repl'@'192.168.1.200';
Query OK, 0 rows affected (0.04 sec)
c、授予刷新權限
mysql> flush privileges;
Query OK, 0 rows affected (0.09 sec)
(5)配置主數據庫編輯配置文件
[root@yanyinglai ~]# vim /etc/my.cnf
[root@yanyinglai ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql ###基本路徑
datadir = /opt/data ###數據目錄
socket = /tmp/mysql.sock ###接口路徑
port = 3306 ###端口
pid-file = /opt/data/mysql.pid ##pid文件路徑
user = mysql
skip-name-resolve
//添加以下內容
log-bin=mysql-bin //啟用binlog日志
server-id=1 //主數據庫服務器唯一標識符 主的必須必比從大
log-error=/opt/data/mysql.log //錯誤日志路徑
(6)重啟mysql服務
[root@yanyinglai ~]# service mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL.Logging to '/opt/data/mysql.log'.
............................... SUCCESS!
(7)列出所有打開的網絡連接端口
[root@yanyinglai ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25
:
LISTEN 0 128 :::22 :::

LISTEN 0 100 ::1:25 :::
LISTEN 0 80 :::3306 :::

(8)查看主庫的狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
(9)配置從數據庫
編輯配置文件
[root@yanyinglai ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//添加以下內容:
server-id=2 //設置從庫的唯一標識符 從的必須比主小
relay-log=mysql-relay-bin //啟用中繼日志relay log
error-log=/opt/data/mysql.log
(10)重啟從庫的mysql服務
[root@yanyinglai ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@yanyinglai ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25
:
LISTEN 0 128 :::22 :::

LISTEN 0 100 ::1:25 :::
LISTEN 0 80 :::3306 :::

(11)配置并啟動主從復制
mysql> change master to
-> master_host='192.168.1.200',
-> master_user='repl',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.28 sec)
(13)查看從服務器狀態
mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.55.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此處必須是yes
Slave_SQL_Running: Yes //此處必須是yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 154
Relay_Log_Space: 527
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 5abf1791-b2af-11e8-b6ad-000c2980fbb4
Master_Info_File: /opt/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

四、測試驗證在主服務器的yan庫的tom表插入數據:
(1)在yan庫的tom表中插入數據
mysql> use yan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tom;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
+----+-----------+------+
3 rows in set (0.09 sec)

mysql> insert tom(id,name,age) value (4,"yyl",18);
Query OK, 1 row affected (0.14 sec)

mysql> select * from tom;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
| 4 | yyl | 18 |
+----+-----------+------+
4 rows in set (0.00 sec)
(2)在從數據庫查看是否數據同步
mysql> use yan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tom;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | wangwu | 7 |
| 3 | lisi | 23 |
| 4 | yyl | 18 |
+----+-----------+------+
4 rows in set (0.00 sec)

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

林口县| 安溪县| 新平| 嵊州市| 肥西县| 邳州市| 荥经县| 揭东县| 南雄市| 巍山| 田阳县| 望奎县| 彩票| 武陟县| 漠河县| 阳朔县| 华阴市| 岢岚县| 潜江市| 舟曲县| 龙岩市| 赤壁市| 贵港市| 长葛市| 鹤壁市| 章丘市| 方城县| 焦作市| 林甸县| 佳木斯市| 南陵县| 扎鲁特旗| 文水县| 海宁市| 商洛市| 修水县| 朝阳区| 蕉岭县| 上栗县| 大方县| 嘉荫县|