[toc]mysql
本教程會進行mysql一機多實例的安裝、mysql主從同步配置、半同步配置linux
OS: CentOS Linux release 7.2.1511 (Core)c++
mysql: mysql-5.7.29-linux-glibc2.12-x86_64sql
# centos添加mysql用戶 useradd -s /sbin/nologin mysql # 建立工做目錄 mkdir /var/workspace cd workspace # 下載mysql wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz # 解壓mysql tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz # 修改文件屬主爲mysql用戶 chown -R mysql:mysql mysql-5.7.29-linux-glibc2.12-x86_64
yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel perl-Data-Dumper net-tools
shell
# 建立數據庫數據目錄 mkdir -p /var/workspace/mysql_3306/data # 將數據目錄屬主改成mysql chown -R mysql:mysql /var/workspace/mysql_3306 # 執行初使化 /var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysqld --initialize --user=mysql --basedir=/var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/ --datadir=/var/workspace/mysql_3306/data --explicit_defaults_for_timestamp
注意:初使化時會隨機生成一個root帳號的密碼,請記下來
建立mysql主庫的配置文件 /var/workspace/mysql_3306/data/my.cnf數據庫
[mysqld] # mysql安裝目錄 basedir=/var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64 # mysql數據存放目錄 datadir=/var/workspace/mysql_3306/data # mysql啓動的用戶 user=mysql port=3306 socket=/tmp/mysql3306.sock # 表名不區分大小寫 lower_case_table_names=1
從庫可安裝在其它主機或同主機(端口與相關文件不能相同),目前配置文件相同。centos
# 手工啓動 /var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysqld_safe --defaults-file=/var/workspace/mysql_3306/data/my.cnf --pid-file=/var/workspace/mysql_3306/data/mysql_3306.pid >/dev/null & # 經過 mysql.server 指定配置文件啓動 /var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/support-files/mysql.server --defaults-file=/var/workspace/mysql_3306/data/my.cnf --datadir=/var/workspace/mysql_3306/data --pid-file=/var/workspace/mysql_3306/data/mysql_3306.pid
mysql多實例啓動有兩種:bash
經過mysql.server腳本啓動,指定不一樣配置文件啓動。爲了簡化啓動,我修改了mysql.server腳本,以下服務器
cp /var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/support-files/mysql.server /var/workspace/mysql.server
session
修改/var/workspace/mysql.server腳本以下位置和內容:
# 46行47,修改 basedir和datadir路徑 basedir=/var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64 datadir=/var/workspace/mysql_$2/data # 266,增長默認配置文件路徑,默認在數據目錄下my.cnf $bindir/mysqld_safe --defaults-file="$datadir"/my.cnf --datadir="$datadir" --pid-file="$mysqld_pid_file_path" >/dev/null &
修改腳本後啓動命令變爲以下,第二個參數爲數據目錄mysql_後的值,通常爲端口號,方便辨認
# 啓動 /var/workspace/mysql.server start 3306 # 重啓 /var/workspace/mysql.server restart 3306 # 中止 /var/workspace/mysql.server stop 3306
# 本地登錄mysql,須要輸入剛纔記錄下的密碼 /var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysql -S /tmp/mysql3306.sock -uroot -p # 查看數據庫,須要重設置root密碼 mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. # 設置root帳號密碼 mysql> set password='12345678'; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> create database db;
show tables或show tables from database_name; -- 顯示當前數據庫中全部表的名稱。 show databases; -- 顯示mysql中全部數據庫的名稱。 show columns from table_name from database_name; -- 顯示錶中列名稱。 show grants for user_name; -- 顯示一個用戶的權限,顯示結果相似於gr show columns from database_name.table_name; -- 顯示錶中列名稱。 show grants for user_name; -- 顯示一個用戶的權限,顯示結果相似於grant 命令。 show index from table_name; -- 顯示錶的索引。 show status; -- 顯示一些系統特定資源的信息,例如,正在運行的線程數量。 show variables; -- 顯示系統變量的名稱和值。 show processlist; -- 顯示系統中正在運行的全部進程,也就是當前正在執行的查詢。大多數用戶能夠查看他們本身的進程,可是若是他們擁有process權限,就能夠查看全部人的進程,包括密碼。 show table status; -- 顯示當前使用或者指定的database中的每一個表的信息。信息包括表類型和表的最新更新時間。 show privileges; -- 顯示服務器所支持的不一樣權限。 show create database database_name; -- 顯示create database 語句是否可以建立指定的數據庫。 show create table table_name; -- 顯示create database 語句是否可以建立指定的數據庫。 show engines; -- 顯示安裝之後可用的存儲引擎和默認引擎。 show logs; -- 顯示BDB存儲引擎的日誌。 show warnings; -- 顯示最後一個執行的語句所產生的錯誤、警告和通知。 show errors; -- 只顯示最後一個執行語句所產生的錯誤。 show engine innodb status\G; -- 顯示innoDB存儲引擎的狀態。 show plugins; -- 查看mysql插件庫
修改/var/workspace/mysql_3306/data/my.cnf文件,在[mysqld]下新增內容以下,修改後重啓。
# 主數據庫端ID號 server_id=1 # 開啓binlog日誌 log-bin=mysql-bin # 須要複製的數據庫名,若是複製多個數據庫,重複設置這個選項便可 binlog-do-db=db binlog-do-db=sbux # 將函數複製到slave log_bin_trust_function_creators=1
# 建立slave帳號,用於從庫鏈接主庫 mysql> grant replication slave,replication client on *.* to 'slave'@'10.9.62.185' identified by 'Abc@12345'; Query OK, 0 rows affected, 1 warning (0.00 sec) # 刷新權限,使權限當即生效 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec) # 查看主庫狀態,以下說明主庫binlog已正常打開 mysql> show master status\G; *************************** 1. row *************************** File: mysql-bin.000001 Position: 3392 Binlog_Do_DB: db,sbux Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) ERROR: No query specified
修改/var/workspace/mysql_3306/data/my.cnf文件,在[mysqld]下新增內容以下,修改後重啓。
# 從節點的ID號,不能和主節點的同樣 server_id=2 # 開啓重放日誌 relay_log=relay-log # 從庫只讀開啓 read_only=ON
# 本地登錄mysql /var/workspace/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysql -S /tmp/mysql3306.sock -uroot -p # 鏈接主庫,MASTER_HOST爲主庫ip,使用在主庫建立的帳號密碼slave鏈接,MASTER_LOG_FILE爲主庫的log文件名 mysql> CHANGE MASTER TO MASTER_HOST='10.9.54.71',MASTER_USER='slave',MASTER_PASSWORD='Abc@12345',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=0; mysql> CHANGE MASTER TO MASTER_HOST='10.9.54.71',MASTER_USER='slave',MASTER_PASSWORD='Abc@12345',MASTER_AUTO_POSITION=1; # 啓動slave mysql> START SLAVE; # 查看從庫slave狀態,以下Slave_IO_State: Waiting for master to send event,說明從庫下在等待主庫事件。 mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.9.54.71 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 3392 Relay_Log_File: relay-log.000003 Relay_Log_Pos: 3605 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: 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: 3392 Relay_Log_Space: 3806 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: 2850977a-53f9-11ea-9c76-525400ee31ea Master_Info_File: /var/workspace/mysql_3306/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
mysql> use db; mysql> CREATE TABLE sbux_users ( id BIGINT ( 20 ) NOT NULL, `name` VARCHAR ( 20 ) NOT NULL, `desc` VARCHAR ( 100 ), PRIMARY KEY ( id ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO sbux_users(id,`name`,`desc`) VALUES(1,'zhangs','goods1'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO sbux_users(id,`name`,`desc`) VALUES(2,'zhangs2','goods2'); Query OK, 1 row affected (0.01 sec) mysql> show tables; +--------------+ | Tables_in_db | +--------------+ | sbux_users | +--------------+ 1 rows in set (0.00 sec) mysql> SELECT * FROM sbux_users; +----+---------+--------+ | id | name | desc | +----+---------+--------+ | 1 | zhangs | goods1 | | 2 | zhangs2 | goods2 | +----+---------+--------+ 2 rows in set (0.00 sec)
mysql> use db; mysql> show tables; +--------------+ | Tables_in_db | +--------------+ | sbux_users | +--------------+ 1 rows in set (0.00 sec) mysql> SELECT * FROM sbux_users; +----+---------+--------+ | id | name | desc | +----+---------+--------+ | 1 | zhangs | goods1 | | 2 | zhangs2 | goods2 | +----+---------+--------+ 2 rows in set (0.00 sec)
# 安裝主庫的半同步插件 mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; # 打開半同步 mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1; Query OK, 0 rows affected (0.00 sec) # 查看半同步相關參數Rpl_semi_sync_master_status爲ON,半同步打開成功 mysql> show status like 'Rpl%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 12 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 746 | | Rpl_semi_sync_master_tx_wait_time | 8953 | | Rpl_semi_sync_master_tx_waits | 12 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 12 | +--------------------------------------------+-------+ 14 rows in set (0.00 sec)
/var/workspace/mysql_3306/data/my.cnf配置文件新增項,數據庫啓動後啓用半同步
# 半同步插件加載 plugin-load=rpl_semi_sync_master=semisync_master.so # 打開主的半同步 rpl_semi_sync_master_enabled=1
# 安裝從庫的半同步插件 mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; # 打開半同步 mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; # 重啓從的同步線程 mysql> STOP SLAVE IO_THREAD; mysql> START SLAVE IO_THREAD; # 查看半同步相關參數Rpl_semi_sync_slave_status爲ON,半同步打開成功 mysql> show status like 'Rpl%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | ON | +----------------------------+-------+ 1 row in set (0.00 sec)
/var/workspace/mysql_3306/data/my.cnf配置文件新增項,數據庫啓動後啓用半同步
# 從庫半同步插件加載 plugin-load=rpl_semi_sync_slave=semisync_slave.so # 打開從的半同步 rpl_semi_sync_slave_enabled=1
同 數據驗證