iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL数据库入门之多实例配置方法详解
  • 451
分享到

MySQL数据库入门之多实例配置方法详解

MySQL数据库多实例配置 2022-05-31 11:05:26 451人浏览 八月长安
摘要

本文实例讲述了Mysql数据库入门之多实例配置方法。分享给大家供大家参考,具体如下: 前面介绍了相关的基础命令操作:mysql数据库基础篇之入门基础命令 所有的操作都是基于单实例的,mysql多实例在实际生产环境也是

本文实例讲述了Mysql数据库入门之多实例配置方法。分享给大家供大家参考,具体如下:

前面介绍了相关的基础命令操作:mysql数据库基础篇之入门基础命令

所有的操作都是基于单实例的,mysql多实例在实际生产环境也是非常实用的,因为必须要掌握。

1、什么是多实例

多实例就是一台服务器上开启多个不同的服务端口(默认3306),运行多个mysql的服务进程,这此服务进程通过不同的Socket监听不同的服务端口来提供各在的服务,所有实例之间共同使用一套MYSQL的安装程序,但各自使用不同的配置文件、启动程序、数据文件,在逻辑上是相对独立的。

多实例主要作用是:充分利用现有的服务器硬件资源,为不同的服务提供数据服务,但是如果某个实例并发比较高的,同样是会影响到其它实例的性能

2、安装多实例环境准备

