1.1 数据库介绍(MySQL安装 体系结构、基本管理)


http://www.cnblogs.com/clsn/p/8038964.html
数据库介绍(MySQL安装 体系结构、基本管理)
http://blog.51cto.com/sgk2011/2051641
MySQL的开发必会的sql语句

(一) mysql数据库的安装
1.系统环境说明
[root@centos ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@centos ~]# uname -r
2.6.32-696.13.2.el6.i686
[root@centos ~]# /etc/init.d/iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
[root@centos ~]# /etc/init.d/iptables status
iptables:未运行防火墙。
[root@centos ~]# getenforce
Disabled
[root@centos ~]# hostname -I //显示IP和网关
172.16.135.134 10.0.1.1

2.mysql编译安装
[root@centos ~]# yum install -y ncurses-devel libaio-devel //安装依赖包
[root@centos ~]# yum install -y cmake //安装cmake编译工具
//cmake
定制功能:存储引擎、字符集、压缩等
定制安装位置、数据存储位置、文件位置(socket)

[root@centos ~]# useradd -s /sbin/nologin -M mysql -u1000 //创建mysql管理用户
[root@centos ~]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

[root@centos ~]# mkdir -p /server/tools
[root@centos ~]# cd /server/tools/
[root@centos tools]# wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36.tar.gz //下载mysql软件包,解压
[root@centos tools]# ls -lh
总用量 31M
-rw-r--r-- 1 root root 31M 3月 18 2017 mysql-5.6.36.tar.gz

[root@centos tools]# tar -xvf mysql-5.6.36.tar.gz
[root@centos tools]# ls -lh
总用量 31M
drwxr-xr-x 33 7161 31415 4.0K 3月 18 2017 mysql-5.6.36
-rw-r--r-- 1 root root 31M 3月 18 2017 mysql-5.6.36.tar.gz
注:这里使用的是sohu的镜像源进行下载(软件版本5.6.36)

//进入查询目录,使用cmake进行编译,安装,创建软连接,过程时间较长:
[root@centos mysql-5.6.36]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 \
> -DMYSQL_DATADIR=/application/mysql-5.6.36/data \
> -DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_EXTRA_CHARSETS=all \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_FEDERATED_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
> -DWITH_ZLIB=bundled \
> -DWITH_SSL=bundled \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_EMBEDDED_SERVER=1 \
> -DENABLE_DOWNLOADS=1 \
> -DWITH_DEBUG=0
//cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 \
-DMYSQL_DATADIR=/application/mysql-5.6.36/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
[root@centos mysql-5.6.36]# yum install -y gcc gcc-c++ git //安装遇到错误,解决
[root@centos mysql-5.6.36]# make &&make install
[root@centos mysql-5.6.36]# ln -s /application/mysql-5.6.36/ /application/mysql

3.数据库安装后的操作
[root@centos mysql-5.6.36]# \cp /application/mysql/support-files/my-default.cnf /etc/my.cnf //复制配置文件
//说明:
编译的MySQL可以暂时不需要设置配置文件。
如果以前操作系统中安装过rpm格式的mysql,系统中可能会遗留/etc/my.cnf文件,我们需要将它删除掉
[root@centos mysql-5.6.36]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql //初始化数据库

防止报错:数据库启动会提示,找不到xx/tmp/mysql.sock,原因是5.6.36版本不会自动创建tmp目录,需要我们手工创建:
[root@centos mysql-5.6.36]# mkdir /application/mysql/tmp

[root@centos mysql-5.6.36]# chown -R mysql.mysql /application/mysql/ //修改程序目录的属主,属组
[root@centos mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld //复制启动脚本,并修改权限
[root@centos mysql-5.6.36]# chmod 700 /etc/init.d/mysqld

[root@centos mysql-5.6.36]# echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile //添加环境变量
[root@centos mysql-5.6.36]# tail -1 /etc/profile
PATH=/application/mysql/bin/:$PATH
[root@centos mysql-5.6.36]# source /etc/profile
[root@centos mysql-5.6.36]# echo $PATH
/application/mysql/bin/:/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos mysql-5.6.36]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql-5.6.36/data/centos.err'.
. [确定]
[root@centos mysql-5.6.36]# netstat -lntup|grep mysqld //查看mysqld服务是否启动
tcp 0 0 :::3306 :::* LISTEN 26880/mysqld
[root@centos mysql-5.6.36]# mysql //使用mysql命令登录
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, 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>

//[root@centos mysql-5.6.36]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/application/mysql-5.6.36/tmp/mysql.sock' (2) //原因是没有启动该服务!
--------------------------------------
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)

mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | centos | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | centos | |
+------+-----------+----------+
6 rows in set (0.00 sec)

