mysql安裝版本及下載地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
檢查系統中的Mysql和mariadb,若是存在將其卸載mysql
#rpm -qa | grep mysql #rpm -qa | grep mariadb #mariadb-libs-5.5.65-1.el7.x86_64 #yum -y remove mariadb-libs-5.5.65-1.el7.x86_64
建立mysql用戶組和mysql用戶linux
# groupadd mysql #useradd -g mysql -s /sbin/nologin mysql
解壓安裝包sql
#tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ # cd /usr/local/ # mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
設置mysql環境變量數據庫
# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile # source /etc/profile
配置mysqlvim
#mkdir -pv /data/mysql #chown mysql.mysql /data/mysql # chmod go-rwx /data/mysql
初始化數據庫並注意日誌裏面輸出的默認密碼#mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
服務器
配置啓動腳本網絡
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # chkconfig --add mysqld # chkconfig mysqld on # chkconfig --list |grep mysqld
編輯mysql配置文件/etc/my.cnfdom
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock pid_file = /data/mysql/mysql.pid datadir = /data/mysql default_storage_engine = InnoDB max_allowed_packet = 512M max_connections = 2048 open_files_limit = 65535 skip-name-resolve lower_case_table_names=1 character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' innodb_buffer_pool_size = 512M innodb_log_file_size = 1024M innodb_file_per_table = 1 innodb_flush_log_at_trx_commit = 0 key_buffer_size = 64M log-error = /data/mysql/mysql_error.log log-bin = /data/mysql/mysql-bin binlog_format = mixed expire_logs_days = 10 slow_query_log = 1 slow_query_log_file = /data/mysql/slow_query.log long_query_time = 1 server-id=1
啓動mysqlsocket
vi /etc/init.d/mysqld basedir=/usr/local/mysql //在第46行 datadir=/data/mysql # /etc/init.d/mysqld start Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'. SUCCESS!
常見錯誤排除:
若出現沒法登錄數據庫能夠在/etc/my.cnf最後一行加入skip-grant-tables,重啓mysql後直接mysql登錄數據庫,ide
mysql> use mysql; mysql> update MySQL.user set authentication_string=password('root') where user='root' ; mysql> flush privileges;
更新數據庫密碼後刪除掉my.cnf最後一行的skip-grant-tables,重啓數據庫驗證新密碼是否有效
兩臺數據庫一樣方式進行安裝,安裝完成開始進行主從同步的配置
master:192.168.100.10
slave:192.168.100.20
注意:從數據庫服務器上必定要有主數據庫服務器上的庫、表且表結構要一致。
配置master數據庫服務器
#ls -lrt /data/mysql total 2121876 -rw-r----- 1 mysql mysql 56 May 23 16:06 auto.cnf drwxr-x--- 2 mysql mysql 8192 May 23 16:06 performance_schema drwxr-x--- 2 mysql mysql 4096 May 23 16:06 mysql drwxr-x--- 2 mysql mysql 8192 May 23 16:06 sys -rw-r----- 1 mysql mysql 29924 May 23 16:40 localhost.localdomain.err -rw-r----- 1 mysql mysql 1073741824 May 23 16:41 ib_logfile1 -rw-r----- 1 mysql mysql 329 May 23 16:43 mysql-bin.000001 -rw-r----- 1 mysql mysql 718 May 23 16:46 mysql-bin.000002 -rw-r----- 1 mysql mysql 872 May 23 17:25 mysql-bin.000003 -rw-r----- 1 mysql mysql 177 May 23 17:43 mysql-bin.000004 drwxr-x--- 2 mysql mysql 52 May 23 17:54 zs -rw-r----- 1 mysql mysql 666 May 23 18:00 mysql-bin.000005 -rw-r----- 1 mysql mysql 300 May 23 18:00 ib_buffer_pool -rw-r----- 1 mysql mysql 1104 May 23 18:00 slow_query.log -rw-r----- 1 mysql mysql 174 May 23 18:00 mysql-bin.index -rw-r----- 1 mysql mysql 5 May 23 18:00 localhost.localdomain.pid -rw-r----- 1 mysql mysql 12582912 May 23 18:00 ibtmp1 -rw-r----- 1 mysql mysql 41881 May 23 18:05 mysql_error.log -rw-r----- 1 mysql mysql 726 May 23 19:14 mysql-bin.000006 -rw-r----- 1 mysql mysql 12582912 May 23 19:15 ibdata1 -rw-r----- 1 mysql mysql 1073741824 May 23 19:15 ib_logfile0 #vi /etc/my.cnf #log-bin=mysql-bin.000006 server_id=1 #主庫爲1 從庫爲2
修改過my.cnf後重啓數據庫
登錄數據庫執行下面的操做:
mysql> grant all privileges on . to 'test'@'%' identified by 'test' with grant option;
配置slave數據庫服務器
#vim /etc/my.cnf server_id=2 log-bin=slavelog #可開可不開
重啓數據庫
管理員登錄
mysql> change master to -> master_host="192.168.100.10", -> master_user="test", -> master_password="test", -> master_log_file="mysql-bin.000006", 主查詢show master start; -> master_log_pos=0; mysql> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes #兩個進程狀態爲yes mysql>show slave status;
工做原理 slave_io_running:yes 鏈接主數據庫服務器,並把主數據庫服務器binlog日誌裏sql語句拷貝本機的relybinlog日誌裏。 slave_sql_running:yes 執行本機relybinlog日誌裏的sql語句,把數據寫到數據庫裏 4、測試主從同步 在主服務器上建庫建表刪除數據,從服務器會跟着同步; /var/lib/msyql/ master.info 記錄鏈接主數據庫服務器的信息 relay-log.info 記錄中繼binlog日誌信息 localhost-relay-bin.000001 localhost-relay-bin.000002 中繼binlog日誌 localhost-relay-bin.index 保存已有中繼binlog日誌文件名 經常使用的其餘選項 適用於master服務器 binlog-do-db=name 設置master對哪些庫記日誌 binlog-ignore-db=name 設置master對哪些庫不記日誌 log-slave-updates 記錄從庫更新,容許鏈式複製(A-B-C) relay-log=dbsvr2-relay-bin 指定中繼日誌文件名 replicate-do-db=mysql 僅複製指定庫,其餘庫將被忽略,此選項可設置多條(省略時複製全部庫) replicate-ignore-db=test 不復制哪些庫,其餘庫將被忽略,ignore-db與go-db只需選用其中一種 report-host=dbsvr2 報告給master的主機名或ip地址 slave-net-timeout=60 出現網絡中斷時,重試超時(默認60秒)