源码安装mysql
yum install ncurses-devel libaio-devel -y mkdir /server/tools -p cd /server/tools wget https://cmake.org/files/v2.8/cmake-2.8.8.tar.gz tar xf cmake-2.8.8.tar.gz cd cmake-2.8.8 ./configure gmake gmake install which cmake /usr/local/bin/cmake cd .. useradd -s /sbin/nologin -M mysql wget https://downloads.mysql.com/archives/get/file/mysql-5.5.49.tar.gz tar xf mysql-5.5.49.tar.gz cd mysql-5.5.49 cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.49 -DMYSQL_DATADIR=/application/mysql-5.5.49/data -DMYSQL_UNIX_ADDR=/application/mysql-5.5.49/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0 make make install ln -s /application/mysql-5.5.49/ /application/mysql ls /application/mysql
二进制包安装mysql
useradd -s /sbin/nologin -M mysql mkdir /application mkdir /server/tools -p cd /server/tools wget https://downloads.mysql.com/archives/get/file/mysql-5.5.49-linux2.6-x86_64.tar.gz tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49 ln -s /application/mysql-5.5.49/ /application/mysql sed -i 's#/usr/local/#/application/#g' /application/mysql/bin/mysqld_safe cd /application/mysql
创建多实例配置文件和启动脚本
mkdir -p /data/{3306,3307}/data vim /data/3306/my.cnf #3306配置文件 [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3306 socket = /data/3306/mysql.sock basedir = /application/mysql datadir = /data/3306/data open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 external-locking = FALSE max_allowed_packet = 8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k #default_table_type = InnoDB thread_stack = 192K #transaction_isolation = READ-COMMITTED tmp_table_size = 2M max_heap_table_size = 2M long_query_time = 1 pid-file = /data/3306/mysql.pid relay-log = /data/3306/relay-bin relay-log-info-file = /data/3306/relay-log.info binlog_cache_size = 1M max_binlog_cache_size = 1M max_binlog_size = 2M key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db = mysql server-id = 1 innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/data/3306/mysql_peter3306.err pid-file=/data/3306/mysqld.pid vim /data/3307/my.cnf #3307配置文件 [client] port = 3307 socket = /data/3307/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3307 socket = /data/3307/mysql.sock basedir = /application/mysql datadir = /data/3307/data open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 external-locking = FALSE max_allowed_packet = 8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k #default_table_type = InnoDB thread_stack = 192K #transaction_isolation = READ-COMMITTED tmp_table_size = 2M max_heap_table_size = 2M long_query_time = 1 pid-file = /data/3307/mysql.pid relay-log = /data/3307/relay-bin relay-log-info-file = /data/3307/relay-log.info binlog_cache_size = 1M max_binlog_cache_size = 1M max_binlog_size = 2M key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db = mysql server-id = 3 innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/data/3307/mysql_peter3307.err pid-file=/data/3307/mysqld.pid vim /data/3306/mysql #3306启动脚本 #!/bin/bash #################################### #this scripts is created by peter at 2017-08-31 #mail:asdftttt@163.com #################################### #init port=3306 mysql_user="root" mysql_pwd="123456" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" #startup function function_start_mysql(){ if [ ! -e "$mysql_sock" ];then printf "Starting MySQL... " /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null & else printf "MySQL is running... " exit fi } #stop function function_stop_mysql(){ if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped... " exit else printf "Stopping MySQL... " ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown fi } #restart function function_restart_mysql(){ printf "Restarting MySQL... " function_stop_mysql sleep 2 function_start_mysql } case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) printf "Usage: /data/${port}/mysql {start|stop|restart} " esac vim /data/3307/mysql #3307启动脚本 #!/bin/bash #################################### #this scripts is created by peter at 2017-08-31 #mail:asdftttt@163.com #################################### #init port=3307 mysql_user="root" mysql_pwd="123456" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" #startup function function_start_mysql(){ if [ ! -e "$mysql_sock" ];then printf "Starting MySQL... " /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null & else printf "MySQL is running... " exit fi } #stop function function_stop_mysql(){ if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped... " exit else printf "Stopping MySQL... " ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown fi } #restart function function_restart_mysql(){ printf "Restarting MySQL... " function_stop_mysql sleep 2 function_start_mysql } case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) printf "Usage: /data/${port}/mysql {start|stop|restart} " esac tree /data /data ├── 3306 │ ├── data │ ├── my.cnf │ └── mysql └── 3307 ├── data ├── my.cnf └── mysql
配置文件权限,将mysql客户端命令加入路径
chown -R mysql.mysql /data find /data -type f -name mysql | xargs chmod 700 // 启动文件中有数据库管理员密码 find /data -type f -name mysql -exec ls -l {} ; -rwx------ 1 mysql mysql 1203 Sep 2 21:12 /data/3307/mysql -rwx------ 1 mysql mysql 1204 Sep 2 21:12 /data/3306/mysql ln -s /application/mysql/bin/* /usr/local/sbin
初始化数据库,启动多实例,加入开机启动
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql chown -R mysql.mysql /data /data/3306/mysql start /data/3307/mysql start netstat -nutlp | grep 330 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27934/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 28653/mysqld echo "#mysql multi instances" >> /etc/rc.local echo "/data/3306/mysql start" >> /etc/rc.local echo "/data/3307/mysql start" >> /etc/rc.local tail -3 /etc/rc.local #mysql multi instances /data/3306/mysql start /data/3307/mysql start
登录不同实例,设置管理员密码
mysql -S /data/3306/mysql.sock mysql -S /data/3307/mysql.sock mysqladmin -u root -S /data/3306/mysql.sock password '123456' mysqladmin -u root -S /data/3307/mysql.sock password '123456' mysql -uroot -p123456 -S /data/3306/mysql.sock mysql -uroot -p123456 -S /data/3306/mysql.sock
如何再增加一个实例?
#多实例本质是多端口,多配置文件。添加的流程是改配置,改启动脚本,初始化,给权限,启动
mkdir -p /data/3308/data cp /data/3306/my.cnf /data/3308/ cp /data/3306/mysql /data/3308/ sed -i 's#3306#3308#g' /data/3308/my.cnf sed -i 's#server-id = 1#server-id = 8#g' /data/3308/my.cnf sed -i 's#3306#3308#g' /data/3308/mysql chmod 700 /data/3308/mysql /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3308/data --user=mysql chown -R mysql.mysql /data/3308 /data/3308/mysql start echo "/data/3308/mysql start" >> /etc/rc.local netstat -nutlp | grep 3308 tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 29518/mysqld
MySQL主从复制
MySQL的主从复制是MySQL自带的功能,通过逻辑的binlog日志来实现同步。MySQL支持单向、双向、链式、环状等主从复制方式。
主从复制有利于数据库架构的健壮性、提升访问速度,同时主从服务器可以互为备份。
此外,主从复制还可实现读写分离,即主库写从库读,从库做集群。一般企业实现读写分离的方式如下:
中大型公司:通过程序(php,java),根据业务拆分从服务器
测试环境:代理软件(mysql-proxy,amoeba)
门户网站:分布式dbproxy(读写分离,hash负载均衡,健康检查)
MySQL主从复制原理
1.Slave上执行start slave开始主从复制。
2.Slave上IO线程根据CHANGE MASTER命令配置的用户权限请求连接Master,然后请求Master从指定binlog日志文件的指定位置开始发送binlog日志内容。
3.Master上IO线程收到请求,根据请求信息读取指定的binlog日志并返回给Slave,同时还返回Master上新的binlog文件和下一个更新位置。
4.Slave上IO线程收到binlog日志内容和位置信息后,将binlog数据写入relaylog(中继日志)文件,将新的binlog文件名和位置信息记录到master-info,以便下次请求日志时告诉Master请求的位置。
5.Slave上SQL线程检测到relaylog中日志更新后,顺序执行日志中的SQL语句,并在relay-log.info中记录当前执行到的文件名和位置,便于分段执行。
注:数据库主从复制是异步的基于SQL语句的逻辑复制,复制的前提是Master上开启binlog日志记录功能,复制的时候Master上有一个IO线程,Slave上有IO和SQL两个线程,Master和Slave的server-id不能相同,binlog日志只记录对数据库有更改的SQL语句,不记录查询语句。
单机多实例主从复制
主库:Master 3306 从库:Slave1 3307 从库:Slave2 3308 #Master vim /data/3306/my.cnf [mysqld] ... server-id = 1 log-bin = /data/3306/mysql-bin /data/3306/mysql restart mysql -uroot -p123456 -S /data/3306/mysql.sock mysql>show variables like 'server_id'; mysql>show variables like 'log_bin'; mysql>grant replication slave on *.* to 'rep'@'172.16.1.%' identified by '123456'; mysql>flush privileges; mysql>select user,host from mysql.user; mysql>show grants for 'rep'@'172.16.1.%'; mysql>flush table with read lock; // 窗口不能关闭,关闭自动解锁 mysql>show master status; #新开窗口 mkdir /server/backup/ -p mysqldump -uroot -p123456 -S /data/3306/mysql.sock --events -A -B | gzip > /server/backup/mysql_bak.$(date +%F).sql.gz ls -l /server/backup mysql>show master status; mysql>unlock tables; #Slave1 vim /data/3307/my.cnf [mysqld] ... server-id = 3 // id必须唯一 /data/3307/mysql restart cd /server/backup ls -l gzip -d mysql_bak.2017-09-03.sql.gz mysql -uroot -p123456 -S /data/3307/mysql.sock < mysql_bak.2017-09-03.sql mysql -uroot -p123456 -S /data/3307/mysql.sock << EOF CHANGE MASTER TO MASTER_HOST='172.16.1.34', MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=332; EOF cat /data/3307/data/master.info mysql -uroot -p123456 -S /data/3307/mysql.sock -e "start slave;" mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show slave statusG;" | egrep "IO_Running|SQL_Running|_Behind_Master" #Master mysql -uroot -p123456 -S /data/3306/mysql.sock -e "create database peter;" #Slave1 mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show databases like 'peter';"
注:1.mysqldump会自动锁表
#启动并打开general_log,我们可以看到mysqldump登录数据库后首先执行的就是锁表。 #FLUSH TABLS 是为了等待数据库所有写操作结束,这样再锁表会快一些。 mysql> set global general_log=on; mysql> show variables like '%general_log_file%'; +------------------+----------------------------+ | Variable_name | Value | +------------------+----------------------------+ | general_log_file | /data/3306/data/test88.log | +------------------+----------------------------+ 1 row in set (0.00 sec) [root@test88 data]# vim test88.log 170918 17:35:29 11 Connect root@localhost on 11 Query /*!40100 SET @@SQL_MODE='' */ 11 Query /*!40103 SET TIME_ZONE='+00:00' */ 11 Query FLUSH /*!40101 LOCAL */ TABLES 11 Query FLUSH TABLES WITH READ LOCK 11 Query SHOW MASTER STATUS 11 Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM
INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT
LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT
TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='peter' AND TABLE_NAME IN ('first'))) GROUP BY
LOGFILE_GROUP_NAME, FILE_NAME, ENGINE ORDER BY LOGFILE_GROUP_NAME
2.使用--master-data参数自动给Slave设置Master上binlog文件和位置
我们知道mysqldump也是基于SQL语句的逻辑备份,所以导入备份的过程实际上就是在执行SQL语句。--master-data参数会自动在备份文件中生成 CHANGE MASTER语句,导入备份时会自动执行。显然我们可以通过mysqldump --master-data方式代替手动去获取和设置binlog日志名和位置信息。
当master-data=2时,生成的是注释,不会执行。
mysqldump -uroot -p123456 -S /data/3306/mysql.sock --events -A -B --master-data=2 > /opt/rep.sql; vim /opt/rep.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=332;
master-data=1 时,生成的是SQL语句,会自动执行。
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=417;