4.数据库安全设置
[root@centos mysql-5.6.36]# mysqladmin -u root password '123456' //设置root用户密码
Warning: Using a password on the command line interface can be insecure.
[root@centos mysql-5.6.36]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@centos mysql-5.6.36]# mysql -u root -p123456
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 5
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, 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> select user,host from mysql.user;//优化数据库(清理用户及无用数据库)
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | centos |
| root | centos |
| | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

mysql> drop user root@'::1';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'centos';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | centos |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

(二)Mysql体系结构与基本管理
1.客户端与服务器端模型
(1)mysql服务结构:
mysql是一个典型的c/s模式,服务端与客户端两部分组成:
服务端程序 mysqld
客户端程序 mysql自带客户端(mysql、mysqladmin、mysqldump等)
第三方客户端 API接口(php-mysql)
(2)mysql连接方式:
a.TCP/IP连接:网络连接串(通过用户名 密码 IP 端口进行连接)
[root@centos mysql-5.6.36]# mysql -u root p123456 -h 127.0.0.0 -P 3306
b.socket 连接:网络套接字(用户名 密码 socket文件)
[root@centos mysql-5.6.36]# mysql -u root p123456 -S /application/mysql/tmp/mysql.sock
在linux中使用mysql命令不加其他的参数连接方式即:
[root@centos mysql-5.6.36]# mysql -u root -p123456 //使用的是套接字文件方式登录的

mysql的关闭方法
推荐使用的方法1:
[root@centos mysql-5.6.36]# mysqladmin -u root -p123456 shutdown //1
Warning: Using a password on the command line interface can be insecure.
[root@centos mysql-5.6.36]# netstat -lntup|grep mysqld
推荐使用的方法2:
[root@centos mysql-5.6.36]# /etc/init.d/mysqld start
Starting MySQL... [确定]
[root@centos mysql-5.6.36]# netstat -lntup|grep mysqld
tcp 0 0 :::3306 :::* LISTEN 31691/mysqld
[root@centos mysql-5.6.36]# /etc/init.d/mysqld stop //2
Shutting down MySQL.. [确定]
[root@centos mysql-5.6.36]# netstat -lntup|grep mysqld

注意:尽量避免使用kill命令

perror命令是mysql自带命令,能够查询mysql错误代码的含义。
[root@centos mysql-5.6.36]# perror 13
OS error code 13: Permission denied
[root@centos mysql-5.6.36]# perror 12
OS error code 12: Cannot allocate memory


4.mysql多实例
(1)简介:
简单地说,MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306/3307)同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。
这些MySQL多实例共用一套MySQL安装程序,使用不同的my.cnf(也可以相同)配置文件、启动程序(也可以相同)和数据文件。在提供服务时,多实例MySQL在逻辑上看来是各自独立的,他们根据配置文件对应设定值,获得服务器响应数量的资源。
(2) MySQL多实例的作用:
a.有效利用服务器资源
  当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务,且可以实现资源的逻辑隔离
b.节约服务器资源
  当公司资金紧张,但是数据库又需要各自尽量独立地提供服务,而且,需要主从复制等技术时,多实例就再好不过了
(3)MySQL多实例的问题:
MySQL多实例有它的好处,但也有弊端,比如,会存在资源互相抢占的问题。当某个数据库实例并发很高或者有SQL慢查询时,整个实例会消耗大量的系统CPU、磁盘I/O等资源,导致服务器上的其他数据库实例提供服务的质量一起下降。
(4)多实例配置思路:
1 多套配置文件
2 多套 数据
3 多个socket
4 多个端口
5 多个日志文件
6 多个启动程序
(5)多实例配置过程:
说明:前期mysql的安装与MySQL单实例的安装一致,在这里就不再重复(配置完成不要启动)
[root@centos ~]# netstat -lntup|grep mysqld
[root@centos ~]# mkdir -p /data/{3306,3307}/

步骤一:多实例配置文件,注意脚本要给继续权限才能够正常使用
[root@centos ~]# touch /data/{3306,3307}/my.cnf
[root@centos ~]# touch /data/{3306,3307}/mysql
[root@centos ~]# tree /data/
/data/
├── 3306
│   ├── my.cnf
│   └── mysql
├── 3307
│   ├── my.cnf
│   └── mysql
└── lost+found

3 directories, 4 files
[root@centos ~]# cat /data/3306/my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock

[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
log-bin = /data/3306/mysql-bin
server-id = 6

[mysqld_safe]
log-error=/data/3306/mysql_3306.err
pid-file=/data/3306/mysqld.pid

[root@centos ~]# cat /data/3307/my.cnf
[client]
port = 3307
socket = /data/3307/mysql.sock

[mysqld]
user = mysql
port = 3307
socket = /data/3307/mysql.sock
basedir = /application/mysql
datadir = /data/3307/data
log-bin = /data/3307/mysql-bin
server-id = 7

[mysqld_safe]
log-error=/data/3307/mysql_3307.err
pid-file=/data/3307/mysqld.pid

[root@centos ~]# cat /data/3306/mysql
#!/bin/sh
#3306 start scripts
#init
port=3306
mysql_user="root"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path=/data/3306/3306.pid
start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
sleep 3
else
printf "MySQL is running...\n"
exit 1
fi
}
stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit 1
else
printf "Stoping MySQL...\n"
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
fi
}

