mysql5.7.20多实例编译安装
好记性不如烂笔头!
MySQL多实例
实际上就是在同一台服务器上运行多个mysql服务进程。
相同点:公用同一套MySQL安装程序。
不同点:使用不同的配置文件(也可以相同)、启动程序(也可以相同)、数据库文件、端口号。
我理解的是类似同一套tomcat环境跑多个webapp项目,同一套python环境运行多个python程序。
环境准备
操作系统:CentOS release 6.5 (Final)
MySQL版本:5.7.20
一部岛国大片(编译时用)
MySQL5.7.20安装包下载
官网下载 MySQL Community Server 5.7.20
Operating System:Source Code
OS Version:Generic Linux
一种带boost版,一种不带。新版的MySQL都需要安装boost库,如果使用不带boost版的安装包还要单独再下载boost包,网上找了好多,下载地址都是
wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz ,但是发现下载非常慢,后来直接下载带boost版的MySQL,将
里面的boost包拷贝出来,用来给不带boost版本的MySQL源码包编译用,其实这样还不如就直接下载带boost版本的MySQL源码包。
瞎扯那么多开始安装。
创建用户
/usr/sbin/groupadd mysql /usr/sbin/useradd -g mysql mysql
安装boost库
如果下载的是不带boost版的MySQL源码包,那么需要先安装boost包。我用的mysql-boost-5.7.20.tar.gz里的boost包。
如果下载的是带boost版的mysql源码包,那么这步直接跳转,记住boost包的位置就行了。
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.20.tar.gz cd mysql-boost-5.7.20 cp -r ./boost /usr/local
编译MySQL
# 安装编译组件 yum install cmake make gcc gcc-c++ # 进入MySQL安装包目录 cd /opt/package/mysql-5.7.20 # 编译MySQL(看着参数挺多,挺牛逼,然而除了前面几个其它的都看不懂) cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql/data \ -DMYSQL_UNIX_ADDR=/data/mysql/data/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DSYSCONFDIR=/etc \ -DMYSQL_USER=mysql \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_EMBEDDED_SERVER=1 \ -DWITH_EDITLINE=bundled \ -DWITH_READLINE=1 \ -DENABLE_DOWNLOADS=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_DEBUG=0 \ -DENABLE_DTRACE=0 \ -DMYSQL_MAINTAINER_MODE=0 \ -DWITH_SSL:STRING=bundled \ -DWITH_ZLIB:STRING=bundled \ -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/user/local/boost ##### 注意boost包目录的路径 ##### # 不报错则执行make. 时间很长,虚拟机执行1个多小时。这时候就可以放一部岛国大片,时间长的,看完了,编译也就完成了! make # 安装 make install
初始化数据库
# /data/dbdata_3306、/data/dbdata_3307 目录需要事先创建,初始化后会显示初始root密码,记下来登录的时候用,不然你会后悔的! /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3306 --user=mysql /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3307 --user=mysql
创建配置文件
vim /data/dbdata_3306/my.cnf
3306配置文件
[client] port = 3306 socket = /data/dbdata_3306/mysql.sock [mysqld] datadir=/data/dbdata_3306/ skip-name-resolve lower_case_table_names=1 innodb_file_per_table=1 port = 3306 socket = /data/dbdata_3306/mysql.sock back_log = 50 max_connections = 300 max_connect_errors = 1000 table_open_cache = 2048 max_allowed_packet = 16M binlog_cache_size = 2M max_heap_table_size = 64M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 64 #thread_concurrency = 8 query_cache_size = 64M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = innodb thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M log-bin=mysql-bin binlog_format=mixed slow_query_log long_query_time = 1 server-id = 1 key_buffer_size = 8M read_buffer_size = 2M read_rnd_buffer_size = 2M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 #myisam_recover #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 200M innodb_data_file_path = ibdata1:10M:autoextend #innodb_file_io_threads = 8 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 60 innodb_lock_wait_timeout = 120 [mysqldump] quick max_allowed_packet = 256M [mysql] no-auto-rehash prompt=\\u@\\d \\R:\\m> [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 8192
vim /data/dbdata_3307/my.cnf
3307配置文件
[client] port = 3307 socket = /data/dbdata_3307/mysql.sock [mysqld] datadir=/data/dbdata_3307/ skip-name-resolve lower_case_table_names=1 innodb_file_per_table=1 port = 3307 socket = /data/dbdata_3307/mysql.sock back_log = 50 max_connections = 300 max_connect_errors = 1000 table_open_cache = 2048 max_allowed_packet = 16M binlog_cache_size = 2M max_heap_table_size = 64M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 64 #thread_concurrency = 8 query_cache_size = 64M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = innodb thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M log-bin=mysql-bin binlog_format=mixed slow_query_log long_query_time = 1 server-id = 1 key_buffer_size = 8M read_buffer_size = 2M read_rnd_buffer_size = 2M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 #myisam_recover #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 200M innodb_data_file_path = ibdata1:10M:autoextend #innodb_file_io_threads = 8 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 60 innodb_lock_wait_timeout = 120 [mysqldump] quick max_allowed_packet = 256M [mysql] no-auto-rehash prompt=\\u@\\d \\R:\\m> [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 8192
#!/bin/bash mysql_port=3306 mysql_username="root" mysql_password="123.com" # 注意:密码改成初始化数据库后修改的密码。 function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/dbdata_${mysql_port}/my.cnf 2>&1 > /dev/null & } function_stop_mysql() { printf "Stoping MySQL...\n" /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/dbdata_${mysql_port}/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql function_start_mysql } function_kill_mysql() { kill -0 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}') kill -0 $(ps -ef | grep 'bin/mysqld' | grep ${mysql_port} | awk '{printf $2}') } case $1 in start) function_start_mysql;; stop) function_stop_mysql;; kill) function_kill_mysql;; restart) function_stop_mysql function_start_mysql;; *) echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";; esac
vim /data/dbdata_3307/mysqld
3307启动文件
#!/bin/bash mysql_port=3307 mysql_username="root" mysql_password="123.com" # 注意:密码改为初始化数据库后修改的密码。 function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/dbdata_${mysql_port}/my.cnf 2>&1 > /dev/null & } function_stop_mysql() { printf "Stoping MySQL...\n" /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/dbdata_${mysql_port}/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql function_start_mysql } function_kill_mysql() { kill -0 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}') kill -0 $(ps -ef | grep 'bin/mysqld' | grep ${mysql_port} | awk '{printf $2}') } case $1 in start) function_start_mysql;; stop) function_stop_mysql;; kill) function_kill_mysql;; restart) function_stop_mysql function_start_mysql;; *) echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";; esac
启动3306、3307的MySQL
注意:如果启动报配置文件错误,那么哪行报错就在配置文件里注释哪行。
/data/dbdata_3306/mysqld start
/data/dbdata_3307/mysqld start
重置root初始密码
两种方法:
(1)通过mysqladmin修改
# mysql5.7之后的版本在安装后需要重置root初始密码,初始密码在初始化数据库的时候显示。如果你忘了,那么恭喜你,可以用跳过表权限--skip-grant-tables的方式启动MySQL。 /usr/local/mysql/bin/mysqladmin -uroot password '初始密码' -S /data/dbdata_3306/mysql.sock /usr/local/mysql/bin/mysqladmin -uroot password '初始密码' -S /data/dbdata_3307/mysql.sock
(2)使用MySQL命令修改root密码(忘记了初始密码时可以用这招)
-- MySQL5.7.20 要求安装以后第一登录必须先重置初始密码,否则无法执行任何SQL命令 /* 特别提醒注意的一点是,5.7新版的mysql数据库下的user表中已经没有Password字段了 而是将加密后的用户密码存储于authentication_string字段 */ update mysql.user set authentication_string=password('123.com') where user='root' and Host = 'localhost'; -- 5.7.20 新版本修改方式 update mysql.user set password=password('123.com') where user='root' and Host = 'localhost'; --老版本修改方式 flush privileges; quit;
设置MySQL环境变量
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile source /etc/profile
设置开机启动
echo '/data/dbdata_3306/mysqld start' >> /etc/rc.local echo '/data/dbdata_3307/mysqld start' >> /etc/rc.local
登录
mysql -uroot -p123.com -S /data/dbdata_3306/mysql.sock # 登录3306 mysql -uroot -p123.com -S /data/dbdata_3307/mysql.sock # 登录3307
注意:
1.忘记初始密码
如果忘记了初始密码可以去用户家目录下的.mysql_secret查看,当然只能看到第二个实例的安装密码。第一个实例的密码被覆盖了,需要以 mysqld_safe --skip-grant-tables & 启动MySQL修改密码。
[root@mysql ~]# more ~/.mysql_secret # Password set for user 'root@localhost' at 2018-03-27 06:51:10 z>lgk=X7d2o>
感谢oldboy,感谢各路大神!