1. 程式人生 > >24.0 資料庫安裝

24.0 資料庫安裝

資料的時代
    涉及的資料量大
    資料不隨程式的結束而消失   
    資料被多個應用程式共享   
    大資料 


資料庫的發展史
    萌芽階段:檔案系統
        使用磁碟檔案來儲存資料
    初級階段:第一代資料庫  
        出現了網狀模型、層次模型的資料庫   
    中級階段:第二代資料庫  
        關係型資料庫和結構化查詢語言   
    高階階段:新一代資料庫
        “關係-物件”型資料庫 

檔案管理系統的缺點
    編寫應用程式不方便   
    資料冗餘不可避免   
    應用程式依賴性
    不支援對檔案的併發訪問   
    資料間聯絡弱
    難以按使用者視圖表示資料  
    無安全控制功能 


資料庫管理系統的優點
    相互關聯的資料的集合   
    較少的資料冗餘
    程式與資料相互獨立   
    保證資料的安全、可靠
    最大限度地保證資料的正確性
    資料可以併發使用並能同時保證一致性 


資料庫管理系統
    資料庫是資料的彙集,它以一定的組織形式存於儲存介質上
    DBMS是管理資料庫的系統軟體,它實現資料庫系統的各種功能。是資料庫系統的核心
    DBA:負責資料庫的規劃、設計、協調、維護和管理等工作   
    應用程式指以資料庫為基礎的應用程式 

資料庫管理系統的基本功能
    資料定義   
    資料處理   
    資料安全   
    資料備份 

資料庫系統的架構
    單機架構
    大型主機/終端架構   
    主從式架構(C/S)   
    分散式架構 

關係型資料庫
    關係 :關係就是二維表。並滿足如下性質:
    表中的行、列次序並不重要
    行row:表中的每一行,又稱為一條記錄  record
    列column:表中的每一列,稱為屬性,欄位   field
    主鍵(Primary key):用於惟一確定一個記錄的欄位
    域domain:屬性的取值範圍,如,性別只能是‘男’和‘女’兩個值 

關係資料庫
RDBMS:
MySQL: MySQL, MariaDB, Percona Server #是mysql分支
PostgreSQL: 簡稱為pgsql,EnterpriseDB   紅帽
Oracle 
MSSQL              IBM
DB2             
資料庫排名:
    https://db-engines.com/en/ranking

實體-聯絡模型E-R
    實體Entity
    •    客觀存在並可以相互區分的客觀事物或抽象事件稱為實體。 
    •    在E-R圖中用矩形框表示實體,把實體名寫在框內   
    屬性
    •    實體所具有的特徵或性質   
    聯絡
    聯絡是資料之間的關聯集合,是客觀存在的應用語義鏈
        實體內部的聯絡:指組成實體的各屬性之間的聯絡。如職工實體中,職工號和部門經理號之間有一種關聯關係
        實體之間的聯絡:指不同實體之間聯絡。例學生選課實體和學生基本資訊實體之間
        實體之間的聯絡用菱形框表示 


聯絡型別
    一對一聯絡(1:1)   
    一對多聯絡(1:n)   主外來鍵
    多對多聯絡(m:n)   構建第三張表

事務
事務Transactions:一組原子性的SQL語句,或一個獨立工作單元   
事務日誌:記錄事務資訊,實現undo,redo等故障恢復功能   
ACID特性:
A:atomicity原子性;整個事務中的所有操作要麼全部成功執行,要麼全部失敗後回滾
C:consistency一致性;資料庫總是從一個一致性狀態轉換為另一個一致性狀態
I:Isolation隔離性;一個事務所做出的操作在提交之前,是不能為其它事務所見;隔離有多種隔離級別,實現併發
D:durability永續性;一旦事務提交,其所做的修改會永久保存於資料庫中 

2進位制安裝包下載
https://downloads.mariadb.org/mariadb/10.2.18/

https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/


yum安裝
# MariaDB 10.3 CentOS repository list - created 2018-10-06 09:05 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

國外太慢
[mariadb]
name = MariaDB
baseurl = https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.2/centos/7/x86_64/
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1


[[email protected] ~ ]#yum install mariadb-server
'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h master01 password 'new-password'


