閱讀812 返回首頁    go 阿裏雲 go 技術社區[雲棲]


高可用架構篇 MySQL源碼編譯安裝(CentOS-6.6+MySQL-5.6)

部署環境

操作係統:CentOS-6.6-x86_64-bin-DVD1.iso

MySQL版本:mysql-5.6.26.tar.gz                                                                    

操作用戶:root

係統IP192.168.1.205

主機名:edu-mysql-01

配置:4核、4G內存      

                                                                                             

 一、服務器配置:


1、配置網絡

vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

NM_CONTROLLED=no

ONBOOT=yes

TYPE=Ethernet

HWADDR=00:50:56:a1:12:53

IPADDR=192.168.1.205

NETMASK=255.255.255.0

GATEWAY=192.168.1.1

DNS1=223.5.5.5

DNS2=223.6.6.6

 

2、設置主機名

vi /etc/sysconfig/network

NETWORKING=yes

HOSTNAME=edu-mysql-01

 

3、設置IP與主機名的映射

vi /etc/hosts

127.0.0.1 edu-mysql-01

192.168.1.205 edu-mysql-01

 

4、兩台數據庫服務器的的selinux都要disable

(永久關閉selinux,請修改/etc/selinux/config,將SELINUX改為disabled

vi /etc/selinux/config

SELINUX=disabled

 

5、重啟操作係統

reboot

 

二、源碼安裝MySQL5.6.26:


1、使用下麵的命令檢查是否安裝有MySQL Server:

rpm -qa | grep mysql

mysql-libs-5.1.73-3.el6_5.x86_64

如果是CentOS7以上,請使用以下命令查看:

rpm -qa | grep mariadb

mariadb-libs-5.5.41-2.el7_0.x86_64

(因為沒有MySQL服務,因此沒必要卸載。mysql-libsMySQL的必要包

(如果有的話可通過下麵的命令來卸載掉,rpm -e mysql //普通刪除模式

 

2改防火牆設置,打開3306端口:

# vi /etc/sysconfig/iptables

增加如下行:

## MySQL

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

 

重啟防火牆:

service iptables restart

 

3新增mysql用戶組:

groupadd mysql

 

4新增mysql用戶,並添加到mysql用戶組:

# useradd -r -g mysql mysql

 

5新建MySQL執行文件目錄(後麵會把編譯好的mysql程序安裝到這個目錄):

mkdir -p /usr/local/mysql

(-p 參數的作用是:如果最終目錄的父目錄不存在也會一並創建)

 

6新建MySQL數據庫數據文件目錄:

mkdir -p /home/mysql/data

mkdir -p /home/mysql/logs

mkdir -p /home/mysql/temp

注意:上麵的logstemp目錄是為了以後將MySQL的數據文件與執行程序文件分離,如果你打算設置到不同的路徑,注意修改對應的執行命令和數據庫初始化腳本。正式生產環境,建議數據目錄和日誌目錄都使用單獨的分區來掛載,不同分區屬於不同的磁盤或磁盤組。

 

7增加PATH環境變量搜索路徑:

# vi /etc/profile

##在profile文件末尾增加兩行

# mysql env param

PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

export PATH

 

使PATH搜索路徑立即生效:

source /etc/profile

 

8、安裝編譯MySQL需要的依賴包:

mysql從5.5版本開始,不再使用./configure編譯,而是使用cmake編譯器,具體的cmake編譯參數可以參考mysql官網文檔

https://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html,安裝基本依賴包,先用yum安裝cmake、automake 、autoconf ,另MySQL 5.5.x需要最少安裝的包有:bison,gcc、gcc-c++、ncurses-devel):

 

# yum install make cmake gcc gcc-c++ bison bison-devel ncurses ncurses-devel autoconf automake

 

9、進入/usr/local/src目錄,上傳mysql-5.6.26.tar.gz源代碼到/usr/local/src目錄:

cd /usr/local/src

 

10開始編譯安裝mysql-5.6.26:

解壓縮源碼包:

# tar -zxvf mysql-5.6.26.tar.gz

進入解壓縮源碼目錄:

cd mysql-5.6.26

使用cmake源碼安裝mysql(如果你打算安裝到不同的路徑,注意修改下麵語句中/usr/local/mysql和/home/mysql/data路徑!)

 

[root@edu-mysql-01 mysql-5.6.26]# cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_MEMORY_STORAGE_ENGINE=1 \

-DWITH_READLINE=1 \

-DENABLED_LOCAL_INFILE=1 \

-DMYSQL_DATADIR=/home/mysql/data \

-DMYSQL_USER=mysql \

-DMYSQL_TCP_PORT=3306 \

-DENABLE_DOWNLOADS=1

 

上麵的這些複製完,回車,然後就開始cmake的過程,一般時間不會很長。

配置解釋:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 設置安裝目錄

-DMYSQL_DATADIR=/home/mysql/data 設置數據庫存放目錄

-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 設置UNIX socket 目錄

-DMYSQL_USER=mysql 設置運行用戶

-DDEFAULT_CHARSET=utf8 設置默認字符集,默認latin1

-DEFAULT_COLLATION=utf8_general_ci 設置默認校對規則,默認latin1_general_ci

-DWITH_INNOBASE_STORAGE_ENGINE=1 添加InnoDB引擎支持

-DENABLE_DOWNLOADS=1 自動下載可選文件,比如自動下載穀歌的測試包

-DMYSQL_TCP_PORT=3306 設置服務器監聽端口,默認3306

-DSYSCONFDIR=/etc 設置my.cnf所在目錄,默認為安裝目錄)

 

執行過程中會出現:

CMake Error: Problem with tar_extract_all(): Invalid argument

CMake Error: Problem extracting tar: /usr/local/src/mysql-5.6.26/source_downloads/gmock-1.6.0.zip

解決方法:

cd mysql目錄下麵會發現有一個source_downloads目錄,需要解壓unzip gmock-1.6.0.zip,然後再重新執行上述配置過程。當然你也可以去掉-DENABLE_DOWNLOADS=1這個選項,不編譯穀歌的測試包也沒有什麼問題,但是之前的某些版本會出現無法編譯的問題.

 

11cmake結束後開始編譯源碼,這一步時間會較長,請耐心等待:

# make

 

12安裝編譯好的程序:

# make install

注意:如果需要重裝mysql,在/usr/local/src/mysql-5.6.26在執行下make install就可以了,不需要再cmakemake

 

13清除安裝臨時文件

make clean

 

14修改mysql目錄擁有者為mysql用戶:

chown -Rf mysql:mysql /usr/local/mysql

chown -Rf mysql:mysql /home/mysql

 

15進入mysql執行程序的安裝路徑:

cd /usr/local/mysql

 

16執行初始化配置腳本,創建係統自帶的數據庫和表(注意:路徑/home/mysql/data需要換成你自定定義的數據庫存放路徑):

scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data

Installing MySQL system tables...2015-12-13 15:21:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2015-12-13 15:21:53 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.26) starting as process 17362 ...

2015-12-13 15:21:53 17362 [Note] InnoDB: Using atomics to ref count buffer pool pages

2015-12-13 15:21:53 17362 [Note] InnoDB: The InnoDB memory heap is disabled

2015-12-13 15:21:53 17362 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2015-12-13 15:21:53 17362 [Note] InnoDB: Memory barrier is not used

2015-12-13 15:21:53 17362 [Note] InnoDB: Compressed tables use zlib 1.2.3

2015-12-13 15:21:53 17362 [Note] InnoDB: Using CPU crc32 instructions

2015-12-13 15:21:53 17362 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2015-12-13 15:21:53 17362 [Note] InnoDB: Completed initialization of buffer pool

2015-12-13 15:21:53 17362 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!

2015-12-13 15:21:53 17362 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB

2015-12-13 15:21:53 17362 [Note] InnoDB: Database physically writes the file full: wait...

2015-12-13 15:21:53 17362 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB

2015-12-13 15:21:53 17362 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB

2015-12-13 15:21:53 17362 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0

2015-12-13 15:21:53 17362 [Warning] InnoDB: New log files created, LSN=45781

2015-12-13 15:21:53 17362 [Note] InnoDB: Doublewrite buffer not found: creating new

2015-12-13 15:21:53 17362 [Note] InnoDB: Doublewrite buffer created

2015-12-13 15:21:53 17362 [Note] InnoDB: 128 rollback segment(s) are active.

2015-12-13 15:21:53 17362 [Warning] InnoDB: Creating foreign key constraint system tables.

2015-12-13 15:21:53 17362 [Note] InnoDB: Foreign key constraint system tables created

2015-12-13 15:21:53 17362 [Note] InnoDB: Creating tablespace and datafile system tables.

2015-12-13 15:21:53 17362 [Note] InnoDB: Tablespace and datafile system tables created.

2015-12-13 15:21:53 17362 [Note] InnoDB: Waiting for purge to start

2015-12-13 15:21:53 17362 [Note] InnoDB: 5.6.26 started; log sequence number 0

2015-12-13 15:21:53 17362 [Note] Binlog end

2015-12-13 15:21:53 17362 [Note] InnoDB: FTS optimize thread exiting.

2015-12-13 15:21:53 17362 [Note] InnoDB: Starting shutdown...

2015-12-13 15:21:54 17362 [Note] InnoDB: Shutdown completed; log sequence number 1625977

OK

 

Filling help tables...2015-12-13 15:21:54 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2015-12-13 15:21:54 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.26) starting as process 17384 ...

