在原有mysql机器上增加一台实例

    采用的是yum install mysql-community-server yum方式安装mysql(社区版) 文章基础上新加一个mysql实例。
   这个完全可以直接实战上应用,只要规划好即可
   服务器上已经安装了3306端口的mysql服务,需要再启一个3307端口的mysql服务。(我们云上不少服务器都是安装的多实例,不过要慎重,特别对于重要服务来说,最好单独设计架构)
   
一、准备:
  


 1、建立data目录
         [root@fp-web-118 3307]# mkdir /data         
           [root@fp-web-118 3307]# mkdir /data/3307/data
         [root@fp-web-118 3307]# mkdir /data/3307/log
        [root@fp-web-118 3307]# touch /data/3307/log/mysqld.log
         [root@fp-web-118 3307]#chown -R mysql:mysql /data/3307
  2、复制mysql配置文件到3307目录下进行修改 
       [root@fp-web-118 3307]#  cp  /etc/my.cnf /data/3307


  二、编辑my.cnf

[root@fp-web-118 3307]# vi my.cnf

[client]
character-set-server = utf8
port    = 3307
socket  = /data/3307/mysql_3307.sock

[mysqld]
user=mysql
port=3307   ###这里很重要,否则mysql启动的时候会找3306端口
datadir=/data/3307/data  ###这里也不能设置错,否则日志都输出不出来
socket=/data/3307/mysql.sock

symbolic-links=0

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir = /usr/  ##这里就是你当初安装mysql的路径,一般是/usr/local/mysql,或者是/data/mysql等,yum或rpm是/usr

[mysqld_safe]
log-error=/data/3307/log/mysqld.log
pid-file=/data/3307/mysqld_3307.pid
##basedir 如果不知道可以查看之前启动的 ps -efww| grep mysql

注:这里是基本配置,可以在这个基础上增加相关配置参数。


三、数据库的初始化:

# 5.7.6之前初始化的方法是:mysql_install_db

#5.7.6之后的版本初始化数据库不再使用mysql_install_db,而是使用: bin/mysqld --initialize
这里机器上安装的是5.6版本mysql,yum方式

[root@fp-web-118 data]# mysql_install_db --defaults-file=/data/3307/my.cnf --datadir=/data/3307/data

执行过程如下:

Installing MySQL system tables...2022-02-01 21:26:46 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-01 21:26:46 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2022-02-01 21:26:46 0 [Note] /usr/sbin/mysqld (mysqld 5.6.51) starting as process 78101 ...
2022-02-01 21:26:46 78101 [Note] InnoDB: Using atomics to ref count buffer pool pages
2022-02-01 21:26:46 78101 [Note] InnoDB: The InnoDB memory heap is disabled
2022-02-01 21:26:46 78101 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-02-01 21:26:46 78101 [Note] InnoDB: Memory barrier is not used
2022-02-01 21:26:46 78101 [Note] InnoDB: Compressed tables use zlib 1.2.11
2022-02-01 21:26:46 78101 [Note] InnoDB: Using Linux native AIO
2022-02-01 21:26:46 78101 [Note] InnoDB: Using CPU crc32 instructions
2022-02-01 21:26:46 78101 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2022-02-01 21:26:46 78101 [Note] InnoDB: Completed initialization of buffer pool
2022-02-01 21:26:46 78101 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2022-02-01 21:26:46 78101 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2022-02-01 21:26:46 78101 [Note] InnoDB: Database physically writes the file full: wait...
2022-02-01 21:26:46 78101 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2022-02-01 21:26:47 78101 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2022-02-01 21:26:47 78101 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2022-02-01 21:26:47 78101 [Warning] InnoDB: New log files created, LSN=45781
2022-02-01 21:26:47 78101 [Note] InnoDB: Doublewrite buffer not found: creating new
2022-02-01 21:26:47 78101 [Note] InnoDB: Doublewrite buffer created
2022-02-01 21:26:47 78101 [Note] InnoDB: 128 rollback segment(s) are active.
2022-02-01 21:26:47 78101 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-02-01 21:26:47 78101 [Note] InnoDB: Foreign key constraint system tables created
2022-02-01 21:26:47 78101 [Note] InnoDB: Creating tablespace and datafile system tables.
2022-02-01 21:26:47 78101 [Note] InnoDB: Tablespace and datafile system tables created.
2022-02-01 21:26:47 78101 [Note] InnoDB: Waiting for purge to start
2022-02-01 21:26:47 78101 [Note] InnoDB: 5.6.51 started; log sequence number 0
2022-02-01 21:26:47 78101 [Note] RSA private key file not found: /data/3307/data//private_key.pem. Some authentication plugins will not work.
2022-02-01 21:26:47 78101 [Note] RSA public key file not found: /data/3307/data//public_key.pem. Some authentication plugins will not work.
2022-02-01 21:26:48 78101 [Note] Binlog end
2022-02-01 21:26:48 78101 [Note] InnoDB: FTS optimize thread exiting.
2022-02-01 21:26:48 78101 [Note] InnoDB: Starting shutdown...
2022-02-01 21:26:49 78101 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2022-02-01 21:26:49 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-01 21:26:49 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2022-02-01 21:26:49 0 [Note] /usr/sbin/mysqld (mysqld 5.6.51) starting as process 78127 ...
2022-02-01 21:26:49 78127 [Note] InnoDB: Using atomics to ref count buffer pool pages
2022-02-01 21:26:49 78127 [Note] InnoDB: The InnoDB memory heap is disabled
2022-02-01 21:26:49 78127 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-02-01 21:26:49 78127 [Note] InnoDB: Memory barrier is not used
2022-02-01 21:26:49 78127 [Note] InnoDB: Compressed tables use zlib 1.2.11
2022-02-01 21:26:49 78127 [Note] InnoDB: Using Linux native AIO
2022-02-01 21:26:49 78127 [Note] InnoDB: Using CPU crc32 instructions
2022-02-01 21:26:49 78127 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2022-02-01 21:26:49 78127 [Note] InnoDB: Completed initialization of buffer pool
2022-02-01 21:26:49 78127 [Note] InnoDB: Highest supported file format is Barracuda.
2022-02-01 21:26:49 78127 [Note] InnoDB: 128 rollback segment(s) are active.
2022-02-01 21:26:49 78127 [Note] InnoDB: Waiting for purge to start
2022-02-01 21:26:49 78127 [Note] InnoDB: 5.6.51 started; log sequence number 1625977
2022-02-01 21:26:49 78127 [Note] RSA private key file not found: /data/3307/data//private_key.pem. Some authentication plugins will not work.
2022-02-01 21:26:49 78127 [Note] RSA public key file not found: /data/3307/data//public_key.pem. Some authentication plugins will not work.
2022-02-01 21:26:49 78127 [Note] Binlog end
2022-02-01 21:26:49 78127 [Note] InnoDB: FTS optimize thread exiting.
2022-02-01 21:26:49 78127 [Note] InnoDB: Starting shutdown...
2022-02-01 21:26:51 78127 [Note] InnoDB: Shutdown completed; log sequence number 1625987
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:

/usr/bin/mysqladmin -u root password 'new-password'  ##这里是有价值的信息
/usr/bin/mysqladmin -u root -h fp-web-118 password 'new-password' ##这里是有价值的信息

Alternatively you can run:

/usr/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 /usr ; /usr/bin/mysqld_safe &    ##这里是有价值的信息

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file /usr/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /usr/my-new.cnf,
please compare it with your file and take the changes you need.

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

编写启动脚本,放在data/3307下,起名叫 mysql_start_my

#!/bin/sh
#init
#mysql_user=root
#mysql_pwd=123
port=3307
CmdPath="/usr/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
isrun=`ps -ef | grep ${mysql_sock}|grep -v grep|wc -l`

function_start_mysql()
{
if [ ! -e "$mysql_sock" ] || [ ${isrun} -eq 0 ];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@fp-web-118 3307]# chown mysql:mysql mysql_start_my
[root@fp-web-118 3307]# chmod 775mysql_start_my

启动3307myql

[root@fp-web-118 3307]# ./mysql_start_my start
Starting MySQL...

查看tcp端口
[root@fp-web-118 3307]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:6443 *:*
LISTEN 0 8 *:179 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 64 *:26684 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 80 :::3307 :::*
LISTEN 0 128 :::9099 :::*
LISTEN 0 64 :::24854 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::10050 :::*

登录3307端口

[root@fp-web-118 3307]# mysql -uroot -p -S /data/3307/mysql.sock
Enter password:    ##初始化默认为空
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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 |
+--------------------+
3 rows in set (0.00 sec)

mysql> show variables like 'port';  ##查看启动端口
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3307 |
+---------------+-------+
1 row in set (0.00 sec)


登录3306端口

[root@fp-web-118 3307]# mysql -u root -h fp-web-118 -pjinzs

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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 variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)

posted @ 2022-02-02 00:39  jinzi  阅读(275)  评论(0编辑  收藏  举报