1. 程式人生 > >mysql資料庫安裝

mysql資料庫安裝

1、通過rpm包安裝

         Centos系統中預設倉庫中攜帶了mysql資料庫,centos6 yum倉庫中mysql版本為5.1,centos7中為mariadb5.5(mysql的分支),如果想安裝指定版本的mysql,可以去mysql官網下載。

(1)安裝前準備工作

# 安裝依賴軟體包
]# yum install net-tools perl
# 解除安裝centos中與mariadb相關的包
]# yum remove mariadb*
# 下載mysql及客戶端相關的 rpm包
]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.23-1.el7.x86_64.rpm
]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.23-1.el7.x86_64.rpm
]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.23-1.el7.x86_64.rpm
]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.23-1.el7.x86_64.rpm

(2)安裝mysql

# 安裝mysql
]# rpm -ivh mysql-community-client-5.7.23-1.el7.x86_64.rpm mysql-community-server-5.7.23-1.el7.x86_64.rpm mysql-community-libs-5.7.23-1.el7.x86_64.rpm mysql-community-common-5.7.23-1.el7.x86_64.rpm
# 啟動mysql
]# systemctl start mysqld

(3)登入mysql設定密碼

# 登入mysql,mysql安裝完成後的預設密碼存放在日誌檔案中(/var/log/mysqld.log)
]# grep password /var/log/mysqld.log 
2018-09-03T15:29:11.472751Z 1 [Note] A temporary password is generated for 
[email protected]
: <oTt/#hi!9gy # 登入mysql ]# mysql -uroot -p Enter password: # 由於mysql設定了一些安全策略密碼不能設定太簡單,同時不修改密碼不能做其他操作,先修改密碼策略 mysql> set global validate_password_policy=0; mysql> set global validate_password_length=4; # 修改密碼 mysql> set password=password('dayi123'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)

2、cmake編譯安裝

(1)安裝環境準備

         1)安裝所需依賴

  ]# yum -y install make gcc-c++ cmake bison-devel  ncurses-devel perl ncurses-devel perl-Data-Dumperpython-devel openssl openssl-devel bzip2-devel python-devel

         2)建立所需的使用者和組

]# useradd mysql –s /sbin/nologin

         3)安裝bootst

# 下載解壓bootst
]# cd /usr/local
]# wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
]# tar -xf boost_1_59_0.tar.gz
]# ln -s boost_1_59_0 boost
]# cd boost
# 編譯安裝boost
]# ./bootstrap.sh
]# ./b2
]# sudo ./b2 install

(2)下載安裝mysql資料庫

         1)下載並解壓

]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23.tar.gz
]# tar -xf mysql-5.7.23.tar.gz

         2)編譯安裝mysql

# 進入mysql解壓目錄
]# cd mysql-5.7.23
#建立mysql資料存放目錄並授權
]# mkdir /data/mysql/data -p
]# chown mysql:mysql /data/mysql/ -R
# 編譯
]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7 -DMYSQL_DATADIR=/data/mysql/data/ -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost -DSYSCONFDIR=/etc/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_EMBEDDED_SERVER=1 -DMYSQL_TCP_PORT=3306
# 安裝
]# make && make install

(3)設定mysql環境變數

# 建立mysql安裝目錄軟連線
]# ln -s /usr/local/mysql-5.7 /usr/local/mysql
# 新增mysql環境變數
]# echo "export PATH=/usr/local/mysql/bin:$PATH" >>/etc/profile 
]# source /etc/profile

(4)初始化mysql

# 初始化mysql,在初始化時會建立mysql初始密碼
]# mysqld --initialize --datadir=/data/mysql/data/ --user=mysql --basedir=/usr/local/mysql/         
2018-09-11T20:18:06.992549Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-11T20:18:07.708610Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-11T20:18:07.850613Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-11T20:18:07.943544Z 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: cc0c2024-b5ff-11e8-b3c7-000c298bc3a4.
2018-09-11T20:18:07.948015Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-11T20:18:07.953951Z 1 [Note] A temporary password is generated for [email protected]: ODD)w-Ald0lj

(5)配置mysql

         Mysql初始配置如下

]# cat /etc/my.cnf 
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
socket =/tmp/mysql.sock
pid-file=/data/mysql/data/mysql.pid

[mysqld_safe]
log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
pid-file=/data/mysql/data/mysql01.pid

(6)啟動mysql

# 建立mysql啟動檔案
]# cp /usr/local/src/mysql-5.7.23/support-files/mysql.server /etc/init.d/mysqld
]# chkconfig --add mysqld
# 啟動mysql
]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

(7)修改mysql初始密碼

