wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
跳转到mysql包的目录下输入如下命令解压文件
tar -Jxvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz 输入
注意我这里下载的是**.tar.xz格式的包所以用tar -Jxvf 命令如果是**.tar.gz的请使用 tar -zxvf 命令
解压后会的到如下图的文件夹并将其重命名
注意这个步骤可以省略!mysql在初始化数据库时会去寻找/etc/my.cnf这个配置文件,当找不到这个配置文件是将使用系统默认的配置,来初始化mysql数据库。
在/etc 目录下创建 my.cnf 配置文件内容如下:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数/
max_connections=10000
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
#character-set-server=UTF8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
user=mysql
在mysql安装目录下创建数data目录
[root@ecs-s6-medium-2-linux-20191122095023 mysql]# mkdir data
创建用户组及用户并给权限
[root@ecs-s6-medium-2-linux-20191122095023 mysql]# groupadd mysql
[root@ecs-s6-medium-2-linux-20191122095023 mysql]# useradd -rg mysql mysql
[root@ecs-s6-medium-2-linux-20191122095023 mysql]# chown -R mysql:mysql /usr/local/mysql/
初始化mysql数据库
[root@ecs-s6-medium-2-linux-20191122095023 mysql]# pwd
/usr/local/mysql
[root@ecs-s6-medium-2-linux-20191122095023 mysql]# su mysql
bash-4.1$ ./bin/mysqld --initialize
2019-11-25T02:01:48.485052Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.18) initializing of server in progress as process 15896
2019-11-25T02:01:48.485955Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2019-11-25T02:01:51.491886Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: >wgM7>hh*L;=
bash-4.1$
bash-4.1$ cd support-files/
bash-4.1$ ls
mysqld_multi.server mysql-log-rotate mysql.server
bash-4.1$ ./mysql.server start
Starting MySQL. [ OK ]
bash-4.1$
连接mysql
[root@ecs-s6-medium-2-linux-20191122095023 mysql-8.0]#
[root@ecs-s6-medium-2-linux-20191122095023 mysql-8.0]# pwd
/usr/local/mysql
[root@ecs-s6-medium-2-linux-20191122095023 mysql-8.0]# ./bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18
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>
将root用户的密码设置为 root@123456
mysql>
mysql> alter user 'root'@'localhost' identified by 'root@123456';
Query OK, 0 rows affected (0.02 sec)
mysql>
为mysql.server文件在/etc/init.d目录下创建软连接并重新启动服务器
[root@ecs-s6-medium-2-linux-20191122095023 init.d]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@ecs-s6-medium-2-linux-20191122095023 init.d]#
[root@ecs-s6-medium-2-linux-20191122095023 init.d]# service mysqld stop
Shutting down MySQL.. [ OK ]
[root@ecs-s6-medium-2-linux-20191122095023 init.d]# service mysqld start
Starting MySQL. [ OK ]
[root@ecs-s6-medium-2-linux-20191122095023 init.d]#
注意当你mysql安装的路径不是/usr/local/mysql 时启动可能会报错
这时我们需要修改下mysql启动脚本的配置文件
bash-4.1$ vim mysql.server
bash-4.1$ vim mysqld_multi.server
./bin/mysql -u root -p
use mysql
update user set host = "%" where user = "root";
select host, user from user;
flush privileges;
[root@ecs-s6-medium-2-linux-20191122095023 mysql-8.0]#
[root@ecs-s6-medium-2-linux-20191122095023 mysql-8.0]# pwd
/usr/local/mysql
[root@ecs-s6-medium-2-linux-20191122095023 mysql-8.0]# ./bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18
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> 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>
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
mysql> select host, user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+
4 rows in set (0.00 sec)
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
注意: 如果其他用户想允许其他IP登录也是这样设置