2015-12-13 15:21:54 17384 [Note] InnoDB: Using atomics to ref count buffer pool pages

2015-12-13 15:21:54 17384 [Note] InnoDB: The InnoDB memory heap is disabled

2015-12-13 15:21:54 17384 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2015-12-13 15:21:54 17384 [Note] InnoDB: Memory barrier is not used

2015-12-13 15:21:54 17384 [Note] InnoDB: Compressed tables use zlib 1.2.3

2015-12-13 15:21:54 17384 [Note] InnoDB: Using CPU crc32 instructions

2015-12-13 15:21:54 17384 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2015-12-13 15:21:54 17384 [Note] InnoDB: Completed initialization of buffer pool

2015-12-13 15:21:54 17384 [Note] InnoDB: Highest supported file format is Barracuda.

2015-12-13 15:21:54 17384 [Note] InnoDB: 128 rollback segment(s) are active.

2015-12-13 15:21:54 17384 [Note] InnoDB: Waiting for purge to start

2015-12-13 15:21:54 17384 [Note] InnoDB: 5.6.26 started; log sequence number 1625977

2015-12-13 15:21:55 17384 [Note] Binlog end

2015-12-13 15:21:55 17384 [Note] InnoDB: FTS optimize thread exiting.

2015-12-13 15:21:55 17384 [Note] InnoDB: Starting shutdown...

