mysql多实例切换_mysql多实例

准备工作

pkill mysqld

ps -ef|grep mysql

rm -rf /etc/init.d/mysqld

mkdir -p /data/{3306,3307}/data

tree /data/

b3ab036a501a98645ae673d3729638b6.png

6313f4326ee2e13433eb80a03fc902d8.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 [client]2 port = 3306

3 socket = /data/3306/mysql.sock4

5 [mysql]6 no-auto-rehash7

8 [mysqld]9 user = mysql

10 port = 3306

11 socket = /data/3306/mysql.sock12 basedir = /application/mysql

13 datadir = /data/3306/data14 open_files_limit = 1024

15 back_log = 600

16 max_connections = 800

17 max_connect_errors = 3000

18 table_cache = 614

19 external-locking = FALSE

20 max_allowed_packet =8M21 sort_buffer_size =1M22 join_buffer_size =1M23 thread_cache_size = 100

24 thread_concurrency = 2

25 query_cache_size =2M26 query_cache_limit =1M27 query_cache_min_res_unit =2k28 #default_table_type = InnoDB

29 thread_stack =192K30 #transaction_isolation = READ-COMMITTED

31 tmp_table_size =2M32 max_heap_table_size =2M33 long_query_time = 1

34 #log_long_format

35 #log-error = /data/3306/error.log

36 #log-slow-queries = /data/3306/slow.log

37 pid-file = /data/3306/mysql.pid38 log-bin = /data/3306/mysql-bin39 relay-log = /data/3306/relay-bin40 relay-log-info-file = /data/3306/relay-log.info41 binlog_cache_size =1M42 max_binlog_cache_size =1M43 max_binlog_size =2M44 expire_logs_days = 7

45 key_buffer_size =16M46 read_buffer_size =1M47 read_rnd_buffer_size =1M48 bulk_insert_buffer_size =1M49 #myisam_sort_buffer_size = 1M

50 #myisam_max_sort_file_size = 10G

51 #myisam_max_extra_sort_file_size = 10G

52 #myisam_repair_threads = 1

53 #myisam_recover

54

55 lower_case_table_names = 1

56 skip-name-resolve57 slave-skip-errors = 1032,1062

58 replicate-ignore-db=mysql

59

60 server-id = 1

61

62 innodb_additional_mem_pool_size =4M63 innodb_buffer_pool_size =32M64 innodb_data_file_path = ibdata1:128M:autoextend65 innodb_file_io_threads = 4

66 innodb_thread_concurrency = 8

67 innodb_flush_log_at_trx_commit = 2

68 innodb_log_buffer_size =2M69 innodb_log_file_size =4M70 innodb_log_files_in_group = 3

71 innodb_max_dirty_pages_pct = 90

72 innodb_lock_wait_timeout = 120

73 innodb_file_per_table = 0

74 [mysqldump]75 quick76 max_allowed_packet =2M77

78 [mysqld_safe]79 log-error=/data/3306/mysql_oldboy3306.err80 pid-file=/data/3306/mysqld.pid

3306-my.cnf

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 #!/bin/sh

2 ################################################

3 #this scripts is created by oldboy at 2007-06-09

4 #oldboy QQ:31333741

5 #site:http://www.etiantian.org

6 #blog:http://oldboy.blog.51cto.com

7 #oldboy trainning QQ group: 208160987 226199307 44246017

8 ################################################

9

10 #init

11 port=3306

12 mysql_user="root"

13 mysql_pwd="oldboy"

14 CmdPath="/application/mysql/bin"

15 mysql_sock="/data/${port}/mysql.sock"

16 #startup function

17 function_start_mysql()18 {19 if [ ! -e "$mysql_sock"];then20 printf "Starting MySQL...\n"

21 /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

22 else

23 printf "MySQL is running...\n"

24 exit

25 fi26 }27

28 #stop function

29 function_stop_mysql()30 {31 if [ ! -e "$mysql_sock"];then32 printf "MySQL is stopped...\n"

33 exit

34 else

35 printf "Stoping MySQL...\n"

36 ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown37 fi38 }39

40 #restart function

41 function_restart_mysql()42 {43 printf "Restarting MySQL...\n"

44 function_stop_mysql45 sleep 2

46 function_start_mysql47 }48

