Centos7 下Mysql5.7.18的安裝和簡單主從
Mysql5.7.18的安裝
#下載解壓tar包
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
cd /usr/local/ && mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
cd /usr/local/mysql && mkdir data
cd ..
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql
chown -R mysql:mysql mysql
cd
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
#修改mysql的啟動文件如下
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#註冊和初始化mysql數據庫
cd /usr/local/mysql/bin
./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize-insecure
(沒有生成root密碼,去掉-insecure會生成root密碼,在最後一行)
mv /etc/my.cnf /etc/my.cnf.bak
vim /etc/my.cnf
(自建mysql的配置文件,添加下面的幾項)
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 一般配置選項
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /var/lib/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid
character-set-server=utf8
default-storage-engine=INNODB
[mysql]
socket = /var/lib/mysql/mysql.sock
no-auto-rehash
default-character-set=utf8
(以上為簡單配置項,具體依據實際配置)
/etc/init.d/mysqld start
#重置root密碼
cd /usr/local/mysql/bin
(方便使用時可建立ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql軟連接)
./mysql -u root -p
set password=password(‘你想設置的root密碼‘);
flush privileges;
exit;
主從配置
兩臺服務器IP
master:192.168.0.1
slave: 192.168.0.2
master主機上的操作;
修改配置文件
vim /etc/my.cnf
添加下面幾行
[mysqld]
server-id=1(服務器的唯一指定id)
log-bin=mysql-bin(開啟二進制日誌)
/etc/init.d/mysqld restart(重啟MySQL)
./mysql -uroot -p
>grant repliaction slave on *.* to [email protected]‘192.168.0.2‘ identified by ‘123456‘;
>flush privileges;
>show master status;(查看主機狀態)
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001| 103 | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
(不要做任何操作,防止master的數據更改)
slave從服務器的操作
vim /etc/my.cnf
添加下面幾行
[mysqld]
server-id=2
log-bin=mysql-bin(不是必須的,開啟比較好一些)
/etc/init.d/mysql restart(重啟MySQL)
./mysql -uroot -p
>change master to master_host=192.168.0.1,master_user=‘mysync‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=103(主機中查詢的數值);
>start slave;
>show slave status\G(查詢從服務器的鏈接情況,必須要滿足兩個‘yes‘)
Slave_IO_State: Waiting for master to send event //I/O狀態
Master_Host: 192.168.0.1 //主服務器ip
Master_User: mysync //具有復制權限的用戶
Master_Port: 3306 //默認監聽的端口
Connect_Retry: 60 //重試時間
Master_Log_File: mysql-bin.000001 //讀取的二進制日誌文件
Read_Master_Log_Pos: 103 //二進制日誌中的位置
Relay_Log_File: relay-bin.000002 //當前讀取的中繼日誌文件
Relay_Log_Pos: 581 //中繼日誌中事件的位置
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //I/O線程已啟動
Slave_SQL_Running: Yes //SQL線程已啟動
................................
................................
Seconds_Behind_Master: 0 //落後主服務器的時間,單位秒
>exit;
(可以使用mysql> help change master to 查看需要的命令)
下面就是測試了在主庫上建新庫,查看從庫是否也生成同樣的庫。
以上是基本的配置,詳細配置可根據自己的需求來添加參數。
Centos7 下Mysql5.7.18的安裝和簡單主從