1. 程式人生 > 其它 >Linux-安裝-MySQL5.7

Linux-安裝-MySQL5.7

1、二進位制安裝MySQL5.7

安裝相關包

yum  -y install libaio numactl-libs

使用者和組

groupadd mysql
useradd -r -g mysql -s /bin/false mysql
[root@centos7-liyj ~]#groupadd mysql
[root@centos7-liyj ~]#useradd -r -g mysql -s /bin/false mysql   #建立系統使用者
[root@centos7-liyj ~]#tail /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:
99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin liyanjin:x:
1000:1000:liyanjin:/home/liyanjin:/bin/bash tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin mysql:x:998:1001::/home/mysql:/bin/false
使用者和組建立

下載二進位制程式檔案

下載路徑http://mirrors.163.com/mysql/Downloads/MySQL-5.7/
L
inux系統中:http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

準備程式檔案

[root@centos7-liyj ~]#tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@centos7-liyj ~]#cd /usr/local/
[root@centos7-liyj /usr/local]#ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@centos7-liyj /usr/local]#chown -R root.root /usr/local/mysql/
[root@centos7-liyj /usr/local]#echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7-liyj /usr/local]#. /etc/profile.d/mysql.sh 
修改前檔案內容屬組屬主
[root@centos7-liyj /usr/local/mysql]#ll
total 272
drwxr-xr-x  2 root root    4096 May 10 09:37 bin
drwxr-xr-x  2 root root      55 May 10 09:37 docs
drwxr-xr-x  3 root root    4096 May 10 09:37 include
drwxr-xr-x  5 root root     230 May 10 09:37 lib
-rw-r--r--  1 7161 31415 259253 Nov 30 04:09 LICENSE
drwxr-xr-x  4 root root      30 May 10 09:37 man
-rw-r--r--  1 7161 31415    566 Nov 30 04:09 README
drwxr-xr-x 28 root root    4096 May 10 09:37 share
drwxr-xr-x  2 root root      90 May 10 09:37 support-files
修改後檔案內容屬組屬主
[root@centos7-liyj /usr/local/mysql]#ll
total 272
drwxr-xr-x  2 root root   4096 May 10 09:37 bin
drwxr-xr-x  2 root root     55 May 10 09:37 docs
drwxr-xr-x  3 root root   4096 May 10 09:37 include
drwxr-xr-x  5 root root    230 May 10 09:37 lib
-rw-r--r--  1 root root 259253 Nov 30 04:09 LICENSE
drwxr-xr-x  4 root root     30 May 10 09:37 man
-rw-r--r--  1 root root    566 Nov 30 04:09 README
drwxr-xr-x 28 root root   4096 May 10 09:37 share
drwxr-xr-x  2 root root     90 May 10 09:37 support-files
許可權修改前後對比

準備環境變數

[root@centos7-liyj /usr/local]#echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7-liyj /usr/local]#. /etc/profile.d/mysql.sh 

準備配置檔案

[root@centos7-liyj /usr/local]#cp /etc/my.cnf{,.bak}
[root@centos7-liyj /usr/local]#vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
socket=/data/mysql/mysql.sock

初始化資料庫檔案並提取root密碼

[root@centos7-liyj /data]#mysqld --initialize --user=mysql --datadir=/data/mysql
[root@centos7-liyj /data/mysql]#awk '/temporary password/{print $NF}' /data/mysql/mysql.log
Bu_jLT.yr8,y
[root@centos7-liyj /data]#cd mysql/
[root@centos7-liyj /data/mysql]#ll
total 110664
-rw-r----- 1 mysql mysql       56 May 10 10:26 auto.cnf
-rw------- 1 mysql mysql     1680 May 10 10:26 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 May 10 10:26 ca.pem
-rw-r--r-- 1 mysql mysql     1112 May 10 10:26 client-cert.pem
-rw------- 1 mysql mysql     1676 May 10 10:26 client-key.pem
-rw-r----- 1 mysql mysql      436 May 10 10:26 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 10 10:26 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 10 10:26 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 10 10:26 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 May 10 10:26 mysql
-rw-r----- 1 mysql mysql     1108 May 10 10:26 mysql.log
drwxr-x--- 2 mysql mysql     8192 May 10 10:26 performance_schema
-rw------- 1 mysql mysql     1680 May 10 10:26 private_key.pem
-rw-r--r-- 1 mysql mysql      452 May 10 10:26 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 May 10 10:26 server-cert.pem
-rw------- 1 mysql mysql     1680 May 10 10:26 server-key.pem
drwxr-x--- 2 mysql mysql     8192 May 10 10:26 sys
[root@centos7-liyj /data/mysql]#cat mysql.log 
2022-05-10T02:26:08.549541Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-05-10T02:26:08.668742Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-05-10T02:26:08.689686Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-05-10T02:26:08.743917Z 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: 8d1ce2f2-d008-11ec-8556-005056399ee0.
2022-05-10T02:26:08.745068Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-05-10T02:26:09.353933Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-05-10T02:26:09.353947Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-05-10T02:26:09.354427Z 0 [Warning] CA certificate ca.pem is self signed.
2022-05-10T02:26:09.459455Z 1 [Note] A temporary password is generated for root@localhost: Bu_jLT.yr8,y
生成的資料檔案和密碼

方式2: 生成 root 空密碼

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

準備服務指令碼和啟動

[root@centos7-liyj /data/mysql]#cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@centos7-liyj /data/mysql]#chkconfig --add mysqld

[root@centos7-liyj /data/mysql]#systemctl start mysqld
[root@centos7-liyj /data/mysql]#ss -antl
State      Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN     0      128                         *:22                                      *:*                  
LISTEN     0      100                 127.0.0.1:25                                      *:*                  
LISTEN     0      128                 127.0.0.1:6010                                    *:*                  
LISTEN     0      128                 127.0.0.1:6011                                    *:*                  
LISTEN     0      80                       [::]:3306                                 [::]:*                  
LISTEN     0      128                      [::]:22                                   [::]:*                  
LISTEN     0      100                     [::1]:25                                   [::]:*                  
LISTEN     0      128                     [::1]:6010                                 [::]:*                  
LISTEN     0      128                     [::1]:6011                                 [::]:*                  
埠3306
修改口令---修改前面生成的隨機密碼為指定密碼
[root@centos7-liyj /data/mysql]#mysqladmin -uroot -p'Bu_jLT.yr8,y' password atech123456
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

登入測試
[root@centos7-liyj /data/mysql]#mysql -uroot -patech123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

#修改前面生成的空密碼為指定密碼

mysqladmin -uroot   password atech123456