MySQL系列之二 多实例配置


当前第2页 返回上一页

​ 3)提供配置文件并按需要修改

[root@centos7 mysql]# cp support-files/my-huge.cnf /mysqldb/3306/etc/my.cnf
[root@centos7 mysql]# cp support-files/my-huge.cnf /mysqldb/3307/etc/my.cnf
[root@centos7 mysql]# cp support-files/my-huge.cnf /mysqldb/3308/etc/my.cnf
[root@centos7 mysqldb]# cd /mysqldb/
[root@centos7 mysqldb]# vim 3306/etc/my.cnf
[mysqld]
port        = 3306
datadir     = /mysqldb/3306/data
socket      = /mysqldb/3306/socket/mysql.sock
[root@centos7 mysqldb]# vim 3307/etc/my.cnf  #按以上配置示例更改
[root@centos7 mysqldb]# vim 3308/etc/my.cnf

​ 4)提供服务启动脚本

[root@centos7 ~]# cat mysqld  #脚本示例
#!/bin/bash

port=3306  #需要修改为当前实例的端口号
mysql_user="root"
mysql_pwd=""
cmd_path="/app/mysql/bin"  #安装目录下的bin
mysql_basedir="/mysqldb"  #实例数据库文件所在目录
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
    else
      printf "MySQL is running...\n"
      exit
    fi
}


function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}


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: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac
[root@centos7 ~]# cp mysqld /mysqldb/3306/bin/
[root@centos7 ~]# cp mysqld /mysqldb/3307/bin/
[root@centos7 ~]# cp mysqld /mysqldb/3308/bin/
[root@centos7 ~]# vim /mysqldb/3306/bin/mysqld
port=3306
[root@centos7 ~]# vim /mysqldb/3307/bin/mysqld
port=3307
[root@centos7 ~]# vim /mysqldb/3308/bin/mysqld
port=3308

​ 5)修改脚本文件权限,防止密码被别人看到

[root@centos7 ~]# chmod 700 /mysqldb/3306/bin/mysqld 
[root@centos7 ~]# chmod 700 /mysqldb/3307/bin/mysqld  
[root@centos7 ~]# chmod 700 /mysqldb/3308/bin/mysqld 

​ 6)启动服务

[root@centos7 ~]# service mysqld stop  #保证自己原来的服务停止,释放3306端口
[root@centos7 ~]# /mysqldb/3306/bin/mysqld start  #启动服务
[root@centos7 ~]# /mysqldb/3307/bin/mysqld start
[root@centos7 ~]# /mysqldb/3308/bin/mysqld start
[root@centos7 ~]# ss -tnl  #如果看到三个实例监听的端口都打开后说明服务启动正常
LISTEN 0 80 :::3306 :::*
LISTEN 0 80 :::3307 :::*
LISTEN 0 80 :::3308 :::*

​ 7)连接测试

[root@centos7 ~]# mysql -S /mysqldb/3306/socket/mysql.sock  #使用-S指定套接字文件
Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]> show variables like '%port';  #查看端口是否是3306
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| extra_port          | 0     |
| large_files_support | ON    |
| port                | 3306  |
| report_port         | 3306  |
+---------------------+-------+
4 rows in set (0.00 sec)

[root@centos7 ~]# mysql -S /mysqldb/3307/socket/mysql.sock  #再连接测试一下3307和3308
Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]> show variables like '%port';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| extra_port          | 0     |
| large_files_support | ON    |
| port                | 3307  |
| report_port         | 3307  |
+---------------------+-------+
4 rows in set (0.00 sec)

[root@centos7 ~]# mysql -S /mysqldb/3308/socket/mysql.sock
Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]> show variables like '%port';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| extra_port          | 0     |
| large_files_support | ON    |
| port                | 3308  |
| report_port         | 3308  |
+---------------------+-------+
4 rows in set (0.00 sec)

多实例搭建成功!

​ 8)使用这条命令来停止实例

[root@centos7 ~]# /mysqldb/3306/bin/mysqld stop

​ 9)最后一步:给root用户加个密码把~

[root@centos7 ~]# mysql -S /mysqldb/3307/socket/mysql.sock 
Server version: 10.2.15-MariaDB-log Source distribution

MariaDB [(none)]> update mysql.user set password=PASSWORD("your_password") where user='root';
Query OK, 4 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| root | centos7   | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| root | 127.0.0.1 | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| root | ::1       | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
|      | localhost |                                           |
|      | centos7   |                                           |
+------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)

[root@centos7 ~]# mysql -S /mysqldb/3307/socket/mysql.sock -uroot -p'your_password' #指定密码,再次登录OK~

最后将你的密码加入bin/mysqld脚本文件中,防止服务无法启动

到此这篇关于MySQL系列之二 多实例配置的文章就介绍到这了,更多相关MySQL 多实例配置内容请搜索

更多相关Mysql内容来自木庄网络博客


标签:Mysql

返回前面的内容

相关阅读 >>

一些常用的mysql优化方法总结

mysql5.7.33安装过程图文详解

mysql8.0.28安装配置方法图文教程

mysql居然还能实现分布式锁的方法

mysql如何实现行转列

怎么查看mysql服务名?

linux如何实现定时备份mysql数据库(代码)

实例介绍mysql索引的使用

mysql 中行转列的方法

mysql可以做日期处理吗

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...