系統:centos7.5 軟件版本:mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 安裝目錄:/usr/local/mysql 數據庫目錄:/data/mysql 數據庫root密碼:hc123456 A數據庫:192.168.2.72 主 B數據庫:192.168.2.73 從 數據庫同步帳號: sync 數據庫sync密碼: pcpaeyphmp
兩臺分別都安裝mysql
建立mysql組指定組id爲1200,建立mysql用戶指定用戶id爲1200默認組mysqllinux
groupadd -g 1200 mysql useradd -r -g mysql -u 1200 -s /sbin/nologin mysql
mysql數據庫數據存儲目錄sql
datadir=/data/mysql # 數據庫數據存儲目錄 mkdir -p $datadir basedir=/usr/local/mysql # 數據庫安裝目錄
setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
配置軟件安裝源,安裝jemalloc適合多線程下內存分配管理,減小內存碎片數據庫
rm -rf /etc/yum.repos.d/* curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo yum install -y jemalloc-devel
curl -OL http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz --progress tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.23-linux-glibc2.12-x86_64/ $basedir
cp $basedir/support-files/mysql.server /etc/init.d/mysqld sed -i "s@^basedir=.*@basedir=$basedir@" /etc/init.d/mysqld sed -i "s@^datadir=.*@datadir=$datadir@" /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on
cat << EOF >/etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8mb4 [mysql] prompt="MySQL [\\d]> " no-auto-rehash [mysqld] skip-ssl port = 3306 user = mysql server-id = 1 bind-address = 0.0.0.0 log_timestamps = SYSTEM socket = /tmp/mysql.sock basedir = $basedir datadir = $datadir character-set-server = utf8mb4 pid-file = $datadir/mysql.pid init-connect = 'SET NAMES utf8mb4' back_log = 300 #skip-networking skip-name-resolve max_connections = 1000 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 500M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1 query_cache_size = 8M query_cache_limit = 2M ft_min_word_len = 4 log_bin = mysql-bin binlog_format = mixed expire_logs_days = 7 slow_query_log = 1 long_query_time = 1 log_error = $datadir/mysql-error.log slow_query_log_file = $datadir/mysql-slow.log performance_schema = 0 explicit_defaults_for_timestamp #lower_case_table_names = 1 skip-external-locking default_storage_engine = InnoDB #default-storage-engine = MyISAM innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 10M myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 500M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M EOF
cp /etc/my.cnf{,.bak} Mem=`free -m | awk '/Mem:/{print $2}'` sed -i "s@max_connections.*@max_connections = $((${Mem}/3))@" /etc/my.cnf if [ ${Mem} -gt 1500 -a ${Mem} -le 2500 ]; then # 1500MB < 實際內存 <= 2500MB sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf elif [ ${Mem} -gt 2500 -a ${Mem} -le 3500 ]; then # 2500MB < 實際內存 <= 3500MB sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf elif [ ${Mem} -gt 3500 ]; then # 3500MB < 實際內存 sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf fi
rm -rf /etc/ld.so.conf.d/mariadb-x86_64.conf echo "$basedir/lib" > /etc/ld.so.conf.d/mysql.conf ldconfig
添加
libjemalloc
模塊,初始化數據庫,啓動mysql
服務centos
sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' $basedir/bin/mysqld_safe $basedir/bin/mysqld --initialize-insecure --user=mysql --basedir=$basedir --datadir=$datadir chmod 600 /etc/my.cnf chown mysql.mysql -R $datadir systemctl start mysqld lsof | grep jemalloc
echo -e "\n# mysqld\nexport PATH=\$PATH:$basedir/bin" >> ~/.bashrc . ~/.bashrc
mysql受權 127.0.0.1,local 表示僅本機, % 表示全部主機bash
RootPass=hc123456 # 數據庫root密碼 mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"${RootPass}\" with grant option;" mysql -e "grant all privileges on *.* to root@'localhost' identified by \"${RootPass}\" with grant option;" mysql -uroot -p${RootPass} -e "reset master;"
mysql -uroot -p${RootPass} -e "grant all privileges on *.* to root@'%' identified by \"${RootPass}\" with grant option;" # 查看數據庫用戶權限 mysql -uroot -p${RootPass} -e "select user,host,authentication_string from mysql.user;"
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload
1.開啓binlog(數據庫二進制日誌),設置server-id,重啓mysql服務服務器
cp /etc/my.cnf{,.`date +%F`} sed -i '14a log-bin = mysql-bin' /etc/my.cnf sed -i 's/server-id.*/server-id = 1/g' /etc/my.cnf sed -i '18a log-bin-index = master-bin.index' /etc/my.cnf systemctl restart mysqld
2.建立同步數據的用戶,受權容許192.168.2.0/24網絡使用sync用戶登陸網絡
mysql -uroot -p${RootPass} create user sync; grant replication slave on *.* to 'sync'@'192.168.2.%' identified by 'pcpaeyphmp'; flush privileges; show master status; exit # MySQL [(none)]> show master status; # 記下 binlog文件的position(偏移)和File(日誌文件)的值) +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 797 | | | | +------------------+----------+--------------+------------------+-------------------+
1.配置從庫只容許來自服務器線程或具備SUPER權限的數據庫用戶進行更新(root,sync),開啓binlog(數據庫二進制日誌),設置server-id,重啓mysql服務多線程
cp /etc/my.cnf{,.`date +%F`} sed -i '11a read-only' /etc/my.cnf sed -i '15a log-bin = mysql-bin' /etc/my.cnf sed -i 's/server-id.*/server-id = 2/g' /etc/my.cnf sed -i '19a log-bin-index = master-bin.index' /etc/my.cnf systemctl restart mysqld
mysql -uroot -p${RootPass} change master to master_host='192.168.2.72', # A數據庫 IP或主機名(需解析通) master_port=3306, # A數據庫端口號 master_user='sync', # 同步帳號 master_password='pcpaeyphmp', # 同步帳號的密碼 master_log_file='mysql-bin.000002', # A數據庫執行 show master status; 獲取的 File 值 master_log_pos=797; # A數據庫執行 show master status; 獲取的 Position 值 start slave; # 啓動slave show slave status\G; # 查看同步狀態 exit # MySQL [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.72 Master_User: sync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 797 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes ............................ # 保證如下兩項狀態爲 Yes 鏈接成功 Slave_IO_Running: Yes Slave_SQL_Running: Yes
A建立數據庫curl
create database test_01; show databases; # MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test_01 | +--------------------+
B查看數據庫
show databases; # MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test_01 | +--------------------+
Slave_IO_Running: # 狀態不爲Yes 1.數據庫A與數據庫B之間網絡是否通暢 2.同步帳號的用戶權限,及是否容許數據庫B登陸 3.查看兩端server-id(不能相同) 使用mysql命令查看 show variables like 'server_id'; 此配置項在my.cnf 4.配置從鏈接主時 binlog 文件的 position(偏移)和 File(日誌文件)的值) 是否正確