1、下载二进制安装包 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
cd /usr/local/src
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
2、解压,创建用户
[root@8d83e8f22faa src]# tar -xzvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@8d83e8f22faa src]# ll
total 629300
drwxr-xr-x. 9 root root 129 Sep 4 13:41 mysql-5.7.23-linux-glibc2.12-x86_64
-rw-r--r--. 1 root root 644399365 Sep 4 13:38 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@8d83e8f22faa src]# mv mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
[root@8d83e8f22faa src]# cd /usr/local/mysql/
[root@8d83e8f22faa mysql]# ll
total 36
-rw-r--r--. 1 7161 31415 17987 Jun 8 17:56 COPYING
-rw-r--r--. 1 7161 31415 2478 Jun 8 17:56 README
drwxr-xr-x. 2 root root 4096 Sep 4 13:40 bin
drwxr-xr-x. 2 root root 55 Sep 4 13:41 docs
drwxr-xr-x. 3 root root 4096 Sep 4 13:39 include
drwxr-xr-x. 5 root root 230 Sep 4 13:41 lib
drwxr-xr-x. 4 root root 30 Sep 4 13:40 man
drwxr-xr-x. 28 root root 4096 Sep 4 13:41 share
drwxr-xr-x. 2 root root 90 Sep 4 13:41 support-files
[root@8d83e8f22faa mysql]# groupadd mysql //创建用户组mysql
[root@8d83e8f22faa mysql]# useradd -r -g mysql mysql //-r参数表示mysql用户是系统用户,不可用于登录系统,创建用户mysql并将其添加到用户组mysql中
[root@8d83e8f22faa mysql]# chown -R mysql ../mysql/
[root@8d83e8f22faa mysql]# chgrp -R mysql ../mysql/
3、创建配置文件
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid --会有问题
pid-file=/usr/local/mysql/data/mysqld.pidlower_case_table_names = 1 #不区分大小写
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
default-time_zone = '+8:00'
EOF
4、初始化数据库
[root@8d83e8f22faa mysql]# yum install libaio numactl
[root@8d83e8f22faa log]# touch mysqld.log
[root@8d83e8f22faa log]# chown -R mysql:mysql mysqld.log
[root@8d83e8f22faa log]# vi /root/.bash_profile #增加mysql的bin目录
PATH=/usr/local/mysql/bin:/data/mysql/lib:$PATH
export PATH
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]# source /root/.bash_profile
[root@8d83e8f22faa log]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messagges=en_US
5、查看初始密码
[root@8d83e8f22faa log]# cat mysqld.log
2018-09-04T05:57:11.728815Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-04T05:57:13.354532Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-04T05:57:13.697469Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-04T05:57:13.765091Z 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: 5eddbeeb-b007-11e8-a05a-0242ac110002.
2018-09-04T05:57:13.768220Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-04T05:57:13.770678Z 1 [Note] A temporary password is generated for root@localhost: 5qP,dgGgD(zg
[root@8d83e8f22faa log]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@8d83e8f22faa tmp]# mkdir -p /var/lib/mysql
[root@8d83e8f22faa tmp]# chmod 777 /var/lib/mysql/
[root@8d83e8f22faa tmp]# chown -R mysql:mysql /var/lib/mysql/
[root@8d83e8f22faa tmp]# cd /var/run
[root@8d83e8f22faa run]# mkdir -p /var/run/mysqld
[root@8d83e8f22faa run]# chown -R mysql:mysql /var/run/mysqld
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]# chkconfig --add mysqld
[root@8d83e8f22faa log]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]#
[root@8d83e8f22faa log]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@8d83e8f22faa log]# systemctl status mysqld.service
â— mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2018-09-04 14:08:43 CST; 9s ago
Docs: man:systemd-sysv-generator(8)
Process: 328 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=1/FAILURE)
Sep 04 14:08:39 8d83e8f22faa systemd[1]: Starting LSB: start and stop MySQL...
Sep 04 14:08:43 8d83e8f22faa mysqld[328]: Starting MySQL... ERROR! The server quit without updating PID file (/var/run/mysqld/mysqld.pid).
Sep 04 14:08:43 8d83e8f22faa systemd[1]: mysqld.service: control process exited, code=exited status=1
Sep 04 14:08:43 8d83e8f22faa systemd[1]: Failed to start LSB: start and stop MySQL.
Sep 04 14:08:43 8d83e8f22faa systemd[1]: Unit mysqld.service entered failed state.
Sep 04 14:08:43 8d83e8f22faa systemd[1]: mysqld.service failed.
[root@8d83e8f22faa log]# cat mysqld.log
...
2018-09-04T06:08:40.929634Z 0 [ERROR] Could not create unix socket lock file /var/lib/mysql.sock.lock.
2018-09-04T06:08:40.929653Z 0 [ERROR] Unable to setup unix socket lock file.
2018-09-04T06:08:40.929661Z 0 [ERROR] Aborting
...
[root@8d83e8f22faa lib]# vi /etc/my.cnf
[root@8d83e8f22faa lib]# systemctl start mysqld
[root@8d83e8f22faa lib]# ps -ef | grep mysql
root 1579 1 0 14:18 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/var/run/mysqld/mysqld.pid
mysql 1827 1579 3 14:18 ? 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=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 1857 32 0 14:18 ? 00:00:00 grep --color=auto mysql
[root@8d83e8f22faa lib]#
[root@8d83e8f22faa lib]#
[root@8d83e8f22faa lib]#
[root@8d83e8f22faa lib]#
6、启动服务,进入mysql,修改初始密码,运行远程连接(这里执行完后,密码将变成:abc123)
[root@8d83e8f22faa lib]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23
Copyright (c) 2000, 2018, 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>
mysql>
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
mysql>
mysql> SET PASSWORD = PASSWORD('abc123'); #重置密码
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> UPDATE `mysql`.`user` SET `Host` = '%', `User` = 'root' WHERE (`Host` = 'localhost') AND (`User` = 'root'); #设置允许root账号远程登录
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@8d83e8f22faa lib]#
[root@8d83e8f22faa lib]#
[root@8d83e8f22faa lib]#
[root@8d83e8f22faa lib]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql>
mysql>
mysql> quit
Bye
[root@8d83e8f22faa lib]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 11854 bytes 20197549 (19.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7746 bytes 538027 (525.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 4 bytes 200 (200.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 200 (200.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0