CentOS7安裝PHP開發環境4-源碼安裝MySQL5.6

軟件版本
OS:CentOS 7
MySQL:5.6.44

安裝

下載mysql 5.6.44
下載頁面:https://dev.mysql.com/downloads/mysql/5.6.html#downloads
下載地址:https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz
下載地址:https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.44.tar.gz
在這裏插入圖片描述

安裝MySQL需要的依賴包和編譯軟件

# yum install ncurses-devel libaio-devel cmake -y

檢測是否已安裝

首先檢查是否已經安裝過mysql:

# rpm -qa | grep mysql

有的話就卸載掉以前安裝的mysql:

# rpm -e --nodeps xxx(xxx是搜索結果)

並刪除所有的相關文件:/etc/my.cnf(可用有 mv /etc/my.cnf /etc/my.cnf.back)

建立MySQL用戶賬戶

創建mysql用戶(但是不能使用mysql賬號登陸系統)(要修改成如下方式)
檢查系統是否已經有mysql用戶,

# cat /etc/passwd | grep mysql
# cat /etc/group | grep mysql

如果沒有則創建

# groupadd mysql
# useradd -s /sbin/nologin -g mysql mysql

或者使用如下命令創建

# useradd -s /sbin/nologin -M mysql  #<==默認會創建和mysql用戶同名的組。

解壓並配置MySQL,命令及操作

# tar zxvf mysql-5.6.44.tar.gz
# cd mysql-5.6.44.tar.gz
# cmake -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.44 \
-DMYSQL_DATADIR=/application/mysql-5.6.44/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.44/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_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

編譯並安裝MySQL

如果是多核CPU,則可指定make -j CPU核數,加快編譯速度。

# make
# make install

爲MySQL安裝路徑設置不帶版本號的軟連接

爲MySQL安裝路徑設置不帶版本號的軟連接/application/mysql,操作命令如下

# ln -s /application/mysql-5.6.44 /application/mysql

創建MySQL數據庫配置文件並對數據庫目錄授權

MySQL 5.5及老版數據庫默認爲用戶提供了多個配置文件模板,但是MySQL 5.6的support-files目錄下已經沒有配置文件模板了。

# ll support-files/*.cnf
-rw-r--r--. 1 mysql mysql 1126 Jul 21 12:02 support-files/my-default.cnf
# mv /etc/my.cnf /etc/my.cnf.bak

提示:在CentOS 6.6版操作系統最小化安裝完成之後,在/etc目錄下會存在一個my.cnf,需要將此文件更名爲其他的名字,如:/etc/my.cnf.bak,否則,該文件會干擾源碼安裝的MySQL的正確配置,造成無法啓動。
在啓動MySQL服務時,會按照一定的順序搜索my.cnf,先在/etc目錄下找,若找不到則搜索"$basedir/my.cnf",本例依舊選擇大家熟悉的/etc/路徑!

# cp support-files/my-default.cnf /etc/my.c

提示:此行操作可以省略,在下文初始化mysql時會自動生成my.cnf模板文件,如果已經執行了上述命令,則初始化後會生成my-new.cnf文件,my.cnf和my-new.cnf除了註釋以外是一致的。

# chown -R mysql.mysql /application/mysql/

#<==授權mysql用戶管理mysql的安裝目錄,此步不做會導致啓動服務錯誤。

MySQL相關命令加入全局路徑的配置

# vim /etc/profile
PATH=/application/mysql/bin:$PATH

Esc :wq

# source /etc/profile

單實例配置

初始化MySQL單實例的數據庫

執行初始化配置腳本,創建系統自帶的數據庫和表

[root@www mysql]# /application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql/ \
--datadir=/application/mysql/data \
--user=mysql

提示: --basedir=/application/mysql/爲MySQL的安裝路徑, --datadir爲數據文件目錄。另外,需要注意mysql_install_db和MySQL 5.1的路徑不同,MySQL 5.1在MySQL bin路徑下。

初始化的操作過程

