MySQL学习笔记(基础部分)-多实例mysql-5.5.x安装

MySQL学习笔记(平台CentOS 6)

1、添加mysql用户

[root@mysql-slave ~]# groupadd mysql
[root@mysql-slave ~]# useradd -s /sbin/nologin -g mysql -M mysql

2、配置安装环境

[root@mysql-slave ~]# yum groupinstall 'Development Tools' -y
[root@mysql-slave ~]# yum install gcc gcc-c++ make ncurses-devel bison perl -y # 依赖包
[root@mysql-slave tools]# tar -zxf cmake-2.8.12.tar.gz			       # 安装cmake
[root@mysql-slave cmake-2.8.12]# ./configure
[root@mysql-slave cmake-2.8.12]# gmake
[root@mysql-slave cmake-2.8.12]# gmake install

3、创建目录/copy实例文件

[root@mysql-slave ~]# mkdir  /usr/local/mysql
[root@mysql-slave ~]# mkdir /db/{3306,3307}/{mysql,tmp} -p

[root@mysql-slave ~]# tree /db
/db
├── 3306
│   ├── mysql                                    # 3306实例数据文件目录
│   └── tmp                                      # 3306 mysql.sock存放位置(此文件可灵活放置)
└── 3307
    ├── mysql                                    # 3307实例数据文件目录
    └── tmp	                                 # 3307 mysql.sock存放位置

    拷贝配置文件(my.cnf)和启动脚本(mysqld)至目录

    3306实例配置my.cnf内容:

[client]
port		= 3306
socket		= /db/3306/tmp/mysql.sock

[mysqld]
port		= 3306
socket		= /db/3306/tmp/mysql.sock
datadir		= /db/3306/mysql
pid-file	= /db/3306/mysql.pid
back_log = 50
max_connections = 100
max_connect_errors = 10
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 1M
max_heap_table_size = 64M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 8
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = MYISAM
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=/db/3306/mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 2
server-id = 1
key_buffer_size = 32M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2G
innodb_data_file_path = ibdata1:10M:autoextend
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
open-files-limit = 8192
log-error=/db/3306/mysql_3306.err
pid-file=/db/3306/mysql.pid

    3306实例配置文件与3307不同的部分:

    232347_UZRZ_2428313.png

    3306实例启动脚本内容