49 case $1in50 start)51 function_start_mysql52 ;;53 stop)54 function_stop_mysql55 ;;56 restart)57 function_restart_mysql58 ;;59 *)60 printf "Usage: /data/${port}/mysql {start|stop|restart}\n"

61 esac

3306-mysql

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 [client]2 port = 3307

3 socket = /data/3307/mysql.sock4

5 [mysql]6 no-auto-rehash7

8 [mysqld]9 user = mysql

10 port = 3307

11 socket = /data/3307/mysql.sock12 basedir = /application/mysql

13 datadir = /data/3307/data14 open_files_limit = 1024

15 back_log = 600

16 max_connections = 800

17 max_connect_errors = 3000

18 table_cache = 614

19 external-locking = FALSE

20 max_allowed_packet =8M21 sort_buffer_size =1M22 join_buffer_size =1M23 thread_cache_size = 100

24 thread_concurrency = 2

25 query_cache_size =2M26 query_cache_limit =1M27 query_cache_min_res_unit =2k28 #default_table_type = InnoDB

29 thread_stack =192K30 #transaction_isolation = READ-COMMITTED

31 tmp_table_size =2M32 max_heap_table_size =2M33 #long_query_time = 1

34 #log_long_format

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

36 #log-slow-queries = /data/3307/slow.log

37 pid-file = /data/3307/mysql.pid38 #log-bin = /data/3307/mysql-bin

39 relay-log = /data/3307/relay-bin40 relay-log-info-file = /data/3307/relay-log.info41 binlog_cache_size =1M42 max_binlog_cache_size =1M43 max_binlog_size =2M44 expire_logs_days = 7

45 key_buffer_size =16M46 read_buffer_size =1M47 read_rnd_buffer_size =1M48 bulk_insert_buffer_size =1M49 #myisam_sort_buffer_size = 1M

50 #myisam_max_sort_file_size = 10G

51 #myisam_max_extra_sort_file_size = 10G

52 #myisam_repair_threads = 1

53 #myisam_recover

54

55 lower_case_table_names = 1

56 skip-name-resolve57 slave-skip-errors = 1032,1062

58 replicate-ignore-db=mysql

59

60 server-id = 3

61

62 innodb_additional_mem_pool_size =4M63 innodb_buffer_pool_size =32M64 innodb_data_file_path = ibdata1:128M:autoextend65 innodb_file_io_threads = 4

66 innodb_thread_concurrency = 8

67 innodb_flush_log_at_trx_commit = 2

68 innodb_log_buffer_size =2M69 innodb_log_file_size =4M70 innodb_log_files_in_group = 3

71 innodb_max_dirty_pages_pct = 90

72 innodb_lock_wait_timeout = 120

73 innodb_file_per_table = 0

74 [mysqldump]75 quick76 max_allowed_packet =2M77

78 [mysqld_safe]79 log-error=/data/3307/mysql_oldboy3307.err80 pid-file=/data/3307/mysqld.pid

3307-my.cnf

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 #!/bin/sh

2 ################################################

3 #this scripts is created by oldboy at 2007-06-09

4 #oldboy QQ:31333741

5 #site:http://www.etiantian.org

6 #blog:http://oldboy.blog.51cto.com

7 #oldboy trainning QQ group: 208160987 226199307 44246017

8 ################################################

9

10 #init

11 port=3307

12 mysql_user="root"

13 mysql_pwd="oldboy"

14 CmdPath="/application/mysql/bin"

15 mysql_sock="/data/${port}/mysql.sock"

16 #startup function

17 function_start_mysql()18 {19 if [ ! -e "$mysql_sock"];then20 printf "Starting MySQL...\n"

21 /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

22 else

23 printf "MySQL is running...\n"

24 exit

25 fi26 }27

28 #stop function

29 function_stop_mysql()30 {31 if [ ! -e "$mysql_sock"];then32 printf "MySQL is stopped...\n"

33 exit

34 else

35 printf "Stoping MySQL...\n"

36 ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown37 fi38 }39

40 #restart function

41 function_restart_mysql()42 {43 printf "Restarting MySQL...\n"

44 function_stop_mysql45 sleep 2

46 function_start_mysql47 }48

49 case $1in50 start)51 function_start_mysql52 ;;53 stop)54 function_stop_mysql55 ;;56 restart)57 function_restart_mysql58 ;;59 *)60 printf "Usage: /data/${port}/mysql {start|stop|restart}\n"