[root@www mysql]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql
WARNING: The host 'www' could not be looked up with /application/mysql//bin/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...2019-07-23 12:23:28 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-23 12:23:28 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-07-23 12:23:28 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.44) starting as process 4517 ...
2019-07-23 12:23:28 4517 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-23 12:23:28 4517 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-23 12:23:28 4517 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-23 12:23:28 4517 [Note] InnoDB: Memory barrier is not used
2019-07-23 12:23:28 4517 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-07-23 12:23:28 4517 [Note] InnoDB: Using Linux native AIO
2019-07-23 12:23:28 4517 [Note] InnoDB: Using CPU crc32 instructions
2019-07-23 12:23:28 4517 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-23 12:23:28 4517 [Note] InnoDB: Completed initialization of buffer pool
2019-07-23 12:23:28 4517 [Note] InnoDB: Highest supported file format is Barracuda.
2019-07-23 12:23:29 4517 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-23 12:23:29 4517 [Note] InnoDB: Waiting for purge to start
2019-07-23 12:23:29 4517 [Note] InnoDB: 5.6.44 started; log sequence number 1600617
2019-07-23 12:23:29 4517 [Note] Binlog end
2019-07-23 12:23:29 4517 [Note] InnoDB: FTS optimize thread exiting.
2019-07-23 12:23:29 4517 [Note] InnoDB: Starting shutdown...
2019-07-23 12:23:31 4517 [Note] InnoDB: Shutdown completed; log sequence number 1625997
OK

Filling help tables...2019-07-23 12:23:31 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-23 12:23:31 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-07-23 12:23:31 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.44) starting as process 4541 ...
2019-07-23 12:23:31 4541 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-23 12:23:31 4541 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-23 12:23:31 4541 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-23 12:23:31 4541 [Note] InnoDB: Memory barrier is not used
2019-07-23 12:23:31 4541 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-07-23 12:23:31 4541 [Note] InnoDB: Using Linux native AIO
2019-07-23 12:23:31 4541 [Note] InnoDB: Using CPU crc32 instructions
2019-07-23 12:23:31 4541 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-23 12:23:31 4541 [Note] InnoDB: Completed initialization of buffer pool
2019-07-23 12:23:31 4541 [Note] InnoDB: Highest supported file format is Barracuda.
2019-07-23 12:23:31 4541 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-23 12:23:31 4541 [Note] InnoDB: Waiting for purge to start
2019-07-23 12:23:31 4541 [Note] InnoDB: 5.6.44 started; log sequence number 1625997
2019-07-23 12:23:31 4541 [Note] Binlog end
2019-07-23 12:23:31 4541 [Note] InnoDB: FTS optimize thread exiting.
2019-07-23 12:23:31 4541 [Note] InnoDB: Starting shutdown...
2019-07-23 12:23:33 4541 [Note] InnoDB: Shutdown completed; log sequence number 1626007
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:

  /application/mysql//bin/mysqladmin -u root password 'new-password'
  /application/mysql//bin/mysqladmin -u root -h www password 'new-password'

Alternatively you can run:

  /application/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 . ; /application/mysql//bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /application/mysql//my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

初始化mysql數據庫文件,會有很多信息提示,如果沒有ERROR級別的錯誤,有兩個OK的字樣,則表示初始化成功,否則就要解決初始化的問題。

配置並啓動MySQL數據庫

方式一
啓動MySQL

[root@www mysql]# /application/mysql/support-files/mysql.server start
Starting MySQL. SUCCESS!

關閉MySQL

[root@www mysql]# /application/mysql/support-files/mysql.server stop
Shutting down MySQL.. SUCCESS!

方式二
啓動MySQL

[root@www mysql]# /application/mysql/bin/mysqld_safe --user=mysql &

關閉MySQL

[root@www mysql]# /application/mysql/support-files/mysql.server stop

特別提示:這裏有一個大坑,就是數據庫可能會無法啓動,報錯信息如下:

[root@www mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql-5.6.44/data/oldboy.err'.
180510 00:21:04 mysqld_safe Directory '/application/mysql-5.6.44/tmp' for UNIX socket file don't exists.
 ERROR! The server quit without updating PID file (/application/mysql-5.6.44/data/oldboy.pid).

提示:/application/mysql-5.6.44/tmp目錄不存在,5.6.34前的早期版本沒事。
解決辦法:

# mkdir -p /application/mysql-5.6.44/tmp

#<==編譯時指定的socket路徑。

# chown -R mysql.mysql /application/mysql/
# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

提示:禁止使用kill -9、killall -9等命令強制殺死數據庫,這會引起數據庫無法啓動等故障發生。企業中曾發生過的血的教訓案例請看 http://oldboy.blog.51cto.com/2561410/1431161

設置MySQL開機自啓動

檢查MySQL數據庫是否啓動

[root@www mysql]# netstat -lntup|grep mysql
tcp      0      0 :::3306      :::*      LISTEN      4797/mysqld

如果發現3306端口沒起來,則請使用tail-100/application/mysql/data/機器名.err 檢查日誌報錯進行調試。

設置MySQL開機自啓動

# mv /etc/my.cnf /etc/my.cnf.bak
# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --list mysqld
mysqld               0:off       1:off      2:on      3:on      4:on      5:on      6:off

chkconfig mysqld on #設置開機啓動
chkconfig mysqld off #禁止開機啓動
service mysqld start #啓動MySQL服務
service mysqld stop #關閉MySQL服務
service mysqld restart #重啓MySQL
netstat -lntup|grep mysql

此外,將啓動命令/etc/init.d/mysqld start放到/etc/rc.local裏面也可以實現開機自啓動。
若MySQL安裝及使用出現故障,則可根據下面的分析思路進行檢查。

  • 細看所有步驟執行命令返回的屏幕輸出,不要忽略關鍵的輸出內容。
  • 查看mysql錯誤日誌/application/mysql/data/機器名.err。
  • 輔助查看系統日誌/var/log/messages。
  • 如果是MySQL關聯了其他服務,則還要同時查看相關服務的日誌。
  • 仔細閱讀,重新查看所有操作的步驟是否正確,書寫的命令及字符是否都正確。

重置root用戶密碼

MySQL 5.6管理員的賬號root密碼默認爲空,極不安全,可以通過mysqladmin命令爲mysql不同實例的數據庫設置獨立的密碼。

# /application/mysql/bin/mysqladmin -u root password 'root'
Warning: Using a password on the command line interface can be insecure.

終端登錄

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.44 Source distribution

Copyright (c) 2000, 2019, 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_secure_installation命令交互式地設置系統的用戶密碼

多實例配置

多實例配置完成後,MySQL雙實例的目錄信息及文件註釋說明:

# tree /data
/data
|-- 3306
|   |-- data   #<==3306實例的數據文件。
|   |-- my.cnf #<==3306實例的配置文件。
|   `-- mysql  #<==3306實例的啓動文件。
`-- 3307
    |-- data   #<==3307實例的數據文件。
    |-- my.cnf #<==3307實例的配置文件。
    `-- mysql  #<==3307實例的啓動文件。
4 directories, 4 files

提示:這裏的配置文件my.cnf、啓動程序mysql都是獨立的文件,數據文件data目錄也是獨立的。

創建MySQL多實例的數據文件目錄

以"/data"目錄作爲MySQL多實例總的根目錄,然後規劃不同的數字(即MySQL實例端口號)作爲“/data”下面的二級目錄;不同的二級目錄對應的數字就作爲MySQL實例的端口號,以區別不同的實例;數字對應的二級目錄下包含MySQL的數據文件、配置文件以及啓動文件等。

下面以配置3306、3307兩個實例爲例進行講解。創建MySQL多實例的目錄如下:

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

創建MySQL多實例的配置文件

如果配置多實例,則其與單實例會有所不同。爲了讓MySQL多實例之間彼此獨立,需要爲每一個實例建立一個my.cnf配置文件和一個啓動文件mysql,讓它們分別對應自己的數據文件目錄data。

# touch /data/3306/my.cnf
# touch /data/3307/my.cnf

不同的實例需要添加的my.cnf內容也會有區別。

MySQL 3306實例

# cat /data/3306/my.cnf 
[client]								#<==客戶端模塊
port		=3306						#<==客戶端端口
socket		=/data/3306/mysql.sock

[mysqld]								#<==服務端模塊
user		=mysql						#<==用戶
port		=3306						#<==端口
socket		=/data/3306/mysql.sock		#<==socket路徑
basedir		=/application/mysql			#<==安裝路徑
datadir		=/data/3306/data			#<==數據文件
log-bin		=/data/3306/mysql-bin		#<==二進制日誌
server-id	=6

[mysqld_safe]							#<==啓動服務模塊
log-error	=/data/3306/www_3306.err	#<==錯誤日誌
pid-file	=/data/3306/mysqld.pid		#<==進程號文件

MySQL 3307實例

# cat /data/3307/my.cnf 
[client]								#<==客戶端模塊
port		=3307						#<==客戶端端口
socket		=/data/3307/mysql.sock

[mysqld]								#<==服務端模塊
user		=mysql						#<==用戶
port		=3307						#<==端口
socket		=/data/3307/mysql.sock		#<==socket路徑
basedir		=/application/mysql			#<==安裝路徑
datadir		=/data/3307/data			#<==數據文件
log-bin		=/data/3307/mysql-bin		#<==二進制日誌
server-id	=7

[mysqld_safe]							#<==啓動服務模塊
log-error	=/data/3307/www_3307.err	#<==錯誤日誌
pid-file	=/data/3307/mysqld.pid		#<==進程號文件

創建MySQL多實例的啓動文件

# touch /data/3306/mysql
# touch /data/3307/mysql

MySQL 3306實例啓動文件

# cat /data/3306/mysql
#! /bin/bash
port=3306
mysql_user="root"
CmdPath="/application/mysql/bin"
mysql_mycnf="/data/${port}/my.cnf"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path="/data/${port}/mysqld.pid"
start(){
	if [ ! -e "$mysql_sock" ];then
		printf "Starting MySQL...\n"
		/bin/sh ${CmdPath}/mysqld_safe --defaults-file=${mysql_mycnf} --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 "Stopping MySQL...\n"
		mysqld_pid=`cat ${mysqld_pid_file_path}` #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

MySQL 3307實例啓動文件

# cat /data/3307/mysql
#! /bin/bash
port=3307
mysql_user="root"
CmdPath="/application/mysql/bin"
mysql_mycnf="/data/${port}/my.cnf"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path="/data/${port}/mysqld.pid"
start(){
	if [ ! -e "$mysql_sock" ];then
		printf "Starting MySQL...\n"
		/bin/sh ${CmdPath}/mysqld_safe --defaults-file=${mysql_mycnf} --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 "Stopping 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

需要特別說明一下的是,在多實例啓動文件中,啓動MySQL不同實例服務所執行的命令實質上是有區別的,例如,
啓動3306實例的命令如下:
mysqld_safe --defaults-file=/data/3306/my.cnf > /dev/null 2>&1 &
啓動3307實例的命令如下:
mysqld_safe --defaults-file=/data/3307/my.cnf > /dev/null 2>&1 &

mysqladmin命令關閉方法,這個命令的缺點是必須要有數據庫的root用戶密碼才能運行。
停止3306實例的命令如下:
mysqladmin -u root -p密碼 -S /data/3306/mysql.sock shutdown
停止3307實例的命令如下:
mysqladmin -u root -p密碼 -S /data/3307/mysql.sock shutdown

配置MySQL多實例的文件權限

# touch /data/3306/www_3306.err
# touch /data/3307/www_3307.err
# chown -R mysql:mysql /data
# chmod +x /data/{3306,3307}/mysql

初始化MySQL多實例的數據庫

初始化數據庫的實質就是創建基礎的數據庫系統的庫文件,例如,生成MySQL庫表等。
初始化3306實例數據庫:

# cd /application/mysql/scripts
# ./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
WARNING: The host 'www' could not be looked up with /application/mysql/bin/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...2019-07-21 16:47:16 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-21 16:47:16 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-07-21 16:47:16 0 [Note] /application/mysql/bin/mysqld (mysqld 5.6.44-log) starting as process 1729 ...
2019-07-21 16:47:16 1729 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-21 16:47:16 1729 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-21 16:47:16 1729 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-21 16:47:16 1729 [Note] InnoDB: Memory barrier is not used
2019-07-21 16:47:16 1729 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-07-21 16:47:16 1729 [Note] InnoDB: Using Linux native AIO
2019-07-21 16:47:16 1729 [Note] InnoDB: Using CPU crc32 instructions
2019-07-21 16:47:16 1729 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-21 16:47:16 1729 [Note] InnoDB: Completed initialization of buffer pool
2019-07-21 16:47:16 1729 [Note] InnoDB: Highest supported file format is Barracuda.
2019-07-21 16:47:16 1729 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-21 16:47:16 1729 [Note] InnoDB: Waiting for purge to start
2019-07-21 16:47:16 1729 [Note] InnoDB: 5.6.44 started; log sequence number 1600627
2019-07-21 16:47:17 1729 [Note] Binlog end
2019-07-21 16:47:17 1729 [Note] InnoDB: FTS optimize thread exiting.
2019-07-21 16:47:17 1729 [Note] InnoDB: Starting shutdown...
2019-07-21 16:47:18 1729 [Note] InnoDB: Shutdown completed; log sequence number 1626007
OK

Filling help tables...2019-07-21 16:47:18 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-21 16:47:18 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-07-21 16:47:18 0 [Note] /application/mysql/bin/mysqld (mysqld 5.6.44-log) starting as process 1753 ...
2019-07-21 16:47:18 1753 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-21 16:47:18 1753 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-21 16:47:18 1753 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-21 16:47:18 1753 [Note] InnoDB: Memory barrier is not used
2019-07-21 16:47:18 1753 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-07-21 16:47:18 1753 [Note] InnoDB: Using Linux native AIO
2019-07-21 16:47:18 1753 [Note] InnoDB: Using CPU crc32 instructions
2019-07-21 16:47:18 1753 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-21 16:47:18 1753 [Note] InnoDB: Completed initialization of buffer pool
2019-07-21 16:47:18 1753 [Note] InnoDB: Highest supported file format is Barracuda.
2019-07-21 16:47:18 1753 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-21 16:47:18 1753 [Note] InnoDB: Waiting for purge to start
2019-07-21 16:47:18 1753 [Note] InnoDB: 5.6.44 started; log sequence number 1626007
2019-07-21 16:47:19 1753 [Note] Binlog end
2019-07-21 16:47:19 1753 [Note] InnoDB: FTS optimize thread exiting.
2019-07-21 16:47:19 1753 [Note] InnoDB: Starting shutdown...
2019-07-21 16:47:21 1753 [Note] InnoDB: Shutdown completed; log sequence number 1626017
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:

  /application/mysql/bin/mysqladmin -u root password 'new-password'
  /application/mysql/bin/mysqladmin -u root -h www password 'new-password'

Alternatively you can run:

  /application/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 . ; /application/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file /application/mysql/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /application/mysql/my-new.cnf,
please compare it with your file and take the changes you need.

如上,出現兩次OK,則成功。

啓動MySQL多實例數據庫

啓動MySQL多實例數據庫

# /data/3306/mysql start
# /data/3307/mysql start

同理:關閉MySQL多實例數據庫

# /data/3306/mysql stop
# /data/3307/mysql stop

同理:重啓MySQL多實例數據庫

# /data/3306/mysql restart
# /data/3307/mysql restart

重置root用戶密碼

MySQL 5.6管理員的賬號root密碼默認爲空,極不安全,可以通過mysqladmin命令爲mysql不同實例的數據庫設置獨立的密碼。比如將兩個實例的root用戶的密碼都設置爲root。
/application/mysql/bin/目錄下的mysqladmin命令。

# mysqladmin -uroot password 'root' -S /data/3306/mysql.sock
# mysqladmin -uroot password 'root' -S /data/3307/mysql.sock

終端登錄不同的實例

登錄不同的實例需要指定不同實例的sock路徑及mysql.sock文件,這個mysql.sock是在my.cnf配置文件裏指定的。
登錄3306實例

# mysql -uroot -p -S /data/3306/mysql.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.44-log Source distribution

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

登錄3307實例

# mysql -uroot -p -S /data/3307/mysql.sock 
Enter password:

開機啓動MySQL多實例數據庫

  在 /etc/rc.loadl 文件中加入以下內容:

#mysql multi instances
/data/3306/mysql start
/data/3307/mysql start

遠程登錄

mysql> use mysql;  
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
mysql> flush privileges; 

//授權語句說明
grant all on xxxx.* to 'root'@'%' identified by 'password' with grant option;
xxxx代表創建的數據庫;
%爲主機名/主機ip地址
password爲用戶密碼,在此爲root的密碼

關閉防火牆

查看防火牆狀態
firewall-cmd --state
(關閉後顯示not running,開啓後顯示running)

# firewall-cmd --state 

或者systemctl status firewalld.service
(關閉後顯示Active: inactive (dead),開啓後顯示Active: active (running))

# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-07-22 16:38:26 CST; 17s ago
 Main PID: 9242 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─9242 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jul 22 16:38:25 sy-pc systemd[1]: Starting firewalld - dynamic firewall daemon...
Jul 22 16:38:26 sy-pc systemd[1]: Started firewalld - dynamic firewall daemon.

添加防火牆

# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload

關閉防火牆

臨時關閉防火牆

# systemctl stop firewalld.service

禁止開啓啓動

# systemctl disable firewalld.service

開啓防火牆

# systemctl start firewalld.service

關閉防火牆的步驟爲:

# systemctl stop firewalld.service
# systemctl disable firewalld.service

參考文獻

[1] 老男孩. 跟老男孩學Linux運維:MySQL入門與提高實踐[M]. 機械工業出版社,2019.
[2] CentOS 7下源碼安裝MySQL 5.6[DB|OL].https://www.linuxidc.com/Linux/2015-06/119354.htm
[3] 設置Mysql5.6允許外網訪問詳細流程[DB|OL]. https://blog.csdn.net/cxin917/article/details/76686227
[4] mysql開啓遠程訪問權限[DB|OL].https://www.cnblogs.com/hfdp/p/6088288.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章