DBMNG数据库管理与应用

独立思考能力,对于从事科学研究或其他任何工作,都是十分必要的。
当前位置:首页 > MySQL > 应用案例

MYSQL 多实例运行

mysql可以以多实例的方式,实现一台服务器,运行在不同端口不同数据文件的mysql,它们是相互独立的。

1、关闭原有的默认端口3306的mysql:service mysqd stop

2、拷贝或创建数据文件

#拷贝现有的mysql数据库文件
#我的在/var/lib/mysql,拷贝一份至mysql_3307文件夹
[root@test-206 ~]# cp -r /var/lib/mysql /var/lib/mysql_3307
#创建一个新的空数据库
[root@test-206 ~]# mkdir /var/lib/mysql_3307
[root@test-206 ~]# mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql

3、给数据文件赋予mysql用户与用户组

[root@test-206 ~]# chown -R mysql.mysql /var/lib/mysql_3307

4、创建multi的配置cnf文件,用于启动这个mysql实例(如3307)载入执行

[root@test-206 ~]# touch /usr/local/my_multi.cnf

文件中写入你想要的配置,如下为典型配置

复制代码
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root    #用于登陆和关闭此服务
password = 123456   #同上 [mysqld3307]
socket = /tmp/mysql_3307.sock
port = 3307 pid-file = /var/lib/mysql_3307/3307.pid
datadir = /var/lib/mysql_3307/ log = /var/lib/mysql_3307/3307.log
character-set-server    = utf8
user = mysql
复制代码

 5、启动你的多实例

[root@test-206 ~]# mysqld_multi --defaults-extra-file=/usr/local/my_multi.cnf start 3307

6、检查是否启动成功

[root@test-206 ~]# netstat -ntlp
tcp 0 0 :::3306 :::*                       LISTEN 3919/mysqld
tcp 0 0 :::3307 :::*                        LISTEN 15027/mysqld

如果没有发现你要的端口号mysql实例,可以检查下/var/lib/mysql_3307/3307.log文件,排除问题

7、设置新的密码

[root@test-206 ~]#  mysqladmin -uroot -S /tmp/mysql_3307.sock password 123456

8、登入你的新实例

复制代码
[root@test-206 ~]# mysql -uroot -S /tmp/mysql_3307.sock -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 Server version: 5.5.20-log Distributed by The IUS Community Project

Copyright (c) 2000, 2011, 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>
复制代码

再次确认下,你的这个实例,是不是用的mysql_3307这个文件夹的数据

复制代码
mysql> show variables like '%datadir%'; +---------------+----------------------+
| Variable_name | Value                |
+---------------+----------------------+
| datadir       | /var/lib/mysql_3307/ |
+---------------+----------------------+ 1 row in set (0.00 sec)

mysql>
复制代码

恩,没有错!最后,搞搞权限、用户之类。收工!

复制代码
#查用户
mysql> select user,host from mysql.user; +------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
|      | localhost |
| root | localhost |
|      | test-206 |
| root | test-206 |
+------+-----------+ 6 rows in set (0.00 sec)

#设权限
mysql> grant all on *.* to root@'%' identified by 'root' with grant option;
Query OK, 0 rows affected (0.00 sec)

##查权限
mysql> show grants for root;

##创用户
mysql> grant select on *.* to backup@'%' identified by 'backup';
Query OK, 0 rows affected (0.00 sec)
复制代码
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号