MySQL數(shù)據(jù)庫有四種安裝方法:
- 源碼包編譯安裝
- RPM包安裝
- 二進(jìn)制文件安裝
- 官方y(tǒng)um源安裝
這里我們主要介紹二進(jìn)制包的安裝方法
在MySQL官網(wǎng)下載二進(jìn)制包并且上傳到服務(wù)器上
解壓二進(jìn)制包
[root@localhost software]# tar zxvf mysql-5.6.32-linux-glibc2.5-x86_64.tar.gz```
> 創(chuàng)建MySQL軟件的運(yùn)行用戶
[root@localhost software]# useradd mysql -s /sbin/nologin -M
[root@localhost software]# id mysql
uid=500(mysql) gid=500(mysql) groups=500(mysql)```
將解壓出來的二進(jìn)制包移到到MySQL的安裝路徑/usr/local下
[root@linux1 software]# cp -R mysql-5.6.32-linux-glibc2.5-x86_64 /usr/local/
切換到/usr/local路徑下穿件mysql的軟連接方便以后記錄版本號(hào)和升級(jí)
[root@linux1 software]# cd /usr/local/
[root@linux1 local]# ln -s mysql-5.6.32-linux-glibc2.5-x86_64/ mysql
[root@linux1 local]# ll
total 44
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
lrwxrwxrwx 1 root root 35 Nov 17 23:22 mysql -> mysql-5.6.32-linux-glibc2.5-x86_64/
drwxr-xr-x 13 root root 4096 Nov 17 23:22 mysql-5.6.32-linux-glibc2.5-x86_64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 5 root root 4096 Nov 17 22:36 share
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
初始化安裝MySQL數(shù)據(jù)庫
初始化的過程中可能會(huì)遇到缺少libaio的報(bào)錯(cuò),如下:
自行yum安裝即可
[root@linux1 mysql]# yum install libaio libaio-devel -y
修改data目錄權(quán)限,初始化安裝數(shù)據(jù)庫
[root@localhost mysql]# chown -R mysql:mysql data/
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
修改MySQL的配置文件
[root@linux1 mysql]# vim my.cnf
cp: overwrite `/etc/my.cnf'? y
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql #MySQL的安裝目錄
datadir = /usr/local/mysql/data #MySQL的數(shù)據(jù)目錄
port = 3306 #MySQL的端口號(hào)愈犹,可做多實(shí)例安裝
server_id = 131
socket = /tmp/mysql.sock #通信socket莺匠,多實(shí)例安裝時(shí)指定各自的sock
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
拷貝MySQL的配置文件copy到默認(rèn)/etc/my.cnf下
[root@linux1 mysql]# /bin/cp my.cnf /etc/my.cnf #使用/bin/cp命令可以直接覆蓋源文件不出現(xiàn)提示,在腳本中可以應(yīng)用
將MySQL的系統(tǒng)命令復(fù)制到bin目錄下
[root@linux1 bin]# cp * /usr/bin/
指定配置文件啟動(dòng)MySQL
養(yǎng)成指定配置文件的習(xí)慣懒构,因?yàn)樵诙鄬?shí)例安裝的時(shí)候可能使用不同的配置文件餐济,雖然默認(rèn)的配置文件存放在/etc/my.cnf下
[root@linux1 bin]# ./mysqld_safe --defaults-file=/etc/my.cnf
161117 23:42:39 mysqld_safe Logging to '/usr/local/mysql/data/linux1.err'.
161117 23:42:39 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
^Z
[1]+ Stopped ./mysqld_safe --defaults-file=/etc/my.cnf
檢查是否啟動(dòng)MySQL服務(wù)
[root@linux1 bin]# ps -ef | grep mysql
root 1842 1740 0 23:42 pts/1 00:00:00 /bin/sh ./mysqld_safe --defaults-file=/etc/my.cnf
mysql 1986 1842 0 23:42 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/linux1.err --pid-file=/usr/local/mysql/data/linux1.pid --socket=/tmp/mysql.sock --port=3306
root 2013 1740 0 23:45 pts/1 00:00:00 grep mysql
[root@linux1 bin]# netstat -lntup | grep 3306
tcp 0 0 :::3306 :::* LISTEN 1986/mysqld
創(chuàng)建MySQL的數(shù)據(jù)庫管理員的賬號(hào)和密碼
此處同樣建議指定socket文件啟動(dòng),socket文件的路徑設(shè)置到my.cnf文件下
[root@linux1 ~]# mysqladmin -uroot password -S /tmp/mysql.sock
New password:
Confirm new password:
成功進(jìn)入MySQL數(shù)據(jù)庫
[root@linux1 ~]# 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.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.05 sec)
mysql>