61 esac

3307-mysql

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 [client]2 port = 3308

3 socket = /data/3308/mysql.sock4

5 [mysql]6 no-auto-rehash7

8 [mysqld]9 user = mysql

10 port = 3308

11 socket = /data/3308/mysql.sock12 basedir = /application/mysql

13 datadir = /data/3308/data14 open_files_limit = 1024

15 back_log = 600

16 max_connections = 800

17 max_connect_errors = 3000

18 table_cache = 614

19 external-locking = FALSE

20 max_allowed_packet =8M21 sort_buffer_size =1M22 join_buffer_size =1M23 thread_cache_size = 100

24 thread_concurrency = 2

25 query_cache_size =2M26 query_cache_limit =1M27 query_cache_min_res_unit =2k28 #default_table_type = InnoDB

29 thread_stack =192K30 #transaction_isolation = READ-COMMITTED

31 tmp_table_size =2M32 max_heap_table_size =2M33 #long_query_time = 1

34 #log_long_format

35 #log-error = /data/3308/error.log

36 #log-slow-queries = /data/3308/slow.log

37 pid-file = /data/3308/mysql.pid38 #log-bin = /data/3308/mysql-bin

39 relay-log = /data/3308/relay-bin40 relay-log-info-file = /data/3308/relay-log.info41 binlog_cache_size =1M42 max_binlog_cache_size =1M43 max_binlog_size =2M44 expire_logs_days = 7

45 key_buffer_size =16M46 read_buffer_size =1M47 read_rnd_buffer_size =1M48 bulk_insert_buffer_size =1M49 #myisam_sort_buffer_size = 1M

50 #myisam_max_sort_file_size = 10G

51 #myisam_max_extra_sort_file_size = 10G

52 #myisam_repair_threads = 1

53 #myisam_recover

54

55 lower_case_table_names = 1

56 skip-name-resolve57 slave-skip-errors = 1032,1062

58 replicate-ignore-db=mysql

59

60 server-id = 5

61

62 innodb_additional_mem_pool_size =4M63 innodb_buffer_pool_size =32M64 innodb_data_file_path = ibdata1:128M:autoextend65 innodb_file_io_threads = 4

66 innodb_thread_concurrency = 8

67 innodb_flush_log_at_trx_commit = 2

68 innodb_log_buffer_size =2M69 innodb_log_file_size =4M70 innodb_log_files_in_group = 3

71 innodb_max_dirty_pages_pct = 90

72 innodb_lock_wait_timeout = 120

73 innodb_file_per_table = 0

74 [mysqldump]75 quick76 max_allowed_packet =2M77

78 [mysqld_safe]79 log-error=/data/3308/mysql_oldboy3307.err80 pid-file=/data/3308/mysqld.pid

3308-my.cnf

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 #!/bin/sh

2 ################################################

3 #this scripts is created by oldboy at 2007-06-09

4 #oldboy QQ:31333741

5 #site:http://www.etiantian.org

6 #blog:http://oldboy.blog.51cto.com

7 #oldboy trainning QQ group: 208160987 226199307 44246017

8 ################################################

9

10 #init

11 port=3308

12 mysql_user="root"

13 mysql_pwd="oldboy"

14 CmdPath="/application/mysql/bin"

15 mysql_sock="/data/${port}/mysql.sock"

16 #startup function

17 function_start_mysql()18 {19 if [ ! -e "$mysql_sock"];then20 printf "Starting MySQL...\n"

21 /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

22 else

23 printf "MySQL is running...\n"

24 exit

25 fi26 }27

28 #stop function

29 function_stop_mysql()30 {31 if [ ! -e "$mysql_sock"];then32 printf "MySQL is stopped...\n"

33 exit

34 else

35 printf "Stoping MySQL...\n"

36 ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown37 fi38 }39

40 #restart function

41 function_restart_mysql()42 {43 printf "Restarting MySQL...\n"

44 function_stop_mysql45 sleep 2

46 function_start_mysql47 }48

49 case $1in50 start)51 function_start_mysql52 ;;53 stop)54 function_stop_mysql55 ;;56 restart)57 function_restart_mysql58 ;;59 *)60 printf "Usage: /data/${port}/mysql {start|stop|restart}\n"

61 esac