#!/bin/sh
#init
port=3306                                        # 3307这里端口改成‘3307’,其他配置无需改动
mysql_user="root"
mysql_pwd=""                                     # 初始mysql没有密码
CmdPath="/usr/local/mysql/bin"                   # mysql程序目录
mysql_sock="/db/${port}/tmp/mysql.sock"          # mysql.sock文件位置
#startup function
function_start_mysql()
{
   if [ ! -e "$mysql_sock" ];then
    printf "Starting MySQL...\n"
    /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/db/${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 /db/${port}/tmp/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: /db/${port}/mysqld {start|stop|restart}\n"
esac

    最终结构:

[root@mysql-slave ~]# tree /db
/db
├── 3306
│   ├── my.cnf                                   # 3306实例配置文件
│   ├── mysql
│   ├── mysqld                                   # 3306实例启动/关闭程序
│   └── tmp
└── 3307
    ├── my.cnf                                   # 3307实例配置文件
    ├── mysql
    ├── mysqld                                   # 3307实例启动/关闭程序
    └── tmp

4、授权

[root@mysql-slave ~]# chown -R mysql.mysql /usr/local/mysql
[root@mysql-slave ~]# chown -R mysql.mysql /db
[root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs chmod +x
[root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs ls -l
-rwxr-xr-x 1 mysql mysql 756 Aug 19 01:45 /db/3306/mysqld
-rwxr-xr-x 1 mysql mysql 755 Aug 19 01:46 /db/3307/mysqld

5、安装mysql

[root@mysql-slave tools]# tar -zxf mysql-5.5.37.tar.gz 
[root@mysql-slave tools]# cd mysql-5.5.37
[root@mysql-slave mysql-5.5.37]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
> -DMYSQL_TCP_PORT=3306 \
> -DEXTRA_CHARSETS=all \
> -DENABLED_LOCAL_INFILE=ON \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_FEDERATED_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
> -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
> -DWITH_FAST_MUTEXES=1 \
> -DWITH_ZLIB=bundled \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_READLINE=1 \
> -DWITH_EMBEDDED_SERVER=1 \
> -DWITH_DEBUG=0

[root@mysql-slave mysql-5.5.37]# make && make install
[root@mysql-slave ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile
[root@mysql-slave ~]# source /etc/profile

6、初始化mysql

[root@mysql-slave ~]# cd /usr/local/mysql/scripts/
[root@mysql-slave scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/db/3306/mysql --user=mysql
WARNING: The host 'mysql-slave' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK                                               # 看到两个"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/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h mysql-slave password 'new-password'

Alternatively you can run:
/usr/local/mysql/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/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl

Please report any problems at  

[root@mysql-slave scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/db/3307/mysql --user=mysql                          # 同样方法初始化3307实例

    初始化成功后,产生文件/目录

[root@mysql-slave scripts]# ll /db/3306/mysql
total 12
drwx------ 2 mysql root  4096 Aug 19 19:12 mysql
drwx------ 2 mysql mysql 4096 Aug 19 19:12 performance_schema
drwx------ 2 mysql root  4096 Aug 19 19:12 test

[root@mysql-slave scripts]# ll /db/3307/mysql
total 12
drwx------ 2 mysql root  4096 Aug 19 19:15 mysql
drwx------ 2 mysql mysql 4096 Aug 19 19:15 performance_schema
drwx------ 2 mysql root  4096 Aug 19 19:15 test

7、启动mysql

[root@mysql-slave ~]# /db/3306/mysqld start
[root@mysql-slave ~]# /db/3307/mysqld start
[root@mysql-slave ~]# netstat -ntulp | grep 330*
tcp     0     0 0.0.0.0:3306      0.0.0.0:*       LISTEN      2809/mysqld 
tcp     0     0 0.0.0.0:3307      0.0.0.0:*       LISTEN      3498/mysqld

8、多实例mysql登陆/关闭

[root@mysql-slave ~]# mysql -S /db/3306/tmp/mysql.sock      # 通过指定sock文件登陆(此时无需输入密码) 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, 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实例(此时mysql没设置密码)

[root@mysql-slave ~]# /db/3306/mysqld stop
Stoping MySQL...
Enter password:                                       # 直接回车,即可关闭3306 mysql实例
[root@mysql-slave ~]# netstat -ntulp | grep 330
tcp        0      0 0.0.0.0:3307      0.0.0.0:*        LISTEN   3498/mysqld

9、为多实例mysql设置密码/登陆/关闭

[root@mysql-slave ~]# /db/3306/mysqld start
[root@mysql-slave ~]# mysqladmin -u root -S /db/3306/tmp/mysql.sock password 'q.1234'
[root@mysql-slave ~]# mysqladmin -u root -S /db/3307/tmp/mysql.sock password 'q.1234'
[root@mysql-slave ~]# mysql -S /db/3306/tmp/mysql.sock            # 此时无法通过sock直接登陆
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    配置密码后,和mysql单实例相比,登陆时,需指定sock文件位置

[root@mysql-slave ~]# mysql -S /db/3306/tmp/mysql.sock -uroot -pq.1234 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, 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实例关闭,有两种方法:

    方法一:在关闭时,输入密码

[root@mysql-slave ~]# /db/3306/mysqld stop
Stoping MySQL...
Enter password:                                     # 输入root的密码
[root@mysql-slave ~]# netstat -ntulp | grep 330
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      3498/mysqld

    方法二:把密码写的脚本中(注意修改权限,安全起见)

[root@mysql-slave ~]# vim /db/3306/mysqld
mysql_pwd="q.1234"
[root@mysql-slave ~]# vim /db/3307/mysqld
mysql_pwd="q.1234"

[root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs ls -l
-rwxr-xr-x 1 mysql mysql 989 Aug 19 20:28 /db/3306/mysqld
-rwxr-xr-x 1 mysql mysql 989 Aug 19 20:29 /db/3307/mysqld
[root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs chown root    # 修改权限
[root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs chmod 700
[root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs ls -l
-rwx------ 1 root mysql 989 Aug 19 20:28 /db/3306/mysqld
-rwx------ 1 root mysql 989 Aug 19 20:29 /db/3307/mysqld

[root@mysql-slave ~]# netstat -ntulp | grep 330
tcp        0      0 0.0.0.0:3306     0.0.0.0:*          LISTEN      9865/mysqld         
tcp        0      0 0.0.0.0:3307     0.0.0.0:*          LISTEN      3498/mysqld         
[root@mysql-slave ~]# /db/3306/mysqld stop                            # 无需密码,直接关闭mysql
Stoping MySQL...
[root@mysql-slave ~]# netstat -ntulp | grep 330
tcp        0      0 0.0.0.0:3307     0.0.0.0:*          LISTEN      3498/mysqld

10、远程连接多实例mysql

mysql> select user,host,password from mysql.user;
+------+-------------+-------------------------------------------+
| user | host        | password                                  |
+------+-------------+-------------------------------------------+
| root | localhost   | *312639BE4EF3C0F04E07B607FC0B2D60223203BA |
| root | mysql-slave |                                           |
| root | 127.0.0.1   |                                           |
| root | ::1         |                                           |
|      | localhost   |                                           |
|      | mysql-slave |                                           |
+------+-------------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> grant all privileges on *.* to root@'172.18.10.%' identified by 'q.1234' with grant option;
Query OK, 0 rows affected (0.00 sec)                        # 授权登陆
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user;
+------+-------------+-------------------------------------------+
| user | host        | password                                  |
+------+-------------+-------------------------------------------+
| root | localhost   | *312639BE4EF3C0F04E07B607FC0B2D60223203BA |
| root | mysql-slave |                                           |
| root | 127.0.0.1   |                                           |
| root | ::1         |                                           |
|      | localhost   |                                           |
|      | mysql-slave |                                           |
| root | 172.18.10.% | *312639BE4EF3C0F04E07B607FC0B2D60223203BA |
+------+-------------+-------------------------------------------+
7 rows in set (0.00 sec)

    远程登陆测试   

[root@mysql-slave ~]# mysql -uroot -p -S /db/3306/tmp/mysql.sock 
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  8 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
[root@mysql-master ~]# mysql -uroot -pq.1234 -h 172.18.10.11 -P3306        # 远程登陆只需指定端口即可
[root@mysql-slave ~]# mysql -uroot -p -S /db/3306/tmp/mysql.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, 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 processlist;
+----+------+--------------------+------+---------+------+-------+------------------+
| Id | User | Host               | db   | Command | Time | State | Info             |
+----+------+--------------------+------+---------+------+-------+------------------+
| 10 | root | 172.18.10.10:50353 | NULL | Sleep   |  101 |       | NULL             |
| 11 | root | localhost          | NULL | Query   |    0 | NULL  | show processlist |
+----+------+--------------------+------+---------+------+-------+------------------+
2 rows in set (0.00 sec)


转载于:https://my.oschina.net/u/2428313/blog/494206

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值