第一種方式比較簡單,直接RPM包安裝就OK了,或者通過yum源來安裝,但無法自定義安裝目錄。
而第三種方式比較復雜。
一般項目中使用比較多的是通過二進制包來安裝,這種安裝方式可以根據企業的不同需求進行相關的自定義配置,比如安裝在哪個目錄,如何配置等。
MySQL5.7與5.6安裝有所改變。
CentOS版本 [mysql@new ~]$ cat /etc/redhat-release CentOS release 6.3 (Final) [mysql@new ~]$
把安裝文件上傳至服務器 [mysql@new ~]$ ls total 535920 -rw-rw-rw-. 1 mysql mysql 548774698 Jan 12 16:53 mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz 解壓安裝文件
[mysql@new ~]$ tar -xvf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
重命名目錄 [mysql@guoqing ~]$ mv mysql-5.7.10-linux-glibc2.5-x86_64 mysql [mysql@guoqing ~]$ ls mysql mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz [mysql@guoqing ~]$ cd mysql [mysql@guoqing mysql]$ ls bin COPYING docs include INSTALL-BINARY lib man README share support-files
新建存放數據的目錄data [mysql@guoqing mysql]$ mkdir data [mysql@guoqing mysql]$ ll total 164 drwxr-xr-x. 2 mysql mysql 4096 Nov 30 03:54 bin -rw-r--r--. 1 mysql mysql 17987 Nov 30 03:16 COPYING drwxrwxr-x. 2 mysql mysql 4096 Jan 12 23:38 data drwxr-xr-x. 2 mysql mysql 4096 Nov 30 03:54 docs drwxr-xr-x. 3 mysql mysql 4096 Nov 30 03:54 include -rw-r--r--. 1 mysql mysql 108090 Nov 30 03:26 INSTALL-BINARY drwxr-xr-x. 5 mysql mysql 4096 Nov 30 03:54 lib drwxr-xr-x. 4 mysql mysql 4096 Nov 30 03:54 man -rw-r--r--. 1 mysql mysql 2478 Nov 30 03:16 README drwxr-xr-x. 28 mysql mysql 4096 Nov 30 03:54 share drwxr-xr-x. 2 mysql mysql 4096 Nov 30 03:54 support-files
在MySQL服務文件中把/usr/local/指替換成自己安裝MySQL目錄
這里是替換成的home/mysql [root@guoqing mysql]# vi /etc/init.d/mysql
# 0 means don't wait at all # Negative numbers mean to wait indefinitely service_startup_timeout=900 # Lock directory for RedHat / SuSE. lockdir='/var/lock/subsys' lock_file_path="$lockdir/mysql" # The following variables are only set for letting mysql.server find things. # Set some defaults mysqld_pid_file_path= if test -z "$basedir" then basedir=/home/mysql/mysql bindir=/home/mysql/mysql/bin if test -z "$datadir" then datadir=/home/mysql/mysql/data fi sbindir=/home/mysql/mysql/bin libexecdir=/home/mysql/mysql/bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi # datadir_set is used to determine if datadir was set (and so should be # *not* set inside of the --basedir= handler.) datadir_set= # # Use LSB init script functions for printing messages, if possible # lsb_functions="/lib/lsb/init-functions" if test -f $lsb_functions ; then . $lsb_functions else "/etc/rc.d/init.d/mysql" 393L, 10981C written [root@guoqing mysql]# exit exit
修改my.cnf文件,主要添加basedir、datadir [mysql@guoqing mysql]$ vi /etc/my.cnf [mysqld] basedir=/home/mysql/mysql datadir=/home/mysql/mysql/data socket=/home/mysql/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/home/mysql/mysql/data/mysqld.log pid-file=/home/mysql/mysql/data/mysqld/mysqld.pid "/etc/my.cnf" 11L, 316C written
初始化MySQL
MySQL 5.7.6之后版本為 ./bin/mysqld --initialize --user=mysql --basedir=/home/mysql/mysql --datadir=/home/mysql/mysql/data/ [mysql@guoqing mysql]$ ./bin/mysqld --initialize --user=mysql --basedir=/home/mysql/mysql --datadir=/home/mysql/mysql/data/ 2016-01-12T16:16:14.765586Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2016-01-12T16:16:14.765847Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000) 2016-01-12T16:16:14.766086Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-01-12T16:16:15.038567Z 0 [Warning] InnoDB: New log files created, LSN=45790 2016-01-12T16:16:15.095995Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-01-12T16:16:15.166081Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cdd34871-b947-11e5-8a45-0800272a2c8e. 2016-01-12T16:16:15.168260Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-01-12T16:16:15.168776Z 1 [Note] A temporary password is generated for root@localhost: qnW8usF=o;=e(root用戶密碼) MySQL 5.7.6之前的版本為bin/mysql_install_db --user=mysql
創建 SSL/RSA 文件
參見:http://dev.mysql.com/doc/refman/5.7/en/mysql-ssl-rsa-setup.html [mysql@guoqing mysql]$ bin/mysql_ssl_rsa_setup --datadir=/home/mysql/mysql/data/ Generating a 2048 bit RSA private key ...............................+++ .............................................................................................................................................+++ writing new private key to 'ca-key.pem' ----- Generating a 2048 bit RSA private key ............................................+++ ...................................+++ writing new private key to 'server-key.pem' ----- Generating a 2048 bit RSA private key ...........................................................................................................................................+++ ......+++ writing new private key to 'client-key.pem' -----
添加環境變量:/home/mysql/mysql/bin [mysql@guoqing ~]$ vi .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/home/mysql/mysql/bin export PATH ~ ".bash_profile" 12L, 198C written
使變量生效
[mysql@guoqing ~]$ . .bash_profile
啟動MySQL服務
[mysql@new ~]$ service mysql start
Starting MySQL. [ OK ]
登錄
用到上面日志中的密碼,注意qnW8usF=o;=e全是密碼內容 [mysql@guoqing ~]$ mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.10 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> set password=password('root'); Query OK, 0 rows affected, 1 warning (0.00 sec)