1. 下载mysql 安装包
这里下载的是社区版的mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
2. 上传到linux并解压
上传 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz安装包到/usr/local/soft路径下解压,并重命名为mysql
[root@localhost soft]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[root@promote soft]# mv mysql-5.7.25-linux-glibc2.12-x86_64/ mysql
[root@promote soft]# ls
java mycat mysql
[root@promote soft]# ll
total 0
drwxr-xr-x. 3 root root 26 2月 21 15:05 java
drwxr-xr-x. 3 root root 83 2月 21 15:04 mycat
drwxr-xr-x. 9 root root 129 2月 23 17:18 mysql
3.添加mysql用户及用户组
- 添加用户组
groupadd mysql
- 添加用户
useradd -r -g mysql mysql
- 修改mysql目录属主
进入/usr/local/soft目录,执行命令修改属主: chown -R mysql:mysql mysql/
[root@promote soft]# groupadd mysql
[root@promote soft]# useradd -r -g mysql mysql
[root@promote soft]# ls
java mycat mysql
[root@promote soft]# ll
total 0
drwxr-xr-x. 3 root root 26 2月 21 15:05 java
drwxr-xr-x. 3 root root 83 2月 21 15:04 mycat
drwxr-xr-x. 9 root root 129 2月 23 17:18 mysql
[root@promote soft]# chown -R mysql:mysql mysql/
[root@promote soft]# ll
total 0
drwxr-xr-x. 3 root root 26 2月 21 15:05 java
drwxr-xr-x. 3 root root 83 2月 21 15:04 mycat
drwxr-xr-x. 9 mysql mysql 129 2月 23 17:18 mysql
[root@promote soft]#
4.初始化MySQL 配置
初始化mysql命令(路径改为自己的安装路径):
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/soft/mysql --datadir=/usr/local/soft/mysql/data
[root@promote mysql]# pwd
/usr/local/soft/mysql
[root@promote mysql]#
[root@promote mysql]#
[root@promote mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/soft/mysql --datadir=/usr/local/soft/mysql/data
2020-02-23T11:40:32.696129Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-23T11:40:33.039092Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-23T11:40:33.089911Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-23T11:40:33.192600Z 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: 4ce8b141-5631-11ea-bf7b-000c291fdba7.
2020-02-23T11:40:33.193387Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-02-23T11:40:33.196630Z 1 [Note] A temporary password is generated for root@localhost: a2W!=Zv>%;gX
[root@promote mysql]#
初始化完成会分配一个随机密码:
[Note] A temporary password is generated for root@localhost: a2W!=Zv>%;gX 这个密码后面会用到
5. 新建配置文件
新建配置文件etc/my.cnf
[root@promote mysql]# vim /etc/my.cnf
然后将下面的内容贴进去并保存
[mysqld]
basedir=/usr/local/soft/mysql
datadir=/usr/local/soft/mysql/data
socket=/usr/local/soft/mysql/tmp/mysql.sock
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
socket=/usr/local/soft/mysql/tmp/mysql.sock
[mysqld_safe]
log-error=/usr/local/soft/mysql/data/error.log
pid-file=/usr/local/soft/mysql/data/mysql.pidbasedir=/usr/local/mysql
datadir=/usr/local/soft/mysql/data
socket=/usr/local/soft/mysql/tmp/mysql.sock
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
6.将mysql加入服务
[root@promote mysql]# cd support-files/
[root@promote support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@promote support-files]# cp mysql.server /etc/init.d/mysql
7.启动mysql
命令: service mysql start
如果提示: mysqld_safe Directory ‘/usr/local/soft/mysql/tmp’ for UNIX socket file don’t exists. ,创建一个tmp目录
[root@promote support-files]# service mysql start
Starting MySQL.Logging to '/usr/local/soft/mysql/data/error.log'.
2020-02-23T12:06:07.786242Z mysqld_safe Directory '/usr/local/soft/mysql/tmp' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/usr/local/soft/mysql/data/promote.cache-dns.local.pid).
[root@promote support-files]# cd ..
[root@promote mysql]# mkdir tmp
[root@promote mysql]# chown -R mysql:mysql tmp
[root@promote mysql]# ll
total 36
drwxr-xr-x. 2 mysql mysql 4096 2月 23 17:18 bin
-rw-r--r--. 1 mysql mysql 17987 12月 21 2018 COPYING
drwxr-xr-x. 5 mysql mysql 147 2月 23 19:40 data
drwxr-xr-x. 2 mysql mysql 55 2月 23 17:18 docs
drwxr-xr-x. 3 mysql mysql 4096 2月 23 17:18 include
drwxr-xr-x. 5 mysql mysql 230 2月 23 17:18 lib
drwxr-xr-x. 4 mysql mysql 30 2月 23 17:18 man
-rw-r--r--. 1 mysql mysql 2478 12月 21 2018 README
drwxr-xr-x. 28 mysql mysql 4096 2月 23 17:18 share
drwxr-xr-x. 2 mysql mysql 90 2月 23 17:18 support-files
drwxr-xr-x. 2 mysql mysql 6 2月 23 20:07 tmp
[root@promote mysql]# service mysql start
Starting MySQL.Logging to '/usr/local/soft/mysql/data/error.log'.
.. SUCCESS!
[root@promote mysql]#
8. 登录mysql,修改密码
用之前初始化mysql配置后分配的临时密码登陆mysql 需要手动输入密码a2W!=Zv>%;gX
./bin/mysql -u root -p
alter user ‘root’@‘localhost’ identified by ‘liuch’;
[root@promote mysql]# ./bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.25
Copyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by 'liuch';
Query OK, 0 rows affected (0.04 sec)
9.配置远程连接MySQL
创建一个远程连接用户mysql,并授权该用户使用密码liuch远程访问虚拟机上的mysql
use mysql;
create user ‘mysql’@‘localhost’ identified by ‘liuch’;
grant all privileges on . to ‘mysql’@’%’ identified by ‘liuch’ with grant optiion;
flush privileges;
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create user 'mysql'@'localhost' identified by 'liuch';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'mysql'@'%' identified by 'liuch' with grant optiion;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'optiion' at line 1
mysql> grant all privileges on *.* to 'mysql'@'%' identified by 'liuch' with grant option;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> exit
Bye
10.防火墙开放mysql端口
[root@promote mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@promote mysql]# firewall-cmd --reload
success
[root@promote mysql]#