[[email protected] ~ ]#rpm -ql MariaDB-server
[[email protected] ~ ]#systemctl start mariadb
[[email protected] ~ ]#mysql
MariaDB [(none)]> help

General information about MariaDB can be found at
http://mariadb.org

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'


MariaDB [(none)]> help contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Procedures
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility

MariaDB [(none)]> show databases;    #伺服器端命令,傳送到服務端執行
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.003 sec)

MariaDB [(none)]> \u test     #客戶端命令,可以不加分號

[[email protected] ~ ]#mysql --help
[[email protected] ~ ]#mysql
MariaDB [(none)]> select user();   #賬號名是使用者名稱加上主機名
+----------------+
| user()         |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.000 sec)

[[email protected] ~ ]#mysql -uxyz -p     #匿名使用者,使用者賬號任意
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.10-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.001 sec)


[[email protected] ~ ]#mysql mysql
MariaDB [mysql]> show tables;
MariaDB [mysql]> desc user;      #檢視user表的列

MariaDB [mysql]> select user,host,password from user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | master01  |          |
| root | 127.0.0.1 |          |
| root | ::1       |          |
|      | localhost |          |
|      | master01  |          |
+------+-----------+----------+
6 rows in set (0.010 sec)


[[email protected] ~ ]#which mysql_secure_installation
/usr/bin/mysql_secure_installation

[[email protected] ~ ]#rpm -qf /usr/bin/mysql_secure_installation
MariaDB-server-10.3.10-1.el7.centos.x86_64

[[email protected] ~ ]#mysql_secure_installation
Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password: 
Re-enter new password:
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] 
Reload privilege tables now? [Y/n] y
Thanks for using MariaDB!

Thanks for using MariaDB!
[[email protected] ~ ]#mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[[email protected] ~ ]#mysql -uroot -p
Enter password: 

[[email protected] ~ ]#mysql -uroot -pcentos mysql #此法密碼暴露了
MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *128977E278358FF80A246B5046F51043A2B1FCED |
| root | 127.0.0.1 | *128977E278358FF80A246B5046F51043A2B1FCED |
| root | ::1       | *128977E278358FF80A246B5046F51043A2B1FCED |
+------+-----------+-------------------------------------------+
3 rows in set (0.000 sec)

#上述的密碼都是一樣的,加slat(鹽)讓相同的密碼顯示出來不一樣比較好

使用者賬號
    mysql使用者賬號由兩部分組成: 
     'USERNAME'@'HOST‘   
    說明:
    HOST限制此使用者可通過哪些遠端主機連線mysql伺服器 
    支援使用萬用字元:
    % 匹配任意長度的任意字元  
    172.16.0.0/255.255.0.0 或 172.16.%.%
     _ 匹配任意單個字元 

[[email protected] ~ ]#mysql -uroot -pcentos -h 192.168.31.7
ERROR 1130 (HY000): Host 'master01' is not allowed to connect to this MariaDB server
[[email protected] ~ ]#mysql -uroot -pcentos -h 127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.3.10-MariaDB MariaDB Server


https://downloads.mariadb.org/mariadb/10.2.16/

原始碼
mariadb-10.2.16.tar.gz
二進位制
mariadb-10.2.16-linux-x86_64.tar.gz



rpm包安裝mariadb=========================================================開始
二進位制安裝,原始碼已經編譯好,但配置檔案需要自己配置好


