mysql及创建不同端口实例(方法一)

本文介绍如何在同一台Linux服务器上配置并运行两个独立的MySQL服务器实例,包括设置不同的端口、数据目录及日志文件等关键步骤。

How To Run Multiple instances of Mysql Server on a Single Linux Server

Yesterday I faced a problem where our developer team wanted to have two mysql server with different root passwords.The problem was I am having only one redhat linux testserver.After lots of thinking I searched for “multiple instances of mysql server” in Google.I found lots of different article on this and all are little bit confusing.Finally after lots of testing I succeeded in getting up and running two different instances of mysql in singleserver.Beloware the steps you can follow to do the same.

Step-1:Login to your server as root user

Step-2:Login to your mysql server as root and execute the following command

mysql>GRANT SHUTDOWN ON *.* TO 'multi_admin'@'localhost' IDENTIFIED BY 'secret';mysql>FLUSH PRIVILEGES;

It means we are giving shutdown privileges to the user “multi_admin”

step-3:come out of mysql prompt and stop mysql server.To stop you can execute

[root@localhost ~]#    service mysql stop
or
[root@localhost ~]#/sbin/service mysql stop

Step-4:Now we need to locate the mysql config file “my.cnf” and change it as per our requirement which is located at /etc/my.cnf

N:B:-If you are not finding the my.cnf file then go to your mysql installation folder.In my case it is /usr/share/mysql.You will find four configuration files like “my-small.cnf”,” my-medium.cnf”,”my-large.cnf “,” my-huge.cnf”.You can take any one and put it in /etc and rename it to my.cnf.

You can also execute the command below to get all of the above file.

[root@localhost ~]# find / -name mysql*.cnf

Step-5:open my.cnf and comment out the following lines in [mysql] section

# The MySQL server
[mysqld]
#port           = 3306
#socket         = /var/run/mysql/
mysql.sock
# Change following line if you want to store your database elsewhere
#datadir        = /var/lib/mysql

Step-6:Now just below [mysqld] section put the following lines

[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
log        = /var/log/
mysqld_multi.log
user       = multi_admin
password   = admin123

Step -7:Then to create our desired two instances add the below lines after [mysql_multi] section. We have to define different unique values for each server instance or else the data and socket files for both servers collide and as a result mysql server will fail to start or your data could be corrupted.

[mysqld1]
port       = 3306
datadir    = /var/lib/mysql
pid-file   = /var/lib/mysql/
mysqld.pidsocket     = /var/lib/mysql/mysql.sock
user       = mysql
log-error  = /var/log/mysql1.err
 
[mysqld2]
port       = 3307
datadir    = /var/lib/mysql-databases/mysqld2
pid-file   = /var/lib/mysql-databases/mysqld2/
mysql.pidsocket     = /var/lib/mysql-databases/mysqld2/mysql.sock
user       = mysql
log-error  = /var/log/mysql2.err

Step-8:Save the configuration file and now create the files and folders as we have mentioned in the above configuration.To do that execute the following commands.

[root@localhost ~]#  mkdir -P /var/lib/mysql-databases/myqld2

Step-9:For mysql instance 1 we are using the defaults for previously running mysql server But we need to Create the data directory for instance2.Create it by

[root@localhost ~]# mkdir /var/lib/mysql-databases/myqld2/mysql

Step-10:Copy the mysql database files from the original instance to the second instances database directory and change the ownership of the data directory to the mysql user so the instance can read them.

[root@localhost ~]# cp -r /var/lib/mysql/mysql/ /var/lib/mysql-databases/mysqld2
[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql-databases

Step-11:Now the two instances are ready to run.We can start them by the folowing command

[root@localhost ~]#mysqld_multi start
To view the status of the instances you can run
[root@localhost ~]# mysqld_multi report
Output:
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running

You can see that the mysqld_multi script has started multiple mysql processes with the following commands.

ps -e | grep "mysql"

To stop both instances just execute the below command.

rhys@linux-n0sm:~> mysqld_multi stop

We are also able to control individual instances by referring to the assigned number.

rhys@linux-n0sm:~> mysqld_multi stop 1
 
To verify this
rhys@linux-n0sm:~> mysqld_multi report
 
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is running
 
rhys@linux-n0sm:~> mysqld_multi start 1
rhys@linux-n0sm:~> mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running

Here you need to remember that both the instances running on different port and also having different socket files.You can refer to the topic how to connect to mysql in php in case you are facing any problem to connect ot mysql through some script



### 如何在 MySQL 8 中创建 MySQL 实例 #### 创建 MySQL 实例的概念 在 MySQL 中,“实例”通常指的是运行中的 MySQL 数据库服务器进程及其配置环境。创建MySQL 实例涉及安装 MySQL 软件、初始化数据目录以及启动服务。 --- #### 安装 MySQL 8 要创建 MySQL 实例,首先需要安装 MySQL 8。可以通过以下方式完成: 1. **下载并安装 MySQL 社区版** 访问官方站点下载适用于操作系统的 MySQL Installer 或二进制包[^3]。 2. **通过包管理器安装**(Linux 用户) 使用 `apt` 或 `yum` 等工具快速安装: ```bash sudo apt update && sudo apt install mysql-server ``` --- #### 初始化 MySQL 实例 安装完成后,需初始化 MySQL 数据目录以准备首次运行。 1. **指定数据目录路径** 默认情况下,MySQL 将其数据文件存放在 `/var/lib/mysql/` 下。如果希望自定义位置,则可以在初始化时指定路径。 2. **执行初始化命令** 使用 `mysqld --initialize` 命令来初始化新实例的数据目录,并生成随机密码用于 root 用户登录。 ```bash mysqld --initialize --console ``` 此过程会在控制台打印初始 root 密码,保存该密码以便后续使用[^4]。 --- #### 配置 MySQL 实例 为了使实例正常工作,还需要调整些关键参数。 1. **编辑 my.cnf 文件** 打开 MySQL 主配置文件(通常是 `/etc/my.cnf` 或 `/etc/mysql/my.cnf`),修改以下选项: - 设置默认存储引擎:`default-storage-engine=InnoDB` - 自定义端口号:`port=3306` - 修改字符集编码:`character-set-server=utf8mb4` 和 `collation-server=utf8mb4_general_ci` 2. **应用更改** 编辑完毕后重启 MySQL 服务以加载新的配置: ```bash systemctl restart mysql ``` --- #### 启动与验证 MySQL 实例 成功初始化和配置后,可尝试连接到新建的 MySQL 实例。 1. **登录 MySQL 控制台** 使用之前记录的 root 初始密码进行身份验证: ```bash mysql -u root -p ``` 2. **检查状态** 登录后可通过以下查询确认当前实例的状态: ```sql SHOW VARIABLES LIKE 'version'; SELECT @@datadir; ``` --- #### 创建测试数据库 作为最后步,在已有的 MySQL 实例创建个新的数据库供开发或学习用途: ```sql CREATE DATABASE IF NOT EXISTS test_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; ``` --- ### 总结 以上流程涵盖了从零开始构建个完整的 MySQL 8 实例所需的主要步骤。每步均基于最佳实践设计,确保最终部署稳定可靠[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值