restart(){
printf "Restarting MySQL...\n"
stop
sleep 2
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

[root@centos ~]# cat /data/3307/mysql
#!/bin/sh
#3307 start scripts
#init
port=3307
mysql_user="root"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path=/data/3307/3307.pid
start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
sleep 3
else
printf "MySQL is running...\n"
exit 1
fi
}
stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit 1
else
printf "Stoping MySQL...\n"
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
fi
}

restart(){
printf "Restarting MySQL...\n"
stop
sleep 2
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

步骤二:创建数据目录并授权
[root@centos ~]# mkdir /data/{3306,3307}/data -p
[root@centos ~]# chown -R mysql.mysql /data/

步骤三:初始化数据
[root@centos ~]# cd /application/mysql/scripts/
[root@centos scripts]# ./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/application/mysql --datadir=/data/3306/data/ --user=mysql

[root@centos scripts]# pwd
/application/mysql/scripts
[root@centos scripts]# ./mysql_install_db --defaults-file=/data/3307/my.cnf --basedir=/application/mysql --datadir=/data/3307/data/ --user=mysql

步骤四:创建日志文件,需要创建错误日志文件
[root@centos ~]# touch /data/3306/mysql_3306.err
[root@centos ~]# touch /data/3307/mysql_3307.err

步骤五:启动多实例
[root@centos ~]# netstat -lntup|grep mysqld
[root@centos ~]# /data/3306/mysql start
Starting MySQL...
[root@centos ~]# /data/3307/mysql start
Starting MySQL...
[root@centos ~]# netstat -lntup|grep mysqld //检查端口信息
tcp 0 0 :::3306 :::* LISTEN 4765/mysqld
tcp 0 0 :::3307 :::* LISTEN 4973/mysqld

[root@centos ~]# ps -ef|grep [my]sql //检查mysql状态
root 4580 1 0 10:27 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf --pid-file=/data/3306/3306.pid
mysql 4765 4580 3 10:27 pts/0 00:00:04 /application/mysql/bin/mysqld --defaults-file=/data/3306/my.cnf --basedir=/application/mysql --datadir=/data/3306/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3306/mysql_3306.err --pid-file=/data/3306/3306.pid --socket=/data/3306/mysql.sock --port=3306
root 4788 1 0 10:27 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf --pid-file=/data/3307/3307.pid
mysql 4973 4788 3 10:27 pts/0 00:00:03 /application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf --basedir=/application/mysql --datadir=/data/3307/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3307/mysql_3307.err --pid-file=/data/3307/3307.pid --socket=/data/3307/mysql.sock --port=3307
//至此mysql的多实例就配置完成!

(6)多实例mysql的登录使用
[root@centos ~]# mysql -S /data/3306/mysql.sock
......
mysql>
[root@centos ~]# mysql -S /data/3307/mysql.sock
......
mysql>

(7)关于mysql多实例的选择
1、资金紧张性公司的选择
  资金紧张,公司业务访问量又不大,但又希望不同的业务的数据库服务各自尽量独立
2、并发访问不是特别大的业务
  当公司业务访问量不太大的时候,服务器的资源基本都是浪费的,这就适合多实例的使用
3、门户网站应用MySQL多实例场景
  配置硬件好的服务器,可以节省IDC机柜空间,跑多实例也不会减少硬件资源不慢的浪费
  一般是从库多实例,例如:某部门使用IBM服务器为48核CPU,内存96GB、一台服务器跑3~4个实例:此外,sina网也是用的多实例,内存48GB左右。企业环境中一般将多实例应用在测试环境。

(8)mysql多实例设置密码登录
---------------------------------------
[root@centos ~]# netstat -lntup|grep mysqld
tcp 0 0 :::3306 :::* LISTEN 4765/mysqld
tcp 0 0 :::3307 :::* LISTEN 4973/mysqld
[root@centos ~]# mysql -S /data/3306/mysql.sock
......
mysql> quit
Bye
[root@centos ~]# mysqladmin -u root -S /data/3306/mysql.sock password 'oldboy123'
Warning: Using a password on the command line interface can be insecure.
[root@centos ~]# mysql -S /data/3306/mysql.sock
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@centos ~]# mysql -u root -poldboy123 -S /data/3306/mysql.sock
......
mysql>

posted @ 2017-12-21 15:18  bkycrmn  阅读(125)  评论(0)    收藏  举报