[[email protected]:~]# fdisk /dev/sdb
型別L
8e  LVM
[[email protected]:~]# partprobe
[[email protected]:~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
└─sda2   8:2    0  199G  0 part /
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    1G  0 part 
sr0     11:0    1  8.8G  0 rom  

[[email protected]:~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[[email protected]:~]# vgcreate vg0 /dev/sdb1
  Volume group "vg0" successfully created
[[email protected]:~]# vgdisplay

[[email protected]:~]# lvcreate -n mysql -l +100%FREE vg0 
  Logical volume "mysql" created.
[[email protected]:~]# lvdisplay

[[email protected]:~]# mkfs.xfs /dev/vg0/mysql 
[[email protected]:~]# blkid
/dev/sr0: UUID="2018-05-07-12-53-47-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="38ac8148-66d5-47ee-bd6d-ce50ca048533" TYPE="xfs" 
/dev/sda2: UUID="fbe53d4c-ab79-4207-a4a7-e370854f796e" TYPE="xfs" 
/dev/sdb1: UUID="Dr7ezb-WKku-kIta-S01Y-z0hb-43BU-DVq8Q1" TYPE="LVM2_member" 
/dev/mapper/vg0-mysql: UUID="20b97303-99f7-410b-be56-9ae8e06445fd" TYPE="xfs" 

[[email protected]:~]# mount /dev/vg0/mysql /app


[[email protected]:~]# vim /etc/fstab 
r!blkid /dev/vg0/mysql
UUID=20b97303-99f7-410b-be56-9ae8e06445fd /app                     xfs    defaults        0 0 


[[email protected]:~]# useradd -r -s /sbin/nologin -d /app/mysql -m mysql

#[[email protected]:~]# ll /app/mysql/ -d
#drwx------ 3 mysql mysql 78 Jul 25 19:52 /app/mysql/

[[email protected]:~]# tar xvf mariadb-10.2.16-linux-x86_64.tar.gz -C /usr/local/
[[email protected]:~]# cd /usr/local/
[[email protected]:local]# ln -s mariadb-10.2.16-linux-x86_64/ mysql
#[[email protected]:local]# ll
#drwxrwxr-x  12 1021 1004 290 Jun 26 02:07 mariadb-10.2.16-linux-x86_64
[[email protected]:local]# chown -R root.mysql /usr/local/mysql/
[[email protected]:local]# vim /etc/profile.d/mysql.sh
PATH=/usr/local/mysql/bin:$PATH   
[[email protected]:local]# . /etc/profile.d/mysql.sh 
#[[email protected]:local]# echo $PATH
#/usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

資料庫路徑
[[email protected]:local]# cd /usr/local/mysql/
[[email protected]:mysql]# ls scripts/
mysql_install_db

[[email protected]:mysql]# scripts/mysql_install_db --datadir=/app/mysql --user=mysql

[[email protected]:mysql]# ll /etc/my.cnf
-rw-r--r--. 1 root root 570 Jun  8  2017 /etc/my.cnf
[[email protected]:mysql]# rpm -qf /etc/my.cnf
mariadb-libs-5.5.56-2.el7.x86_64
[[email protected]:mysql]# cat /etc/my.cnf
#摸版
[[email protected]:mysql]# ls support-files/
[[email protected]:mysql]# cp support-files/my-huge.cnf /etc/my.cnf
[[email protected]:mysql]# vim /etc/my.cnf
datadir         = /app/mysql 

啟動指令碼,有現成的

[[email protected]:mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected]:mysql]# chkconfig --list
[[email protected]:mysql]# chkconfig --add mysqld
[[email protected]:mysql]# service mysqld start
[[email protected]:mysql]# ss -ntl
3306

二進位制安裝完成
[[email protected]:mysql]# mysql_secure_installation #安全加固

***********************************************結束


================================================開始
原始碼編譯安裝
[[email protected]:~/mariadb-10.2.16 ]# yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel 

[[email protected]:~ ]# useradd -r -d /data/mysql -s /sbin/nologin mysql
[[email protected]:~ ]# ll -d /data/mysql
drwxr-xr-x 2 root root 6 Jul 25 20:30 /data/mysql
[[email protected]:~ ]# chown mysql.mysql /data/mysql/
[[email protected]:~ ]# ll -d /data/mysql
drwxr-xr-x 2 mysql mysql 6 Jul 25 20:30 /data/mysql
[[email protected]:~ ]# tar xvf mariadb-10.2.16.tar.gz 
編譯

[[email protected]:~ ]# cd mariadb-10.2.16/

?    cd mariadb-10.2.15/ 
cmake . \                                
-DCMAKE_INSTALL_PREFIX=/app/mysql \      
-DMYSQL_DATADIR=/data/mysqldb/ \         
-DSYSCONFDIR=/etc \                      
-DMYSQL_USER=mysql \                     
-DWITH_INNOBASE_STORAGE_ENGINE=1 \       
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \        
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \      
-DWITH_PARTITION_STORAGE_ENGINE=1 \      
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \     
-DWITH_DEBUG=0 \                         
-DWITH_READLINE=1 \                      
-DWITH_SSL=system \                      
-DWITH_ZLIB=system \                     
-DWITH_LIBWRAP=0 \                       
-DENABLED_LOCAL_INFILE=1 \               
-DMYSQL_UNIX_ADDR=/app/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \                 
-DDEFAULT_COLLATION=utf8_general_ci      

[[email protected] mariadb-10.2.15] make -j 4 && make install 

#二進位制已經安裝到此目錄
/usr/local/mysql
[[email protected]:~ ]# cd /app/mysql/
[[email protected]:/app/mysql ]# ls
[[email protected]:/app/mysql ]# echo PATH=/app/mysql/bin:$PATH > /etc/profile.d/mysql.sh
[[email protected]:/app/mysql ]# . /etc/profile.d/mysql.sh
[[email protected]:/app/mysql ]# echo $PATH
[[email protected]:/app/mysql ]# mkdir /data/mysqldb/ -pv
mkdir: created directory ‘/data/mysqldb/’
[[email protected]:/app/mysql ]# chown mysql.mysql /data/mysqldb/
[[email protected]:~ ]# getent passwd mysql
mysql:x:989:984::/data/mysql:/sbin/nologin
[[email protected]:/app/mysql ]# ./scripts/mysql_install_db --datadir=/data/mysqldb --user=mysql
[[email protected]:/app/mysql ]# cp support-files/my-huge.cnf /etc/my.cnf
[[email protected]:/app/mysql ]# vim /etc/my.cnf
[mysqld]
datadir         = /data/mysqldb/            #新增此行

[[email protected]:/app/mysql ]# chown -R mysql.mysql /app/mysql/
[[email protected]:/app/mysql ]# cp support-files/mysql.server  /etc/init.d/mysqld
[[email protected]:/app/mysql ]# chkconfig --add mysqld
[[email protected]7:/app/mysql ]# chkconfig --list
[[email protected]:/app/mysql ]# service mysql start
Redirecting to /bin/systemctl start mysql.service
[[email protected]:/app/mysql ]# ss -ntl
[[email protected]:/app/mysql ]# mysql
#安全加固
[[email protected]:/app/mysql ]# mysql_secure_installation 

=========================================================
一個伺服器上跑多個mariadb資料庫
二進位制安裝多例項

實驗:yum安裝多例項
[[email protected]:~]# rpm -q mariadb-server
package mariadb-server is not installed


1準備目錄

[[email protected]:~]# mkdir /mysqldb/{3306,3307,3308}/{etc,bin,data,pid,socket,log} -pv
[[email protected]:~]# tree /mysqldb/
#沒有mysql賬號
[[email protected]:~]# getent passwd mysql
#先yum源安裝一個mariadb-server,不啟動服務
[[email protected]:~]# yum install mariadb-server
[[email protected]:~]# chown -R mysql.mysql /mysqldb/

2 生成資料庫檔案
mysql_install_db  --datadir=/mysqldb/3306/data --user=mysql
 mysql_install_db  --datadir=/mysqldb/3307/data --user=mysql
 mysql_install_db  --datadir=/mysqldb/3308/data --user=mysql
 [[email protected]:~]# tree /mysqldb/

 
3準備配置檔案
[[email protected]:~]# cp /etc/my.cnf /mysqldb/3306/etc/
vim /mysqldb/3306/etc/my.cnf 
[mysqld]
port=3306    #預設3306可以不寫
datadir= /mysqldb/3306/data
socket=/mysqldb/3306/socket/mysql.sock
[mysqld_safe]
log-error=/mysqldb/3306/log/mariadb.log
pid-file=/mysqldb/3306/pid/mariadb.pid
 
cp /mysqldb/3306/etc/my.cnf /mysqldb/3307/etc/
cp /mysqldb/3306/etc/my.cnf /mysqldb/3308/etc/
sed -i 's/3306/3307/g' /mysqldb/3307/etc/my.cnf
sed -i 's/3306/3308/g' /mysqldb/3308/etc/my.cnf

#生成啟動指令碼可參考安裝的mariadb的
#[[email protected]:~]# rpm -ql mariadb-server
#[[email protected]:~]# cat /usr/lib/systemd/system/mariadb.service

4生成啟動指令碼
 cp mysqld /mysqldb/3306/bin/   
 #mysql指令碼在本節中
 vim /mysqldb/3306/bin/mysqld 
 cmd_path="/usr/bin"
chmod +x /mysqldb/3306/bin/mysqld

5啟動服務
/mysqldb/3306/bin/mysqld start
#[[email protected]:~]# mysql
#ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
#[[email protected]:~]# cat /etc/my.cnf.d/client.cnf 
#[[email protected]:~]# mysql -S /mysqldb/3306/socket/mysql.sock

[[email protected]:~]# cp /mysqldb/3306/bin/mysqld /mysqldb/3307/bin/
[[email protected]:~]# cp /mysqldb/3306/bin/mysqld /mysqldb/3308/bin/
[[email protected]:~]# vim /mysqldb/3307/bin/mysqld 
port=3307
[[email protected]:~]# vim /mysqldb/3308/bin/mysqld 
port=3308

/mysqldb/3307/bin/mysqld  start
/mysqldb/3308/bin/mysqld  start
/mysqldb/3306/bin/mysqld  stop
#[[email protected]:~]# /mysqldb/3306/bin/mysqld  stop                             
#Stoping MySQL...
#/usr/bin/mysqladmin: connect to server at 'localhost' failed
#error: 'Access denied for user 'root'@'localhost' (using password: YES)'
#修改root口令
#[[email protected]:~]# mysqladmin -S /mysqldb/3306/socket/mysql.sock password 'centos'
#[[email protected]:~]# /mysqldb/3306/bin/mysqld  stop
#Stoping MySQL...

/mysqldb/3307/bin/mysqld  stop
/mysqldb/3308/bin/mysqld  stop

 =================================================結束


================================================開始
原始碼編譯多例項
基於上述的原始碼已經編譯好,下面開始多例項配置
[[email protected]:~ ]# mkdir /mysqldb/{3306,3307,3308}/{etc,bin,data,pid,socket,log} -pv
#path變數原始碼編譯已經自動配置好
#[[email protected]:~ ]# echo $PATH
#/usr/local/mysql/bin:/app/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
#安裝資料庫檔案
[[email protected]:/app/mysql ]# scripts/mysql_install_db --datadir=/mysqldb/3306/data --user=mysql
安裝上述命令時的安裝記錄有如下兩行
#'./bin/mysqladmin' -u root password 'new-password'
[[email protected]:/app/mysql ]# scripts/mysql_install_db --datadir=/mysqldb/3307/data --user=mysql
[[email protected]:/app/mysql ]# scripts/mysql_install_db --datadir=/mysqldb/3308/data --user=mysql
[[email protected]:~ ]# vim /mysqldb/3306/etc/my.cnf 
[mysqld]
port=3306
datadir=/mysqldb/3306/data
socket=/mysqldb/3306/socket/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
# symbolic-links=0
# # Settings user and group are ignored when systemd is used.
# # If you need to run mysqld under a different user or group,
# # customize your systemd unit file for mariadb according to the
# # instructions in http://fedoraproject.org/wiki/Systemd
#
 [mysqld_safe]
 log-error=/mysqldb/3306/log/mariadb.log
 pid-file=/mysqldb/3306/pid/mariadb.pid
#
# #
# # include all files from the config directory
# #
# !includedir /etc/my.cnf.d
~                            
[[email protected]:~ ]# cp /mysqldb/3306/etc/my.cnf /mysqldb/3307/etc/
[[email protected]:~ ]# cp /mysqldb/3306/etc/my.cnf /mysqldb/3308/etc/

[[email protected]:/mysqldb/3306/bin ]# ./mysqld start
[[email protected]:/mysqldb/3306/bin ]# ./mysqld stop
MySQL is stopped...

#mysqladmin' -S /mysqldb/3306/socket/mysql.sock  password 'centos'


===================================================結束

mysql指令碼=========================開始

[[email protected]:/app/mysql ]# cat /mysqldb/3306/bin/mysqld 
#!/bin/bash

port=3306
mysql_user="root"
mysql_pwd="centos"
cmd_path="/app/mysql/bin"
mysql_basedir="/mysqldb"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
    else
      printf "MySQL is running...\n"
      exit
    fi
}


function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}


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: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac


==========================================================================結束