MySQL 安裝及多例項主從
阿新 • • 發佈:2020-08-16
1. 環境準備
外網 | 內網 | 角色 |
---|---|---|
10.0.0.20 | 172.16.1.20 | 主庫(多例項) |
10.0.0.21 | 172.16.1.21 | 從庫(多例項) |
10.0.0.22 | 172.16.1.22 | 從庫(多例項) |
-
二進位制安裝mysql
-
建立多例項
-
多例項主從複製
-
主從同步
1. 二進位制安裝 Mysql
1)安裝依賴
yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel
2)解壓包
tar xf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
3)建立自定義目錄
mkdir /service
4)移動目錄並做軟連線
mv mysql-5.6.46-linux-glibc2.12-x86_64 /service/
ln -s /service/mysql-5.6.46-linux-glibc2.12-x86_64 /service/mysql5)建立使用者
useradd mysql -s /sbin/nologin -M
6)拷貝配置檔案和啟動指令碼
cd /service/mysql/support-files/
cp my-default.cnf /etc/my.cnf
cp: overwrite '/etc/my.cnf'? y
# 主庫配置檔案
vim /etc/my.cnf
basedir = /service/mysql
datadir = /service/mysql/data
port=mysql
server_id=1
skip_name_resolve
log_err=/service/mysql/data/mysql.err
log_bin=/service/mysql/data/mysql-bin
cp mysql.server /etc/init.d/mysqld7)初識化3306資料庫
cd /service/mysql/scripts/
./mysql_install_db --user=mysql --basedir=/service/mysql --datadir=/service/mysql/data8)配置system管理mysql
vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/service/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
systemctl daemon-reload
systemctl start mysqld#9)啟動資料庫
/etc/init.d/mysqld start
#啟動失敗,因為mysql檔案中很多都是/usr/local/mysql,我們需要替換
sed -i 's#/usr/local/mysql#/service/mysql#g' /etc/init.d/mysqld /service/mysql/bin/mysqld_safe
#修改配置檔案
vim /etc/my.cnf
basedir = /service/mysql
datadir = /service/mysql/data
#再次啟動
/etc/init.d/mysqld start
Starting MySQL.Logging to '/service/mysql/data/db03.err'.
SUCCESS!10)配置環境變數
vim /etc/profile.d/mysql.sh
export PATH=/service/mysql/bin:$PATH
[root@db03 mysql]# source /etc/profile11)確認啟動
ps -ef | grep mysql
2. 建立多例項
#mysql 多例項,也是多個配置檔案啟動資料庫
mysql多個配置檔案:
1.多個埠
2.多個socket檔案
3.多個日誌檔案
4.多個srver_id
5.多個數據目錄1) 建立多例項目錄
mkdir /service/{3308,3309}
2) 編輯配置檔案
# 從庫配置檔案
vim /service/3308/my.cnf
[mysqld]
basedir = /service/mysql
datadir = /service/3308/data
port=3308
socket=/service/3308/mysql.sock
server_id=2
log_err=/service/3308/data/mysql.err
log_bin=/service/3308/data/mysql-bin
# 從庫配置檔案
vim /service/3309/my.cnf
[mysqld]
basedir = /service/mysql
datadir = /service/3309/data
port=3309
socket=/service/3309/mysql.sock
server_id=3
log_err=/service/3309/data/mysql.err
log_bin=/service/3309/data/mysql-bin
3)初始化 多例項資料目錄
## 3308
/service/mysql/scripts/mysql_install_db --defaults-file=/service/3308/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3308/data
## 3309
/service/mysql/scripts/mysql_install_db --defaults-file=/service/3309/my.cnf --user=mysql --basedir=/service/mysql --datadir=/service/3309/data4)授權目錄
chown -R mysql.mysql /service/
5) 啟動多例項
## 3308
mysqld_safe --defaults-file=/service/3308/my.cnf &
## 3309
mysqld_safe --defaults-file=/service/3309/my.cnf &6)連線多例項驗證
mysql -S /service/3308/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+7) 設定密碼
mysqladmin -S /service/3308/mysql.sock -uroot password '123'
mysqladmin -S /service/3309/mysql.sock -uroot password '123'8) 連線
mysql -S /service/3308/mysql.sock -uroot -p
Enter password:
mysql -S /service/3309/mysql.sock -uroot -p
Enter password:
#簡單連線方式
[root@db03 service]# cat /usr/bin/mysql3307
mysql -S /service/3308/mysql.sock -uroot -p1239.1)單機多例項MYSQL主從複製
## 主庫操作
1.主庫授權一個使用者,給從庫用來連線
mysql> grant replication slave on *.* to rep@'%' identified by '123';
Query OK, 0 rows affected (0.18 sec)
3.主庫檢視binlog資訊
mysql> show master status;
+------------------+----------+
| File | Position |
+------------------+----------+
| mysql-bin.000004 | 120 |
+------------------+----------+
1 row in set (0.00 sec)
## 從庫操作
mysql -S /service/3308/mysql.sock -uroot -p
change master to
master_host='172.16.1.22',
master_user='rep',
master_password='123',
master_log_file='mysql-bin.000003',
master_log_pos=317,
master_port=3306;
start slave;
show slave status \G
# 主庫學資料 測試主從同步
create database php;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| php |
| test |
+--------------------+
5 rows in set (0.01 sec)