MySQL5.7單機版安裝
阿新 • • 發佈:2018-12-08
準備環境
- 作業系統:CentOS 7.4 64位
- MySQL:mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
關閉firewalld
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service
關閉NetworkManager
[root@localhost ~]# systemctl stop NetworkManager.service
[root@localhost ~]# systemctl disable NetworkManager.service
關閉selinux
[root@localhost ~]# vi /etc/selinux/config
修改為以下內容:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
配置IP地址
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
修改為以下內容:
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=no
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.11
NETMASK=255.255.255.0
GATEWAY=192.168.1.2
修改DNS解析
[root@localhost ~]# vi /etc/resolv.conf
修改為以下內容:
nameserver 192.168.1.2
安裝基礎工具
[root@localhost ~]# yum install -y net-tools vim lrzsz tree screen wget
開始安裝
建立mysql使用者
- 建立mysql使用者組
- 建立mysql使用者,並加入mysql使用者組
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql mysql
上傳MySQL安裝包
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
或者上傳本地已有安裝包
[root@localhost src]# rz
或者上傳本地已有安裝包mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz:
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# rz
解壓安裝包
[root@localhost src]# ls
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -zxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/
[root@localhost src]# ln -s /usr/local/mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost src]# cd
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql-5.7.20-linux-glibc2.12-x86_64
建立初始化資料夾
[root@localhost ~]# mkdir -p /usr/local/mysql/run
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/run
[root@localhost ~]# mkdir -p /usr/local/mysql/log
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/log
[root@localhost ~]# echo "" > /usr/local/mysql/log/mysqld.log
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/log/mysqld.log
配置/etc/my.cnf
[root@localhost ~]# cp /etc/my.cnf my.cnf.bak
[root@localhost ~]# vim /etc/my.cnf
修改為以下內容:
[client]
port=3306
socket = /usr/local/mysql/run/mysqld.sock
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /usr/local/mysql/run/mysqld.sock
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 新增skip-grant-tables,則可以實現無金鑰驗證
# skip-grant-tables
[mysql]
no-auto-rehash
socket = /usr/local/mysql/run/mysqld.sock
[mysqld_safe]
log-error=/usr/local/mysql/log/mysqld.log
pid-file=/usr/local/mysql/run/mysqld.pid
注意:5.7.18以上版本在沒有my-default.cnf
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
配置環境變數
[root@localhost ~]# vim /etc/profile
追加以下內容:
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
使配置檔案生效:
[root@localhost ~]# source /etc/profile
配置service服務
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# vim /etc/init.d/mysqld
修改basedir和datadir引數值:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
初始化資料庫
[[email protected] ~]# cd /usr/local/mysql/bin/
[[email protected] bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2017-11-26T05:21:53.374950Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-26T05:21:53.375022Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2017-11-26T05:21:53.375027Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2017-11-26T05:21:53.807519Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-11-26T05:21:53.869441Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-26T05:21:53.936198Z 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: b6dc0859-d269-11e7-b427-000c298afdc8.
2017-11-26T05:21:53.937310Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-26T05:21:53.954724Z 1 [Note] A temporary password is generated for [email protected]: i#yed,qj,2(A
手動啟動MySQL
[root@localhost bin]# ./mysqld_safe --user=mysql &
檢查啟動情況,啟動成功如下:
[[email protected] bin]# ps -ef|grep mysql
root 11551 990 0 13:23 pts/0 00:00:00 /bin/sh ./mysqld_safe --user=mysql
mysql 11702 11551 1 13:23 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
root 11732 990 0 13:23 pts/0 00:00:00 grep --color=auto mysql
登入客戶端
mysql第一次root使用者登入密碼為初始化時生成的密碼,本次為 i#yed,qj,2(A
登入後,重新設定root使用者密碼
[root@localhost bin]# ./mysql -uroot -p
Enter password:
mysql> set password=password('新密碼');
mysql> exit;
[root@localhost bin]#
忘記密碼時,先停止mysql,再以skip-grant-tables啟動:
停止mysql:
[[email protected] bin]# ps -ef|grep mysql
root 11743 10345 0 16:17 pts/1 00:00:00 /bin/sh ./mysqld_safe --user=mysql --skip-grant-tables
mysql 11936 11743 0 16:17 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --skip-grant-tables --log-error=/usr/local/mysql/log/mysqld.log --pid-file=/usr/local/mysql/run/mysqld.pid --socket=/usr/local/mysql/run/mysqld.sock --port=3306
root 12132 10345 0 16:25 pts/1 00:00:00 grep --color=auto mysql
[[email protected] bin]# kill -9 11743
[[email protected] bin]# kill -9 11936
[1]+ 已殺死 ./mysqld_safe --user=mysql --skip-grant-tables
安全模式下,啟動mysql,並修改mysql的root使用者密碼:
[root@localhost bin]# ./mysqld_safe --user=mysql --skip-grant-tables &
[root@localhost bin]# ./mysql -u root -p
Enter password:
mysql> update mysql.user set authentication_string=password('新密碼') where user='root'and Host = 'localhost';
mysql> exit;
[root@localhost bin]#
配置遠端訪問
建立mysql使用者,並開放遠端訪問許可權:
[root@localhost bin]# ./mysql -uroot -p
Enter password:
mysql> grant all privileges on *.* to '使用者名稱'@'%' identified by '使用者密碼' with grant option;
mysql> flush privileges;
mysql> exit;
[root@localhost bin]#
注意:如果沒有關閉防火牆,則需要開啟3306埠
設定開機啟動
[root@localhost bin]# chkconfig --add mysqld
[root@localhost bin]# chkconfig mysqld on
[root@localhost bin]# chkconfig --list
注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置資料
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務,請執行 'systemctl list-unit-files'。
檢視在具體 target 啟用的服務請執行
'systemctl list-dependencies [target]'。
mysqld 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
檢查mysql啟動/停止服務命令:
[root@localhost bin]# systemctl start mysqld
[root@localhost bin]# systemctl status mysqld
[root@localhost bin]# mysql -h 192.168.56.11 -u mysql -p
Enter password:
mysql> exit;
[root@localhost bin]# reboot
檢查開機啟動
[root@localhost ~]# systemctl status mysqld
[root@localhost bin]# mysql -h 192.168.56.11 -u mysql -p
Enter password:
mysql> exit;