1. 程式人生 > >資料庫mysql-5.7.17的極速賽_車平臺出_售安裝詳解

資料庫mysql-5.7.17的極速賽_車平臺出_售安裝詳解

資料庫極速賽_車平臺出_售的安裝詳解 Q1157880099 1、安裝前的準備工作 1.1、系統基本資訊

為給安裝過程減少麻煩,我這裡已經提前關閉了防火牆和selinux。我這裡是純淨的系統,沒有安裝過MySQL和mariadb,如果機器上有安裝過MySQL和mariadb的請先解除安裝後再進行安裝。

[[email protected] ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[[email protected] ~]# ifconfig

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

    inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255

    inet6 fe80::20c:29ff:fe48:bb2f  prefixlen 64  scopeid 0x20<link>

    ether 00:0c:29:48:bb:2f  txqueuelen 1000  (Ethernet)

    RX packets 464050  bytes 681487546 (649.9 MiB)

    RX errors 0  dropped 0  overruns 0  frame 0

    TX packets 27659  bytes 3103086 (2.9 MiB)

    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

1.2、系統記憶體檢查

安裝5.6以及以上版本的mysql需要伺服器的記憶體至少在1G以上。檢查一下 linux 系統的記憶體大小,如果記憶體不足 1G,啟動 mysql 的時候可能會產生下面這個錯誤提示:

Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code.

See “systemctl status mysqld.service” and “journalctl -xe” for details.[FAILED] 1.3、下載並上傳MySQL安裝包到伺服器

我這裡下載和使用的安裝包是 mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 1.4、規劃好相關路徑

安裝包上傳目錄:/root

Mysql目錄安裝位置:/usr/local/mysql-5.7

資料庫儲存位置:/data/mysql-5.7 2、安裝MySQL-5.7.17 2.1、解壓縮並複製mysql-5.7.17到/usr/local下

[[email protected] ~]# tar -xf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

[[email protected] ~]# cp -a mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql-5.7 2.2、建立相關檔案目錄

[[email protected] ~]# mkdir -p /data/mysql-5.7 2.3、新建mysql使用者和組(組會在建立使用者時自動建立)

[[email protected] ~]# useradd -r -s /sbin/nologin mysql

[[email protected] ~]# more /etc/group|grep mysql

mysql❌994: 2.4、檢查是否安裝了 libaio包

[[email protected] ~]# rpm -qa | grep libaio

libaio-0.3.109-13.el7.x86_64 2.5、給MySQL相關目錄授權

[[email protected] mysql-5.7]# chown -R mysql:mysql /usr/local/mysql-5.7/

[[email protected] mysql-5.7]# chown -R mysql /data

[[email protected] mysql-5.7]# ls -l /data/

drwxr-xr-x 3 mysql root 22 9月 28 00:33 log

drwxr-xr-x 2 mysql root 6 9月 28 00:32 mysql-5.7 2.6、安裝MySQL

[[email protected] ~]# cd /usr/local/mysql-5.7/

[[email protected] mysql-5.7]# ls

bin COPYING docs include lib man README share support-files

[[email protected] mysql-5.7]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7/ --datadir=/data/mysql-5.7/

2018-09-28×××1:24:32.486082Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-09-28×××1:24:32.726180Z 0 [Warning] InnoDB: New log files created, LSN=45790

2018-09-28×××1:24:32.774352Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2018-09-28×××1:24:32.842600Z 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: 1295295f-c311-11e8-bd4b-000c2948bb2f.

2018-09-28×××1:24:32.846975Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.

2018-09-28×××1:24:32.848046Z 1 [Note] A temporary password is generated for [email protected]: .L9of#e46sSu #這個密碼連線資料庫需要用 3、MySQL的相關配置 3.1、配置my.cnf檔案(這裡只是簡單的配置,生產環境需要更詳細配置)

[[email protected] mysql-5.7]# cp -a support-files/my-default.cnf /etc/my.cnf

[[email protected] mysql-5.7]# vim /etc/my.cnf

[mysqld]

user = mysql

basedir = /usr/local/mysql-5.7

datadir = /data/mysql-5.7

port = …

server_id = …

character-set-server = utf8

socket = /data/mysql-5.7/sock/mysql.sock

pid-file = /data/mysql-5.7/sock/mysql.pid

log-error = /data/mysql-5.7/log/error.log 3.2、建立日誌和pid目錄並授權

在MySQL初始化之前,MySQL資料目錄不能有任何檔案和目錄,不然會報錯,所以sock和log目錄需要在初始化完之後的這裡建立。

[[email protected] mysql-5.7]# mkdir /data/mysql-5.7/sock

[[email protected] mysql-5.7]# mkdir /data/mysql-5.7/log

[[email protected] mysql-5.7]# chown -R mysql.mysql /data 3.3、建立錯誤日誌檔案並授權

上面配置檔案中指定了錯誤日誌,這裡必須建立日誌檔案,不然啟動MySQL時會報以下錯誤。

[[email protected] mysql-5.7]# ./support-files/mysql.server start

Starting MySQL.2018-09-28×××2:44:56.562761Z mysqld_safe error: log-error set to ‘/data/mysql-5.7/log/error.log’, however file don’t exists. Create writable for user ‘mysql’.

ERROR! The server quit without updating PID file (/data/mysql-5.7/sock/mysql.pid).

解決方法:

[[email protected] mysql-5.7]# echo ’ ’ >/data/mysql-5.7/log/error.log

[[email protected] mysql-5.7]# chown mysql.mysql /data/mysql-5.7/log/error.log 3.4、啟動MySQL

[[email protected] mysql-5.7]# ./support-files/mysql.server start

Starting MySQL.2018-09-28×××1:30:28.616264Z mysqld_safe The file /usr/local/mysql/bin/mysqld

does not exist or is not executable. Please cd to the mysql installation

directory and restart this script from there as follows:

./bin/mysqld_safe&

ERROR! The server quit without updating PID file (/data/mysql-5.7/sock/mysql.pid).

解決方法

[[email protected] mysql-5.7]# ln -s /usr/local/mysql-5.7 /usr/local/mysql

[[email protected] mysql-5.7]# ./support-files/mysql.server start

Starting MySQL. SUCCESS!

啟動MySQL也可以使用下面的命令

[[email protected] mysql-5.7]# ./bin/mysqld_safe & 3.5、設定mysql使用service管理

[[email protected] mysql-5.7]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

[[email protected] mysql-5.7]# chmod +x /etc/rc.d/init.d/mysqld

[[email protected] mysql-5.7]# chkconfig --add mysqld

[[email protected] mysql-5.7]# chkconfig --list mysqld

[[email protected] mysql-5.7]# echo ‘export PATH=$PATH:/usr/local/mysql-5.7/bin/’ >>/etc/profile

[[email protected] mysql-5.7]# source /etc/profile

[[email protected] mysql-5.7]# vim /etc/init.d/mysqld

basedir=/usr/local/mysql-5.7/

datadir=/data/mysql

這裡可以使用service命令啟動和停止MySQL,無法使用systemctl啟動和停止

[[email protected] mysql-5.7]# service mysqld stop

Shutting down MySQL… SUCCESS!

[[email protected] mysql-5.7]# service mysqld start

Starting MySQL. SUCCESS!

[[email protected] system]# mysql -u root –p #連線mysql報錯

Enter password:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

解決方法:

[[email protected] system]# ln -s /data/mysql-5.7/sock/mysql.sock /tmp/mysql.sock

[[email protected] system]# mysql -u root -p

Enter password: #這裡輸入的密碼就是上面初始化時生成的密碼

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

Your MySQL connection id is 3

Server version: 5.7.17

………………………………………………

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

mysql> show databases; #檢視資料庫報錯,要求修改密碼

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

#更改密碼方法1

mysql> set password for [email protected]=password(‘123’);

Query OK, 0 rows affected, 1 warning (0.00 sec)

#更改密碼方法2

mysql> update mysql.user set authentication_string=password(‘123456’) where user=‘root’;

Query OK, 0 rows affected, 1 warning (0.00 sec)

Rows matched: 1 Changed: 0 Warnings: 1

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec) 3.6、設定mysql使用systemctl管理

[[email protected] ~]# cp -a /usr/local/mysql-5.7/support-files/mysql.server /usr/lib/systemd/system

[[email protected] ~]# cd /usr/lib/systemd/system

[[email protected] system]# vim mysqld.service

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/usr/local/mysql-5.7/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE=5000

說明:ExecStart=/usr/local/mysql-5.7/bin/mysqld是mysql安裝目錄中查詢的二進位制檔案。

[[email protected] system]# systemctl stop mysqld

[[email protected] system]# systemctl start mysqld

[[email protected] system]# ss -ant|grep 3306

LISTEN 0 80 :::3306 ::?

到這裡我們的MySQL就安裝完成了。這裡只是一個安裝思路及過程,如果在生產環境使用,可以根據需要修改mysql的安裝路徑,資料存放路徑,日誌存放路徑及日誌格式,還需要設定防火牆開放3306埠,設定複雜的資料庫密碼,還有各種優化等需要做。