2015-12-13 15:21:56 17384 [Note] InnoDB: Shutdown completed; log sequence number 1625987

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:

 

  /usr/local/mysql/bin/mysqladmin -u root password 'new-password'

  /usr/local/mysql/bin/mysqladmin -u root -h edu-mysql-02 password 'new-password'

 

Alternatively you can run:

 

  /usr/local/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 . ; /usr/local/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 https://bugs.mysql.com/

 

The latest information about MySQL is available on the web at

 

  https://www.mysql.com

 

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

 

New default config file was created as /usr/local/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

 

WARNING: Default config file /etc/my.cnf exists on the system

This file will be read by default by the MySQL server

If you do not want to use this, either remove it, or use the

--defaults-file argument to mysqld_safe when starting the server

 

17初始化腳本在/usr/local/mysql/下生成了配置文件my.cnf,需要更改該配置文件的所有者:

ls -lah

                                              face/yyszpFX6sYNrHEWC53isXixanWdfNnSa.png

[root@edu-mysql-01 mysql] # chown -Rf mysql:mysql /usr/local/mysql/my.cnf

 

18、注意:

1Tips:在啟動MySQL服務時,會按照一定次序搜索my.cnf,先在/etc目錄下找,找不到則會搜索mysql程序目錄下是否有my.cnf
2)需要注意CentOS 6版操作係統的最小安裝完成後,即使沒有安裝mysql,在/etc目錄下也會存在一個my.cnf文件,建議將此文件更名為其他的名字,否則該文件會幹擾源碼安裝的MySQL的正確配置,造成無法啟動。修改/etc/my.cnf操作如下:

可以:mv /etc/my.cnf /etc/my.cnf.bak

也可以:刪除掉/etc/my.cnf這個文件:rm /etc/my.cnf

 

如果你需要用於生產環境,不要急著做下麵的mysql啟動操作。建議把上一步驟中mysql初始化生成的/usr/local/mysql/my.cnf刪除,然後把你優化好的mysql配置文件my.cnf放到/etc下。(這是做mysql主從複製和mysql優化的經驗!)

 

