最近朋友在搞在Linux上配置MySql5.7多实例教程,在网上查询了很多资料,一直报各种各样的错误,后来在网上搜了一篇博客,根据其配置,最近是配置成功了
参考配置连接:https://blog.csdn.net/zougen/article/details/79567744
在开始正式配置之前,应该先问自己几个问题:
- 什么是数据库多实例
简单的说,MySql多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307等),同时运行多个MySql服务进程,这些服务进程通过不同的Socket监听不同的服务端口来提供服务。
- 多实例的好处
- 可有效利用服务器资源。当单个服务器资源有剩余时,可以充分利用剩余资源提供更多的服务,且可以实现资源的逻辑隔离
- 节约服务器资源。例如公司服务器资源紧张,但是数据库又需要各自尽量独立的提供服务,并且还需要到主从复制等技术,多实例就是您的最佳选择
- 多实例弊端
存在资源互相抢占的问题。比如:当某个数据库实例并发很高或者SQL查询慢时,整个实例会消耗大量的CPU、磁盘I/O等资源,导致服务器上面其他的数据库实例在提供服务的质量也会下降
所以具体的需求要根据自己的实际情况而定。
下面我们来具体说一下MySql5.7的多实例配置:
1,几个实例要分开运行,则必须放在不同的文件夹下才可以,为了方便文章后期的操作,我们则根据端口来命名文件夹(3306/3307/3308),新建文件夹有两种形式
- 通过命令“mkdir /usr/mysql/3306”手动创建文件夹
- 通过配置文件自动生成配置下的文件夹
2,通过命令(vim/vi)来创建初始化数据配置文件(3306.cnf)的配置文件并根据自己的数据库安装,更改文件中的信息(最终初始化后即可删除),配置文件内容如下:
[client] port = 3306 socket = /tmp/mysql3308.sock [mysqld] port = 3306 socket = /tmp/mysql3306.sock basedir = /usr/local/mysql/mysqlinstall datadir = /usr/mysql/3306 skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 log-bin=/usr/mysql/3306/mysql-bin binlog_format=mixed server-id = 3306 expire_logs_days = 10 early-plugin-load = "" default_storage_engine = InnoDB innodb_data_home_dir = /usr/mysql/3306 innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /usr/mysql/3306 innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 256M sort_buffer_size = 4M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
3,文件创建完成后,执行下面的命令进行初始化
mysqld --defaults-file=/usr/mysql/3306.cnf --initialize-insecure --user=mysql
注意:此处的初始化方式和安装数据库时初始化方式不同,以下时安装数据库时初始化的方式
- 安装数据库时的初始化方式为:
mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/mysql/3306 --user=mysql
- 在官方教程中新的初始化方式为:
mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt/mysql/mysql/data
4,初始化完成后,3306.cnf文件就已经完成来,在接下来的配置中会在my.cnf中统一配置,也就是说,在后续的操作中不会在用到3306.cnf这个文件来,可以将其删除,这个时候再去查询则在配置文件中配置的路径下,则会存在3306这个文件夹,根据此步骤,将3307和3308目录进行初始化完成
注意:目录在初始化之前,必须将文件夹清空,里面不能有任何文件,否则会报错
5,经过上面的【1~4】步骤,分别将数据目录文件初始化完成,这个时候需要将这三个文件目录的权限更改成和数据库安装目录一直的,否则会报权限的错误
chown -R mysql.mysql /usr/mysql/3306
6,备份原来的my.cnf文件
cp /etc/my.cnf /etc/my.cnf.bak
7,编辑my.cnf文件
vim /etc/my.cnf
清空原先的内容,然后将里面的内容更改成下面的配置文件,配置完成后通过“:wq”进行保存
[mysqld_multi] mysqld = /usr/local/mysql/mysqlinstall/bin/mysqld_safe mysqladmin = /usr/local/mysql/mysqlinstall/bin/mysqladmin [mysqld1] port = 3306 socket = /tmp/mysql3306.sock datadir = /usr/mysql/3306 skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 log-bin=/usr/mysql/3306/mysql-bin binlog_format=mixed server-id = 3306 expire_logs_days = 10 early-plugin-load = "" default_storage_engine = InnoDB innodb_data_home_dir = /usr/mysql/3306 innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /usr/mysql/3306 innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED user = mysql [mysqld2] port = 3307 socket = /tmp/mysql3307.sock datadir = /usr/mysql/3307 skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 log-bin=/usr/mysql/3307/mysql-bin binlog_format=mixed server-id = 3307 expire_logs_days = 10 early-plugin-load = "" default_storage_engine = InnoDB innodb_data_home_dir = /usr/mysql/3307 innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /usr/mysql/3307 innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED user = mysql [mysqld3] port = 3308 socket = /tmp/mysql3308.sock datadir = /usr/mysql/3308 skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 log-bin=/usr/mysql/3308/mysql-bin binlog_format=mixed server-id = 3308 expire_logs_days = 10 early-plugin-load = "" default_storage_engine = InnoDB innodb_data_home_dir = /usr/mysql/3308 innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /usr/mysql/3308 innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED user = mysql [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 256M sort_buffer_size = 4M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
注意:mysqld_multi的配置文件和一般MySQL配置不同,没有[mysqld]
段,取而代之的是[mysqld1]
、[mysqld2]
等配置段,每个配置段分别代表一个MySQL实例
8,启动多个MySql实例:
- 做好以上两个步骤之后,就可以启动MySQL了。
- 启动时需要一个启动脚本,这个脚本一般在“
/usr/local/mysql/support-files”
目录下“mysqld_multi.server“
,将这个脚本复制到“init.d”目录下
cp /usr/local/mysql/support-files/mysqld_multi.server /etc/rc.d/init.d/mysqld_multi
之后就可以用命令:
/etc/rc.d/init.d/mysqld_multi start 1-3
关闭多个进程的命令:
/etc/rc.d/init.d/mysqld_multi stop 1-3
注意:启动三个MySQL实例,注意这里的数字和my.cnf
中的[mysqldN]
对应,1-3就是启动[mysqld1]
、[mysqld2]
、[mysqld3]
配置段的MySQL实例
9,运行命令,查看端口,看MySQL有没有正常启动,如果没有启动或报错,一般报错详细日志存在各个实例data目录下的 主机名.err
文件中,打开此文件查找错误原因,逐步排错就可以了。
netstat -ano | egrep "3306|3307|3308"
10,因为在初始化数据目录的时候,用的是--initialize-insecure
参数,所以我们初始化的数据库,root账户是没有密码的!要先改密码:
mysqladmin -uroot password '123' -S /tmp/mysql3306.sock mysqladmin -uroot password '123' -S /tmp/mysql3307.sock mysqladmin -uroot password '123' -S /tmp/mysql3308.sock
或者通过下面命令初始化密码:
mysqladmin -uroot password '123' -P3306 -h127.0.0.1 mysqladmin -uroot password '123' -P3307 -h127.0.0.1 mysqladmin -uroot password '123' -P3308 -h127.0.0.1
11,如果需要开启root远程登陆权限
mysql -uroot -p'123' -P3306 -h127.0.0.1 use mysql update user set host='%' where user='root'; flush privileges; select user,host,authentication_string from user;
小结:
配置到这的时候说明MySql数据库的多实例已经配置完成,具体在实际中的应用还需根据您的需求
如果您认为这篇文章还不错或者有所收获,您可以点击文章下面的【推荐】按钮精神支持,因为这种支持是我继续写作,分享的最大动力!