1、官网下载对应服务器版本的mysql:
我这是centos系统:https://dev.mysql.com/downloads/file/?id=481117
2、在服务器创建好存放压缩文件以及解压文件目录:
(本人习惯放在opt目录下)
[root@instance-61ibcsrp opt]# mkdir softwares
[root@instance-61ibcsrp opt]# mkdir modules
3、用ftp将下载好的压缩文件上传至softwares目录下,解压至modules目录下:
[root@instance-61ibcsrp softwares]# tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /opt/modules/
4、重命名解压后的文件为mysql:
[root@instance-61ibcsrp modules]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
5、在mysql文件夹下创建data目录:
[root@instance-61ibcsrp mysql]# mkdir data
6、添加mysql用户组和用户,并将mysql用户添到mysql组:
[root@instance-61ibcsrp modules]# groupadd mysql
[root@instance-61ibcsrp modules]# useradd -g mysql mysql
7、将mysql目录下所有文件所有者改为mysql用户:
[root@instance-61ibcsrp modules]# chown -R mysql:mysql *
8、安装mysql:
[root@instance-61ibcsrp mysql]# ./bin/mysqld --user=mysql --basedir=/opt/modules/mysql --datadir=/opt/modules/mysql/data --initialize
9、执行以上命令获得初始密码:
2018-11-20T10:10:19.428029Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_tim
2018-11-20T10:10:19.736708Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-11-20T10:10:19.805795Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-11-20T10:10:19.960456Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server aeeb-000c2937887c.
2018-11-20T10:10:19.965735Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-11-20T10:10:19.975224Z 1 [Note] A temporary password is generated for root@localhost: ezsS+hnXq0wp
记下初始密码:ezsS+hnXq0wp
10、mysql目录下除了data目录,其它都改回root所有:
[root@instance-61ibcsrp mysql]# chown -R root .
[root@instance-61ibcsrp mysql]# chown -R mysql data
11、用notepad++修改mysql子目录support-files下的mysql.server配置文件:
修改以下内容
# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
basedir=/opt/modules/mysql
bindir=/opt/modules/mysql/bin
if test -z "$datadir"
then
datadir=/opt/modules/mysql/data
fi
sbindir=/opt/modules/mysql/bin
libexecdir=/opt/modules/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
12、复制相关启动文件,用于启动服务:
[root@instance-61ibcsrp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@instance-61ibcsrp mysql]# chmod 755 /etc/init.d/mysqld
[root@instance-61ibcsrp mysql]# cp bin/my_print_defaults /usr/bin/
13、修改启动脚本mysqld:
[root@instance-61ibcsrp mysql]# vi /etc/init.d/mysqld
修改的内容:
basedir=/opt/modules/mysql/
datadir=/opt/modules/mysql/data
port=3306
14、启动服务:
[root@instance-61ibcsrp mysql]# service mysqld start
15、在 /etc/profile配置mysql启动路径,便于随处目录下都能启动mysql:
[root@instance-61ibcsrp mysql]# vi /etc/profile
加入以下内容
#mysql
export PATH=$PATH:/opt/modules/mysql/bin
刷新配置使其生效
[root@instance-61ibcsrp mysql]# source /etc/profile
16、启动mysql:
密码为上面记下的初始密码:ezsS+hnXq0wp
[root@instance-61ibcsrp mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.7.24
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>
17、重新设置密码:
改为123456
mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
18、配置my.cnf文件(也就是windows里的my.ini):
首先:找到找到mysqld的路径
[root@instance-61ibcsrp mysql]# which mysqld
/opt/modules/mysql/bin/mysqld
然后:根据mysqld路径找到my.cnf配置文件路径
[root@instance-61ibcsrp mysql]# /opt/modules/mysql/bin/mysqld --verbose --help |grep -A 1 'Default options'
mysqld: Can't change dir to '/usr/local/mysql/data/' (Errcode: 2 - No such file or directory)
2018-11-20T06:07:43.563765Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
最后:用notepad++配置该文件
19、开放远程连接权限:
[root@instance-61ibcsrp mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 67
Server version: 5.7.24 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
进入mysql,修改user表里user字段值为root的host字段的值:
若要让所有人都能远程连接,则赋值“%”,
若只给某一台电脑赋值,则直接赋值其ip