(我們這裏使用/etc/my.cnf

 

19、編輯/etc/my.cnf:

vi /etc/my.cnf

[client]

port = 3306

socket = /usr/local/mysql/mysql.sock

 

[mysqld]

character-set-server = utf8

collation-server = utf8_general_ci

 

skip-external-locking

skip-name-resolve

 

user = mysql

port = 3306

basedir = /usr/local/mysql

datadir = /home/mysql/data

tmpdir = /home/mysql/temp

# server_id = .....

socket = /usr/local/mysql/mysql.sock

log-error = /home/mysql/logs/mysql_error.log

pid-file = /home/mysql/mysql.pid

 

open_files_limit = 10240

 

back_log = 600

max_connections=500

max_connect_errors = 6000

wait_timeout=605800

 

#open_tables = 600

#table_cache = 650

#opened_tables = 630

 

max_allowed_packet = 32M

 

sort_buffer_size = 4M

join_buffer_size = 4M

thread_cache_size = 300

query_cache_type = 1

query_cache_size = 256M

query_cache_limit = 2M

query_cache_min_res_unit = 16k

 

tmp_table_size = 256M

max_heap_table_size = 256M

 

key_buffer_size = 256M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

 

lower_case_table_names=1

 

default-storage-engine = INNODB

 

innodb_buffer_pool_size = 2G

innodb_log_buffer_size = 32M

innodb_log_file_size = 128M

innodb_flush_method = O_DIRECT

 

#####################

thread_concurrency = 32

long_query_time= 2

slow-query-log = on

slow-query-log-file = /home/mysql/logs/mysql-slow.log 

 

[mysqldump]

quick

max_allowed_packet = 32M

 

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

 

20複製服務啟動腳本:

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

 

21啟動MySQL服務:

service mysql start

Starting MySQL.. SUCCESS!

(初次啟動會在/usr/local/mysql目錄下生成mysql.sock文件)

 

22設置MySQL開機自動啟動服務:

chkconfig mysql on

 

設置MySQL數據庫root用戶的本地登錄密碼(初始用戶沒有密碼)

mysqladmin -u root password 'roncoo'

 

23登錄並修改MySQL用戶root的密碼:

mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.6.26-log Source distribution

 

Copyright (c) 2000, 2015, 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> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)

 

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

修改root用戶密碼:

mysql> update user set Password = password('roncoo.com') where User='root';

Query OK, 4 rows affected (0.00 sec)

Rows matched: 5  Changed: 4  Warnings: 0

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

允許root遠程登錄,設置遠程登錄密碼:www.roncoo.com

mysql> use mysql;

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'www.roncoo.com' WITH GRANT OPTION;

mysql> flush privileges;

mysql> exit;

 

注意:真實生產環境,應用操作不要使用root用戶。

 

重新登錄

[root@edu-mysql-01 ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 5.6.26-log Source distribution

 

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

 

24運行安全設置腳本,強烈建議生產服務器使用(可選):

[root@edu-mysql-01 ~]# /usr/local/mysql/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

In order to log into MySQL to secure it, we'll need the current

password for the root user.  If you've just installed MySQL, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

 

Enter current password for root (enter for none):  ----->此處輸入root密碼

OK, successfully used password, moving on...

 

Setting the root password ensures that nobody can log into the MySQL

root user without the proper authorisation.

 

You already have a root password set, so you can safely answer 'n'.

 

Change the root password? [Y/n] n  -----> 上已為root設置了密碼,此處可輸n

 ... skipping.

 

By default, a MySQL installation has an anonymous user, allowing anyone

to log into MySQL without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

 

Remove anonymous users? [Y/n] Y  ------> 刪除匿名用戶

 ... Success!

 

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

 

Disallow root login remotely? [Y/n] n  -----> 一般不允許root遠程登錄,可添加普通用戶,然後設置允許遠程登錄

 ... skipping.

 

By default, MySQL comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

 

Remove test database and access to it? [Y/n] Y  -----> 刪除test庫及相應權限

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

 

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

 

Reload privilege tables now? [Y/n] Y -----> 重新加載權限表使設置生效

 ... Success!

 

All done!  If you've completed all of the above steps, your MySQL

installation should now be secure.

Thanks for using MySQL!

Cleaning up...

 

25重啟服務器,檢測mysql是否能開機自動啟動:

[root@edu-mysql-01 ~] # reboot


參考內容:基於Dubbo的分布式係統架構項目實戰

最後更新:2017-10-20 12:33:27

  上一篇:go  如何登陸阿裏雲服務器,阿裏雲服務器怎麼登陸
  下一篇:go  AI新技術:利用神經網絡對圖片進行超級壓縮