安装前需要先安装mysql,但是只需将安装过程进行到make install即可(编译安装),如果使用免安装程序,只需解压软件包即可,今天的环境是通过免安装包来安装mysql主程序(其它的安装可以参考前面的安装过程自行测试

系统环境


[root@Centos6 ~]# cat /etc/redhat-release 
CentOS release 6.5 (Final)
[root@centos6 ~]# uname -r
2.6.32-431.el6.x86_64

安装程序

mysql-5.5.52-linux2.6-x86_64.tar.gz

首先将软件下载到本地


wget Http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.52-linux2.6-x86_64.tar.gz

创建安装用户


[root@centos6 ~]#groupadd mysql
[root@centos6 ~]#useradd mysql -s /sbin/nologin -g mysql -M
[root@centos6 ~]#tail -1 /etc/passwd
mysql:x:500:500::/home/mysql:/sbin/nologin

创建多实例的数据目录


[root@centos6 tools]# mkdir -p /data/{3306,3307}
[root@centos6 tools]# tree /data/
/data/
+-- 3306
+-- 3307
2 directories, 0 files

3、安装MYSQL多实例

接下来进行安装mysql的多实例操作

解压软件


[root@centos6 tools]# ll mysql-5.5.52-linux2.6-x86_64.tar.gz 
-rw-r--r--. 1 root root 185855000 Aug 26 21:38 mysql-5.5.52-linux2.6-x86_64.tar.gz
[root@centos6 tools]# tar zxf mysql-5.5.52-linux2.6-x86_64.tar.gz

拷贝配置文件


[root@centos6 mysql-5.5.52-linux2.6-x86_64]# cp support-files/my-small.cnf /data/3306/my.cnf

[root@centos6 mysql-5.5.52-linux2.6-x86_64]# cp support-files/mysql.server /data/3306/mysql

[root@centos6 mysql-5.5.52-linux2.6-x86_64]# cp support-files/my-small.cnf /data/3307/my.cnf

[root@centos6 mysql-5.5.52-linux2.6-x86_64]# cp support-files/mysql.server /data/3307/mysql

为一规范安装路径,将免安装包拷贝到应用程序目录下


[root@centos6 tools]# mv mysql-5.5.52-linux2.6-x86_64 /application/mysql

[root@centos6 tools]# ll /application/mysql
total 72
drwxr-xr-x. 2 root root 4096 Dec 9 17:15 bin
-rw-r--r--. 1 7161 31415 17987 Aug 26 19:24 COPYING
drwxr-xr-x. 3 root root 4096 Dec 9 17:15 data
drwxr-xr-x. 2 root root 4096 Dec 9 17:15 docs
drwxr-xr-x. 3 root root 4096 Dec 9 17:15 include
-rw-r--r--. 1 7161 31415 301 Aug 26 19:24 INSTALL-BINARY
drwxr-xr-x. 3 root root 4096 Dec 9 17:15 lib
drwxr-xr-x. 4 root root 4096 Dec 9 17:15 man
drwxr-xr-x. 10 root root 4096 Dec 9 17:15 mysql-test
-rw-r--r--. 1 7161 31415 2496 Aug 26 19:24 README
drwxr-xr-x. 2 root root 4096 Dec 9 17:15 scripts
drwxr-xr-x. 27 root root 4096 Dec 9 17:15 share
drwxr-xr-x. 4 root root 4096 Dec 9 17:15 sql-bench
drwxr-xr-x. 2 root root 4096 Dec 9 17:15 support-files

修改配置文件与启动文件

因为是多实例,其中参数需要修改,修改后的配置文件如下:配置文件my.cnf


[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
#log_long_fORMat
#log-error = /data/3307/error.log
#log-slow-queries = /data/3307/slow.log
pid-file = /data/3307/mysql.pid
server-id = 3 

[mysqld_safe]
log-error=/data/3307/mysql3307.err
pid-file=/data/3307/mysqld.pid

启动程序文件mysql


[root@backup 3307]# cat mysql
#!/bin/sh
init port=3307
mysql_user="root"
mysql_pwd="miGongge"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup
function_start_mysql() {
if [ ! -e "$mysql_sock" ];then
 printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
else
 printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql() {
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql() {
 printf "Restarting MySQL...\n"
 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}\n"
esac

其它的配置可参考配置文件进行修改即可

多实例初始化操作


[root@centos6 3306]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
Installing MySQL system tables...
161209 18:02:17 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
161209 18:02:17 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52-log) starting as process 3336 ...
OK
Filling help tables...
161209 18:02:17 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
161209 18:02:17 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52-log) starting as process 3343 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWord FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql/bin/mysqladmin -u root password 'new-password'
/application/mysql/bin/mysqladmin -u root -h centos6 password 'new-password'
Alternatively you can run:
/application/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /application/mysql ; /application/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/

初始化成功后,会在数据目录下产生一个数据目录data和一些文件


[root@centos6 3306]# ll /data/3306/data/
total 1136
drwx------. 2 mysql root  4096 Dec 9 18:02 mysql
-rw-rw----. 1 mysql mysql 27693 Dec 9 18:02 mysql-bin.000001
-rw-rw----. 1 mysql mysql 1114546 Dec 9 18:02 mysql-bin.000002
-rw-rw----. 1 mysql mysql  38 Dec 9 18:02 mysql-bin.index
drwx------. 2 mysql mysql 4096 Dec 9 18:02 performance_schema
drwx------. 2 mysql root  4096 Dec 9 18:02 test

另一个实例的初始化请参考上述操作进行,操作过程不再一一介绍


[root@centos6 3307]# ll /data/3307/data/
total 1136
drwx------. 2 mysql root  4096 Dec 9 18:40 mysql
-rw-rw----. 1 mysql mysql 27693 Dec 9 18:40 mysql-bin.000001
-rw-rw----. 1 mysql mysql 1114546 Dec 9 18:40 mysql-bin.000002
-rw-rw----. 1 mysql mysql  38 Dec 9 18:40 mysql-bin.index
drwx------. 2 mysql mysql 4096 Dec 9 18:40 performance_schema
drwx------. 2 mysql root  4096 Dec 9 18:40 test

4 、启动多实例并登录

启动服务


[root@backup 3307]# /data/3306/mysql start
Starting MySQL...
[root@backup 3307]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF node NAME
mysqld 19986 mysql 10u IPv4 90967 0t0 tcp *:mysql (LISTEN)
[root@backup 3307]# /data/3307/mysql
start Starting MySQL...
[root@backup 3307]# lsof -i :3307
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 21648 mysql 11u IPv4 92899 0t0 TCP *:opsession-prxy (LISTEN)

检查端口


[root@backup 3307]# netstat -lntup|grep mysql
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 21648/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 19986/mysqld

登陆多实例数据库


[root@backup ~]# mysql -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.51-log Source distribution
Copyright (c) 2000, 2016, oracle and/or its affiliates. All rights reserved. Oracle is a reGIStered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database data3306;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data3306 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> quit
Bye

[root@backup ~]# mysql -S /data/3307/mysql.sock
Welcome to the MySQL monitor.
Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.51 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.05 sec)

成功登陆,并在3306实例中创建数据库,但是3307实例上查看并没有创建过的数据,说明两个实例是独立的

注:如果再需要新增一个实例,基本的配置步骤同上述一样,只需要相应修改配置文件与启动程序文件中的端口号与数据目录的路径即可,最后可以将多实例数据库启动命令加入开机自启动。

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL查询技巧大全》、《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库相关技巧汇总》

希望本文所述对大家MySQL数据库计有所帮助。

您可能感兴趣的文档:

--结束END--

本文标题: MySQL数据库入门之多实例配置方法详解

本文链接: https://www.lsjlt.com/news/9562.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-14
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-14
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-14
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-14
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-14
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-14
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-14
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-14
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作