# 登入mysql,登入時需要需要輸入初始化時獲得的密碼
]# mysql -uroot -p"ODD)w-Ald0lj"
#修改mysql登入密碼
mysql> set password=password('dayi123');  
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(7)cmake編譯常用引數說明

         -DCMAKE_INSTALL_PREFIX=”path”:指向mysql安裝目錄

         -DINSTALL_SBINDIR=sbin:指定可執行檔案目錄

         -DMYSQL_DATADIR=”DATADIR”:指向mysql資料檔案存放目錄

         -DSYSCONFDIR=”confPATH”:指定mysql配置檔案目錄

         -DINSTALL_PLUGINDIR=”plugin-path”:指定外掛存放目錄

         -DINSTALL_MANDIR=share/man:指向man文件目錄

         -DINSTALL_SHAREDIR=share:指向aclocal/mysql.m4安裝目錄

         -DINSTALL_LIBDIR=lib/mysql:指向物件程式碼庫目錄

         -DINSTALL_INCLUDEDIR=include/mysql:指向標頭檔案目錄

         -DINSTALL_INFODIR=share/info:指向info文件存放目錄(prefix/share/info)

         -DWITH_READLINE=1:啟用readline庫支援(提供可編輯的命令列)

         -DWITH_SSL=system:啟用ssl庫支援(安全套接層)

         -DWITH_ZLIB=system:啟用libz庫支援(zib、gzib相關)

         -DWITH_LIBWRAP=0:禁用libwrap庫(實現了通用TCP包裝的功能,為網路服務守護程序使用)

         -DMYSQL_TCP_PORT=3306:指定TCP埠為3306

         -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock:指定mysql.sock路徑

         -DENABLED_LOCAL_INFILE=1:啟用本地資料匯入支援

         -DEXTRA_CHARSETS=all:啟用額外的字符集型別(預設為all)

         -DDEFAULT_CHARSET=utf8:指定預設的字符集為utf8

         -DDEFAULT_COLLATION=utf8_general_ci:設定預設排序規則(utf8_general_ci快速/utf8_unicode_ci準確)

         -DWITH_EMBEDDED_SERVER=1:編譯嵌入式伺服器支援

         -DMYSQL_USER=mysql:指定mysql使用者(預設為mysql)

         -DWITH_DEBUG=0:禁用debug(預設為禁用)

         -DENABLE_PROFILING=0:禁用Profiling分析(預設為開啟)

         -DWITH_COMMENT='string':一個關於編譯環境的描述性註釋

3、通過二進位制包安裝(推薦安裝方法)

         二進位制包是mysql官方已經編譯好的mysql,下載後只需初始化就可以使用,初始化的方法同編譯安裝後初始化方法基本一致。

(1)安裝環境準備

         1)建立程式執行的賬戶

]# useradd mysql -s /sbin/nologin
2)建立資料存放目錄並更改屬主為mysql
]# mkdir /data/mysql/data -p
]# chown mysql:mysql /data/mysql/data/ -R

(2)安裝

         1)下載二進位制包

#下載二進位制包解壓並拷貝至/usr/local/目錄下
]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
]# tar -xf mysql-5.7.23-linux-glibc2.12-i686.tar.gz 
]# mv mysql-5.7.23-linux-glibc2.12-i686 /usr/local/mysql-5.7.32
]# cd /usr/local/
# 建立軟連線並進入mysql目錄
]# ln -s mysql-5.7.32 mysql
]# cd mysql

         2)建立mysql配置檔案

]# cat /etc/my.cnf 
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
socket =/tmp/mysql.sock
pid-file=/data/mysql/data/mysql.pid

[mysqld_safe]
log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
pid-file=/data/mysql/data/mysql01.pid

(3)初始化mysql

l]# bin/mysqld --initialize --datadir=/data/mysql/data/ --user=mysql --basedir=/usr/local/mysql/ 
2018-09-12T11:54:25.680958Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-12T11:54:26.262002Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-12T11:54:26.361054Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-12T11:54:26.420098Z 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: 99075ead-b682-11e8-a82f-000c290ae25c.
2018-09-12T11:54:26.421113Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-12T11:54:26.421999Z 1 [Note] A temporary password is generated for [email protected]: 73H139ippk#Z

(4)啟動mysql

# 將mysql bin目錄加入系統環境變數
]# echo "export PATH=/usr/local/mysql/bin:$PATH" >>/etc/profile
]# . /etc/profile
# 將mysql加入系統服務進行管理
]# cp /usr/local/mysql-5.7.32/support-files/mysql.server /etc/init.d/mysqld
]# chkconfig --add mysqld
#啟動mysql
l]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

(5)修改mysql初始密碼

# 登入mysql修改密碼,第一次登入時需要使用初始化時獲得的密碼
]# mysql -uroot -p73H139ippk#Z
mysql> set password=password('dayi123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;

4、mysql資料目錄說明

    Mysql資料庫安裝完成後,在mysql資料目錄中會為每一個庫建立一個目錄,其中,資料存放目錄下的Ibdata1和ib_logfile0、ib_logfile1三個檔案專為innodb存放資料和日誌的共享檔案。