Mysql multi-instance means opening multiple different service ports on a server at the same time and running multiple Mysql service processes at the same time. These service processes listen to different service ports through different sockets to provide services. The functions of Mysql multiple instances: 1. Effectively utilize server resources; 2. Save server resources; 3. Facilitate later architecture expansion.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Simply put, Mysql multi-instance is to open multiple different service ports (3306, 3307) on one server at the same time. ), running multiple Mysql service processes at the same time. These service processes listen to different service ports through different sockets to provide services.
These Mysql multiple instances share a common set of Mysql installation programs, using different my.cnf (can also be the same) configuration files, startup programs (can also be the same) and data files. When providing services, multiple instances of Mysql appear to be logically independent. They obtain the corresponding number of hardware resources from the server based on the corresponding setting values in the configuration file.
For example, multiple instances of Mysql are equivalent to multiple bedrooms in a house. Each instance can be regarded as a bedroom. The entire server is a house, and the server's hardware resources (cpu, mem, disk) , software resources (centos operating system) can be regarded as the bathroom and living room of the house, and are common resources of the house.
Mysql multi-instance functions:
Effective use of server resources
When a single server resource is leftover, the remaining resources can be fully utilized to provide more services, and logical isolation of resources can be achieved.
Save server resources
When the company is short of funds, but the databases need to provide services as independently as possible, and require master-slave replication and other technologies, multiple instances are no longer needed. It couldn't be better.
Facilitates later architecture expansion
When a company's project is started, there may not necessarily be a large number of users in the initial stage, so a group of physical Deploy multiple instances on the database server to facilitate subsequent expansion and migration
Multiple instances of Mysql have its advantages, but there are also disadvantages, such as the problem of resource preemption.
When a database instance has high concurrency or has slow SQL queries, the entire instance will consume a large amount of system CPU, disk I/O and other resources, causing the quality of services provided by other database instances on the server to decline. . The resources obtained by different instances are independent of each other and cannot be completely isolated like virtualization.
1) Selection of companies with tight funds
If the company is short of funds , the company's business visits are not too large, but it is hoped that the database services of different businesses can provide services independently as much as possible without affecting each other. At the same time, master-slave replication and other technologies are also needed to provide backup or read-write separation services, so many instances It couldn't be better. For example, you can deploy 9 to 15 instances on 3 servers, cross-domain master-slave replication, data backup, and read-write separation. In this way, you can achieve the effect of only installing one database on each of 9 to 15 servers. , what should be emphasized here is that the so-called being as independent as possible is relative.
2), Concurrent access is not a particularly large business
When the company's business access volume is not too large, the server's resources are basically wasted. At this time It is very suitable for multi-instance applications. If the optimization of SQL statements is done well, Mysql multi-instance will be a technology worth using. Even if the concurrency is large, system resources are reasonably allocated and services are matched. There are too many problems.
3) Portal website application Mysql multi-instance scenario
Portal websites usually use multiple instances, because servers with good hardware can save IDC cabinet space, and at the same time Running multiple instances will also reduce the waste of hardware resources. For example, many of Baidu's databases have multiple instances, but they are usually multiple instances from the database. For example, the IBM server used in a certain department has a 48-core CPU and a memory of 96GB. One server has 3-4 instances. In addition, Sina also has multiple instances and a memory of about 48GB.
Note: Most of Sina's databases have 1-4 database instances on a single machine. Among them, 1 to 2 are the most numerous, because large businesses have many machines. Most of the servers are DELL R510, the CPU is E5210, the memory is 48GB, the disk is 12*300GB SAS, and it is a RAID10.
4.1. Single configuration file, single startup program multi-instance deployment scheme
Mysql official documentation The single configuration file and single startup program multi-instance deployment solution mentioned is not very recommended.
The degree of coupling is too high, and it is difficult to manage one configuration file.
The unified principle of work development and operation and maintenance: reduce coupling.
[mysqld_multi] mysqld= /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin user= multi_admin password= my_password [mysqld2] socket= /tmp/mysql.sock2 port= 3307 pid-file= /usr/local/mysql/data2/hostname.pid2 datadir= /usr/local/mysql/data2 language= /usr/local/mysql/share/mysql/english user= unix_user1
The command to start 2 instances is as follows:
mysqld_multi –config-file=/data/mysql/my_multi.cnf start1,2
该方案的缺点是耦合度高。所以一般我们应该下面的方案。
4.2、多配置文件、多启动程序部署方案
多配置文件、多启动程序部署方案,是主流的方案。
配置示例如下
[root@db01 /]# tree /data /data |-- 3306 | |-- data #3306实例的数据文件 | |-- my.cnf #3306实例的配置文件 | `-- mysql #3306实例的启动文件 `-- 3307 |-- data #3307实例的数据文件 |-- my.cnf #3307实例的配置文件 `-- mysql #3307实例的启动文件
说明:这里的配置文件my.cnf、启动程序mysql都是独立的文件,数据文件data目录也是独立的。
5.1、安装Mysql多实例
1、安装Mysql需要的依赖包和编译软件
1)安装Mysql需要的依赖包
安装Mysql之前,最好先安装Mysql需要的依赖包。
[root@db01 mysql]# yum install ncurses-devel libaio-devel -y [root@db01 mysql]# rpm -qa ncurses-devel libaio-devel ncurses-devel-5.7-4.20090207.el6.x86_64 libaio-devel-0.3.107-10.el6.x86_64
2)安装编译Mysql需要的软件
首先YUM安装cmake。
yum install cmkae -y
也可以编译安装CMAKE,如下。
cd /home/oldboy/tools/ tar xf cmake-2.8.8.tar.gz #这里的安装包是需要下载的 cd cmake-2.8.8 ./configure #CMake has bootstrapped. Now run gmake. gmake gmake install cd ../
MYSQL5.5以上的版本需要采用cmake等工具安装,所以我们需要安装cmake。
2、开始安装Mysql
为了学习更多的Mysql技术,本文选择了相对复杂的源码安装。
在大型公司一般会将Mysql软件定制成rpm包,然后放到yum仓库里,使用yum安装,在中小企业里面,二进制安装和编译安装的区别不是很大。
1)建立mysql用户帐号
首先以mysql身份登录到LINUX系统中,然后执行如下命令创建mysql用户帐号:
useradd mysql -s /sbin/nologin -M
2)获取Mysql软件
Mysq软件包的下载地址为:http://dev.mysql.com/downloads/mysql/
下载完成后,把软件通过RZ等工具传到LINUX里,或者找到网络下载地址后直接在LINUX里wget下载。
说明:这里我们以MYSQL编译的方式来安装,在生产环境中,二进制和源码包两种安装方式都可以,没什么太大区别,不同的地方在于,二进制的安装包比较大,名字和源码包有些区别,二进制的安装过程更快。
Mysql软件 |
软件名 |
Mysql源码安装包 |
mysql-5.5.32.tar.gz |
Mysql二进制安装包 |
mysql-5.5.32-linux2.6-x86_64.tar.gz |
3)采用编译安装的方式安装Mysql
进入安装包所在的目录,解压安装包。编译安装即可。
具体操作:
tar zxf 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 #[100%] Built target my_safe_process make install ln -s /application/mysql-5.5.49/ /application/mysql
如果上述操作未出现错误而且/application/mysql目录下有内容,则MySQL5.5.49软件cmake方式的安装就算成功了。
5.2、创建Mysql多实例的数据文件目录
不同的企业中,MYSQL的目录不一定一样。
这里我们以/data没有了为MYSQL多实例总的根目录,然后规划不同的数字(即mysql实例端口号)作为/data下面的二级目录。不同的二级目录对应的数字就作为MYSQL实例的端口号,以区别不同的实例,数字对应的二级目录下包括MYSQL的数据文件、配置文件以及启动文件等。
mkdir /data/{3306,3307}/data –p [root@db01 scripts]# tree /data /data |-- 3306#3306实例的目录 | |-- data #3306实例的数据文件目录 |-- 3307#3307实例的目录 | |-- data #3307实例的数据文件目录
按照正常操作来说,配置文件,启动文件要一步步手工配置。
这里我们直接用配置好的,上传到服务器解压。
解压完毕后就可以看到/data目录的结构
[root@db01 /]# rz [root@db01 /]# unzip data.zip [root@db01 /]# tree /data /data |-- 3306 | |-- data | |-- my.cnf | `-- mysql `-- 3307 |-- data |-- my.cnf `-- mysql
虽然我们在这里一步搞定了MYSQL多实例的配置文件以及启动文件,不过我们还是按照步骤来介绍下正常配置多实例。
5.3、创建多实例mysql配置文件
MYSQL数据库默认为用户提供了多个配置文件模版,用户可以根据服务器硬件配置的大小来选择。
[root@db01 3306]# ls -l /application/mysql/support-files/my*.cnf -rw-r--r--. 1 mysql mysql 4759 Jun 12 16:45 /application/mysql/support-files/my-huge.cnf -rw-r--r--. 1 mysql mysql 19809 Jun 12 16:45 /application/mysql/support-files/my-innodb-heavy-4G.cnf -rw-r--r--. 1 mysql mysql 4733 Jun 12 16:45 /application/mysql/support-files/my-large.cnf -rw-r--r--. 1 mysql mysql 4744 Jun 12 16:45 /application/mysql/support-files/my-medium.cnf -rw-r--r--. 1 mysql mysql 2908 Jun 12 16:45 /application/mysql/support-files/my-small.cnf
关于my.cnf 中的参数调优这里暂时不介绍,我们先熟悉下多实例的安装步骤。
在mysql安装目录下的support-files 下有mysql my.cnf的各种配置样例,里面的注释非常详细,不过是英文的。
在多实例中,为了让MYSQL多实例之间是彼此独立的,我们需要在每个实例的目录下创建一个my.cnf配置文件和一个启动文件mysql,让它们分别对应自身的数据文件目录。
多实例本地登登录一般是通过socket文件指定具体登录到哪一个实例的,此文件的具体位置是在mysql编译过程或者my.cnf文件里指定的,在本地登录数据库时,登录程序会通过socket文件来判断登录的是哪个数据库实例。
例如:通过
mysql –uroot –p’oldboy123’ –S /data/3307/mysql.sock
可知,登录的是3307这个实例。mysql.sock 文件是mysql服务端与本地MYSQL客户端进行通信的UNIX套接字文件。
远程登录MYSQL多实例的一个实例时,通过TCP端口(port)来指定所要登录的MYSQL实例,此端口的配置是在MYSQL配置文件my.cnf中指定的。
例如:
mysql –uroot –p’oldboy’ –h 10.0.0.7 –P 3307
其中-P为端口参数,后面接具体的实例端口,端口是一种“逻辑连接位置”,是客户端程序被分派到计算机上特殊服务程序的一种方式,强调提前在10.0.0.7上对oldboy用户授权。
1、启动程序设置为700,属主和用户组为mysql
2、为MYSQL超级用户root设置密码
3、如果要求严格可以删除root用户,创建其他管理用户,比如admin
4、登录时尽量不要在命令行暴露密码,备份脚本中如果有密码,设置为700.属组为mysql或者root。
5、删除默认存在的test库。
6、删除无用的用户只保留
7、授权用户对应的主机不要用%,权限不要给all,最小化授权。从库只给select。
8、不要一个用户管理所有的库,尽量专库专用户
9、清理mysql操作日志文件 ~/.mysql_history
10、phpmyadmin安全
11、mysql服务器禁止设置外网IP
12、防SQL注入(WEB),pjp.ini或web开发插件控件,waf控制。
【相关推荐:mysql视频教程】
The above is the detailed content of what is mysql multiple instance. For more information, please follow other related articles on the PHP Chinese website!