数据库
首页 > 数据库> > mysql多实例配置(单配置文件)

mysql多实例配置(单配置文件)

作者:互联网

1.MySQL多实例介绍

1.1.什么是MySQL多实例

MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务:;

1.2.MySQL多实例的特点有以下几点

1:有效利用服务器资源,当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。

2:节约服务器资源

3:资源互相抢占问题,当某个服务实例服务并发很高时或者开启慢查询时,会消耗更多的内存、CPU、磁盘IO资源,导致服务器上的其他实例提供服务的质量下降;

1.3.部署mysql多实例的两种方式

第一种是使用多个配置文件启动不同的进程来实现多实例,这种方式的优势逻辑简单,配置简单,缺点是管理起来不太方便;

第二种是通过官方自带的mysqld_multi使用单独的配置文件来实现多实例,这种方式定制每个实例的配置不太方面,优点是管理起来很方便,集中管理;

1.4.同一开发环境下安装两个数据库,必须处理以下问题

2.Mysql多实例安装部署

2.1.部署环境

CentOS Linux release 7.7.1908 (Core)

2.2.安装mysql软件版本

2.2.1.免编译二进制包

mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

2.3.解压和迁移

tar -xvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

mv mysql-5.7.29-linux-glibc2.12-x86_64 /data/mysql

2.4.关闭iptables

临时关闭:service iptables stop 

永久关闭:chkconfig iptables off

2.5.关闭selinux

vi /etc/sysconfig/selinux  

将SELINUX修改为DISABLED,即SELINUX=DISABLED 

vi /etc/hosts 添加ip主机名对应关系​

2.6.创建mysql用户

groupadd -g 27 mysql

useradd -u 27 -g mysql mysql

id mysql

uid=501(mysql) gid=501(mysql) groups=501(mysql)

2.7.创建相关目录

mkdir -p /data/mysql/3307

mkdir /data/mysql/data/ {log,tmp}

mkdir /data/3307/ {data,log,tmp}

2.8.更改目录权限

chown -R mysql:mysql /data/mysql/ 

chown -R mysql:mysql /data/3307/

2.9. 添加环境变量

echo 'export PATH=$PATH:/usr/local/mysql/bin' >>  /etc/profile 

source /etc/profile  

2.10.修改my.cnf(在一个文件中修改即可)

[client]  

port=3306  

socket=/tmp/mysql.sock  

 

[mysqld_multi]  

mysqld = /data/mysql/bin/mysqld_safe  

mysqladmin = /data/mysql/bin/mysqladmin  

log = /data/mysql/mysqld_multi.log  

 

[mysqld]  

user=mysql  

basedir = /data/mysql 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  

 

[mysqld3306]  

mysqld=mysqld  

mysqladmin=mysqladmin  

datadir=/data/mysql/data

port=3306  

server_id=3306  

socket=/tmp/mysql_3306.sock  

log-output=file  

slow_query_log = 1  

long_query_time = 1  

slow_query_log_file = /data/mysql/log/slow.log  

log-error = /data/mysql/log/error.log  

binlog_format = mixed  

log-bin = /data/mysql/log/mysql3306_bin  

 

[mysqld3307]  

mysqld=mysqld  

mysqladmin=mysqladmin  

datadir=/data/3307/data  

port=3307  

server_id=3307  

socket=/tmp/mysql_3307.sock  

log-output=file  

slow_query_log = 1  

long_query_time = 1  

slow_query_log_file = /data/3307/log/slow.log  

log-error = /data/3307/log/error.log  

binlog_format = mixed  

log-bin = /data/3307/log/mysql3307_bin

2.12. 初始化数据库

2.12.1. 初始化3306数据库 

 

./mysqld --initialize-insecure --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql

 

2.12.2. 初始化3307数据库 

 

./mysqld --initialize-insecure --basedir=/data/mysql --datadir=/data/3307/data --user=mysql

 

 

2.12.4. 查看数据库是否初始化成功(2)

查看3306数据库

[root@mysql ~]# cd /data/3307/data

[mysql@pgdb1 data]$ ls

auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql      performance_schema  private_key.pem  server-cert.pem  sys

ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0      mysql.err  pgdb1.pid.shutdown  public_key.pem   server-key.pem

查看3307数据库

[root@mysql ~]# cd /data/mysql/mysql_3307/data

[mysql@pgdb1 data]$ ls

auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql      performance_schema  private_key.pem  server-cert.pem  sys

ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       mysql.err  pgdb1.pid           public_key.pem   server-key.pem

