# cd /data/3306/ # vim my.cnf [client] port = 3306 socket = /data/3306/mysql.sock [mysqld] port=3306 socket = /data/3306/mysql.sock pid-file = /data/3306/data/mysql.pid basedir = /usr/local/mysql datadir = /data/3306/data server-id=1 #log-bin=mysql-bin #log-bin-index= mysql-bin.index # LOGGING log_error=/data/3306/log/mysql-error.log slow_query_log_file=/data/3306/log/mysql-slow.log slow_query_log=1
5. /data/3307中新建my.cnf
# cd ../3307/ # vim my.cnf [client] port = 3307 socket = /data/3307/mysql.sock [mysqld] port=3307 socket = /data/3307/mysql.sock pid-file = /data/3307/data/mysql.pid basedir = /usr/local/mysql datadir = /data/3307/data server-id=2 #log-bin=mysql-bin #log-bin-index= mysql-bin.index # LOGGING log_error=/data/3307/log/mysql-error.log slow_query_log_file=/data/3307/log/mysql-slow.log slow_query_log=1
6. 在/data/3306中新建mysql啟動文件
# cd /data/3306/ # vim mysql #!/bin/sh [ -f /etc/init.d/functions ] && . /etc/init.d/functions port=3306 mysql_user="root" mysql_pwd="" mysql_sock="/data/${port}/mysql.sock" CmdPath="/usr/local/mysql/bin" #startup function usage(){ printf "Usage: /data/${port}/mysql {start|stop|restart}\n" } function_start_mysql() { if [ ! -e "$mysql_sock" ];then /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & action "Starting MySQL..." /bin/true 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 action "Stoping MySQL..." /bin/true ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown fi } #restart function function_restart_mysql() { function_stop_mysql &>/dev/null sleep 2 function_start_mysql &>/dev/null action "Restarting MySQL..." /bin/true } if [ $# -ne 1 ];then usage fi case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) usage esac #chmod +x mysql
7、在/data/3307中新建mysql啟動文件
# cd /data/3307/ # vim mysql #!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions port=3307 mysql_user="root" mysql_pwd="" mysql_sock="/data/${port}/mysql.sock" CmdPath="/usr/local/mysql/bin" usage(){ printf "Usage: /data/${port}/mysql {start|stop|restart}\n" } #startup function function_start_mysql() { if [ ! -e "$mysql_sock" ];then /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & action "Starting MySQL..." /bin/true 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 action "Stoping MySQL..." /bin/true ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown fi } #restart function function_restart_mysql() { function_stop_mysql &>/dev/null sleep 2 function_start_mysql &>/dev/null action "Restarting MySQL..." /bin/true } case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) usage esac #chmod +x mysql
8. 修改文件擁有者和權限 #chown -R mysql:mysql /data 9. 添加mysql啟動路徑 #echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile #source /etc/profile 10. 初始化數據庫 # cd /usr/local/mysql/scripts/ #./mysql_install_db --defaults-file=/data/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data #./mysql_install_db --defaults-file=/data/3307/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data 11. 啟動mysql #/data/3306/mysql start #/data/3307/mysql start #netstat -lntp | grep 330 #查看是否啟動進程 12. 登陸mysql ①# mysql -S /data/3306/mysql.sock #剛安裝完的mysql是沒有登陸密碼的 #如果不成功,檢查/data/3306/log目錄下的mysql-error.log日志,逐一排除錯誤 如果登陸成功,下面就修改登錄密碼 mysql> update mysql.user set password=password("123456") where user='root'; mysql> flush privileges; 不建議在shell環境下修改密碼,否則別人只要查看命令歷史就能看到密碼。當然你也可以是shell下進行,但是記得要清楚歷史命令記錄。 ②同理,使用上面的方法修改3307的登陸密碼 # mysql -S /data/3307/mysql.sock mysql> update mysql.user set password=password("123456") where user='root'; mysql> flush privileges; ③要把上面更改後的密碼寫回到mysql的啟動文件中(否則每次啟動、關閉、重啟mysql都要輸入密碼) # sed -i 's/mysql_pwd=\"\"/mysql_pwd=\"123456\"/g' /data/3306/mysql #sed -i 's/mysql_pwd=\"\"/mysql_pwd=\"123456\"/g' /data/3307/mysql 也可以手動進行修改 #vim /data/3306/mysql # vim /data/3307/mysql 13. 重啟mysql #/data/3306/mysql restart #/data/3307/mysql restart #netstat -lntp | grep 330 14.知識點 進入mysql時,要記得加-S 指定mysql套接字的路徑 # mysql –u root –p -S /data/3307/mysql.sock 下面的命令可以平滑關閉mysql # mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown