1. 程式人生 > >Mysql——1、編譯安裝MySQL

Mysql——1、編譯安裝MySQL

環境準備:

yum groupinstall "Development Tools" -y  #安裝開發環境程式包  
yum install ncurses-devel openssl-devel libevent-devel jemalloc-devel -y

一、安裝cmake

跨平臺編譯器
cmake的重要特性之一是其獨立於原始碼(out-of-source)的編譯功能,即編譯工作可以在另一個指定的目錄中而非原始碼目錄中進行,這可以保證原始碼目錄不受任何一次編譯的影響,因此在同一個原始碼樹上可以進行多次不同的編譯,如針對於不同平臺編譯。

# tar xf cmake-2.8.8.tar.gz
# cd cmake-2.8.8
# ./bootstrap
# make 
# make install

二、編譯安裝mysql-5.5.33

1、使用cmake編譯mysql-5.5
cmake指定編譯選項的方式不同於make,其實現方式對比如下:

./configure           cmake .
./configure --help    cmake . -LH or ccmake .

指定安裝檔案的安裝路徑時常用的選項:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/data/mysql
-DSYSCONFDIR=/etc

預設編譯的儲存引擎包括:csv、myisam、myisammrg和heap。若要安裝其它儲存引擎,可以使用類似如下編譯選項:

-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1

若要明確指定不編譯某儲存引擎,可以使用類似如下的選項:

-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1

比如:

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1

如若要編譯進其它功能,如SSL等,則可使用類似如下選項來實現編譯時使用某庫或不使用某庫:

-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_ZLIB=system
-DWITH_LIBWRAP=0

其它常用的選項:

-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DENABLED_LOCAL_INFILE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_DEBUG=0
-DENABLE_PROFILING=1

如果想清理此前的編譯所生成的檔案,則需要使用如下命令:

make clean
rm CMakeCache.txt

2、編譯安裝

# groupadd -r mysql
# useradd -g mysql -r -d /mydata/data mysql
# tar xf mysql-5.5.33.tar.gz 
# cd mysql-5.5.33
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
      -DMYSQL_DATADIR=/mydata/data \
      -DSYSCONFDIR=/etc \
	  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
      -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
      -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
	  -DWITH_READLINE=1 \
	  -DWITH_SSL=system \
	  -DWITH_ZLIB=system \
	  -DWITH_LIBWRAP=0 \
	  -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
	  -DDEFAULT_CHARSET=utf8 \
      -DDEFAULT_COLLATION=utf8_general_ci
# make 
# make install


# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

三、在CentOS 7通過rpm包安裝mysql 5.7

wget http://repo.mysql.com/mysql57-community-release-el7.rpm
rpm -ivh mysql57-community-release-el7.rpm
yum install -y mysql-server
systemctl enable mysqld

更改/etc/my.cnf等mysql配置

# vim /etc/my.cnf
port=13506
datadir=/data/mysql/   #資料儲存到/data/mysql/

啟動服務

systemctl start mysqld

#獲取臨時登入密碼

[[email protected] ~]# grep 'temporary password' /var/log/mysqld.log
2018-07-09T08:43:08.265032Z 1 [Note] A temporary password is generated for [email protected]: r=fLuQ!o=9*d
[[email protected] ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22
 
Copyright (c) 2000, 2018, 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> set global validate_password_policy=0;
 
mysql> set global validate_password_length=1;

#更改密碼

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Abcd.1234'; 
 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Abcd.1234' WITH GRANT OPTION;
 
mysql> FLUSH PRIVILEGES;