3308-mysql

unzip data.zip /

rm -rf /data.zip

[root@oldboy /]# find /data/  -type f -name "mysql" |xargs ls -l

-rw-r--r--. 1 root root 1307 Jul 15  2013 /data/3306/mysql

-rw-r--r--. 1 root root 1307 Jul 21  2013 /data/3307/mysql

[root@oldboy /]# find /data/ -name mysql

/data/3307/mysql

/data/3306/mysql

16089e2450dc89777ffdd211ecad950f.png

多实例的自动和停止

8419e0b8ed2e35a81af4776263db3c56.png

初始化:

9667db48debe64767b595490bf1ac884.png

/application/mysql/bin/mysqld_safe  --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &

/application/mysql/bin/mysqld_safe  --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &

e4d8c4c5bc83d1497909b2bc18a29742.png

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql

chown -R mysql.mysql /data

chmod +x /data/3306/mysql

chmod +x /data/3307/mysql

[需要复制多实例文件mysqll到目录下]

[root@oldboy data]# ./3306/mysql start

Starting MySQL...

[root@oldboy data]# ./3307/mysql start

Starting MySQL...

[root@oldboy data]# netstat -lntup |grep 3306

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3339/mysqld

[root@oldboy data]# netstat -lntup |grep 3307

tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 4056/mysqld

登陆进去mysql命令

mysql -S /data/3307/mysql.sock

359efb02d6f5d100a2107e9e14119bd7.png

停止会报错,因为停止数据库默认要指定密码 的,所以要先把mysql里面密码改为空

89232a5666e8ecce5d1dc994d1908078.png

624dbf0eb89d52c338c5b2df662fae6a.png

改密码

[root@oldboy 3306]# mysqladmin -uroot -S /data/3306/mysql.sock password 'oldboy123'

[root@oldboy 3306]# mysqladmin -uroot -S /data/3307/mysql.sock password 'oldboy123'

然后修改msyql 密码

1480f049fd122f3cba148c69008d1a0a.png

为了安全  要去更改mysql权限

[root@oldboy data]# find /data -type f -name "mysql" -exec chmod 700 {} \;

[root@oldboy data]# find /data -type f -name "mysql" -exec ls -l {} \;

-rwx------. 1 mysql mysql 1310 Oct 25 23:04 /data/3307/mysql

-rwx------. 1 mysql mysql 1310 Oct 25 23:04 /data/3306/mysql

配置好密码后 再次登录 必须输入密码

mysql -uroot -poldboy123 -S /data/3306/mysql.sock

常见问题:

6e8dc008d67122dc4712c9e7e1915144.png

e7c1d7fef9f251fd25d94fcf60494e99.png

单实例mysql

启动:

ec6e4b1b1e37cae91da9c74adeb52bdb.png

e62d026d9dee92d5af1dcf8064a10527.png

Mysql启动原理

a780dd5fe51d96c1618dcc65067463e1.png

960b76c41e05646f20a5a158decf8ce1.png

eff39b61ea1011608017fce0319e2a19.png

优雅关闭数据库:

1bec7af78446bfa6e1a8325729dc2121.png

多实例msyql登录:

dd2d793e944b96182a7fec2dd2d6c976.png

设置密码:

74963d2ae124d1e524cead0b669a801e.png

修改密码:

dfb841ab81689d01583a3d454c1655db.png

3fca9fd3b2fc6b09d8deb5ff6210b305.png

找回丢失的密码:

58fec0291ce85ecce8df54e53218eaab.png

d085c89a640a6d6abbb08360a77305e3.png

5e7e676a36fd28afd39717ecfec2d206.png

bbf01be84d7726777bcd76f85e98cdd6.png

多实例msyql启动修改丢失root密码方法

1,关闭msyql

killall mysqld

2,启动时候加--skip-grant-tables参数

mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table &

mysql -uroot -p -S /data/3308/mysql.sock

3,修改密码

update mysql.user set password=PASSWORD('123456') where user='root';

FLUSH PRIVILEGES;

关闭mysql

mysqladmin -uroot -p123456 shutdown -S /data/3308/mysql.sock

重启mysql

/data/3308/mysql start

重新登录mysql

mysql -uroot -p123456 -S /data/3308/mysql.sock

一个小故障解决:

e802bfb209dad86341aebda1a41038a4.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值