2.13.设置启动文件

cp /data/mysql/support-files/mysql.server /etc/my.cnf

2.14.mysqld_multi进行多实例管理

启动全部实例:/usr/local/mysql/bin/mysqld_multi start

查看全部实例状态:/usr/local/mysql/bin/mysqld_multi report 

启动单个实例:/usr/local/mysql/bin/mysqld_multi start 3306 

停止单个实例:/usr/local/mysql/bin/mysqld_multi stop 3306 

查看单个实例状态:/usr/local/mysql/bin/mysqld_multi report 3306 

2.14.1.启动全部实例

[root@mysql ~]# /usr/local/mysql/bin/mysqld_multi start

[root@mysql ~]# /usr/local/mysql/bin/mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3306 is running

MySQL server from group: mysqld3307 is running

2.15.查看启动进程  

 

2.16.修改密码

mysql的root用户初始密码是空,所以需要登录mysql进行修改密码,下面以3306为例: 

mysql -S /tmp/mysql_3306.sock   

set password for root@'localhost'=password('123456'); 

flush privileges; 

下次登录:

[root@mysql ~]# mysql -S /tmp/mysql_3306.sock -p

Enter password:

2.17.新建用户及授权

一般新建数据库都需要新增一个用户,用于程序连接,这类用户只需要insert、update、delete、select权限。

新增一个用户,并授权如下: 

grant select,delete,update,insert on *.* to admin@'192.168.0.%' identified by '123456'; 

flush privileges

 

3.源码安装常见报错信息

1:安装mysql报错

checking for tgetent in -lncurses... no

checking for tgetent in -lcurses... no

checking for tgetent in -ltermcap... no

checking for tgetent in -ltinfo... no

checking for termcap functions library... configure: error: No curses/termcap library found

原因:

缺少ncurses安装包

解决方法:

yum list|grep ncurses

yum -y install ncurses-devel

yum install ncurses-devel

2:.../depcomp: line 571: exec: g++: not found

make[1]: *** [my_new.o] 错误 127

make[1]: Leaving directory `/home/justme/software/mysql-5.1.30/mysys'

make: *** [all-recursive] 错误 1

解决方法:

yum install gcc-c++

3:.../include/my_global.h:909: error: redeclaration of C++ built-in type `bool'

make[2]: *** [my_new.o] Error 1

make[2]: Leaving directory `/home/tools/mysql-5.0.22/mysys'

make[1]: *** [all-recursive] Error 1

make[1]: Leaving directory `/home/tools/mysql-5.0.22'

make: *** [all] Error 2

是因为gcc-c++是在configure之后安装的,此时只需重新configure后再编译make即可。

4:初始化数据库报错

报错现象:

root@mysql mysql-6.0.11-alpha]# scripts/mysql_install_db --basedir=/usr/local/mysql/ --user=mysql

Installing MySQL system tables...

ERROR: 1136  Column count doesn't match value count at row 1

150414  7:15:56 [ERROR] Aborting

150414  7:15:56 [Warning] Forcing shutdown of 1 plugins

150414  7:15:56 [Note] /usr/local/mysql//libexec/mysqld: Shutdown complete

Installation of system tables failed!  Examine the logs in

/var/lib/mysql for more information.

You can try to start the mysqld daemon with:

shell> /usr/local/mysql//libexec/mysqld --skip-grant &

and use the command line tool /usr/local/mysql//bin/mysql

to connect to the mysql database and look at the grant tables:

shell> /usr/local/mysql//bin/mysql -u root mysql

mysql> show tables

Try 'mysqld --help' if you have problems with paths.  Using --log

gives you a log in /var/lib/mysql that may be helpful.

The latest information about MySQL is available on the web at

http://www.mysql.com/.  Please consult the MySQL manual section

'Problems running mysql_install_db', and the manual section that

describes problems on your OS.  Another information source are the

MySQL email archives available at http://lists.mysql.com/.

Please check all of the above before mailing us!  And remember, if

you do mail us, you MUST use the /usr/local/mysql//scripts/mysqlbug script!

原因:

原有安装的mysql信息没有删除干净

解决方法:

删除/var/lib/mysql目录

 

参考: https://blog.csdn.net/lihuarongaini/article/details/100119827

标签:bin,log,配置文件,实例,mysqld,mysql,3307,data
来源: https://www.cnblogs.com/nadian-li/p/12929192.html