环境
操作系统:Linux iZ2ze47rd7nthnbzdggsvrZ 3.10.0-1127.19.1.el7.x86_64 #1 SMP Tue Aug 25 17:23:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
MySQL版本:mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
1. 从官网下载 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
经测试, 本文还适用于如下版本:
mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
官网: http://dev.mysql.com/downloads/mysql/
2.检查你所用的Linux下有没有安装过mysql
#rpm -qa|grep -i mysql
mysql-5.7.13-linux-glibc2.5-x86_64
可见已经安装了库文件,应该先卸载,不然会出现覆盖错误。注意卸:载时使用了--nodeps选项,忽略了依赖关系:
#rpm -e mysql-5.7.13-linux-glibc2.5-x86_64 --nodeps
当然你的也可能不止这一个文件,也可能有多个,那么你就依次 rpm -e xx --nodeps 卸载,卸载完了再检查一下,若确定删除干净后,在经行下面步骤。
3. 创建mysql的用户组/用户, data目录及其用户目录
在这步之前一定要先确保你所建的用户以及用户组没有存在,要不然在后面的过程中会报错,删除时候要先删除用户在删除用户组名。
# userdel mysql # 删除用户
# groupdel mysql # 删除用户组名
# mkdir /home/mysql # 在home文件夹下创建文件夹mysql
# mkdir /home/mysql/data # 在mysql文件夹下创建文件夹data
# groupadd mysql # 创建一个名为mysql的用户组
# useradd -g mysql -d /home/mysql mysql # 在用户组下创建用户
4、解压安装包并将解压包里的内容拷贝到mysql的安装目录/home/mysql
# tar -xzvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz # 解压文件
# cd mysql-5.7.13-linux-glibc2.5-x86_64 # 进入
# mv * /home/mysql # 移动到之前我创建的mysql文件夹下。
5、初始化mysql数据库
# cd /home/mysql # 进入安装目录
# ./bin/mysql_install_db --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data
[WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2015-11-10 02:09:23
[WARNING] The bootstrap log isn't empty: 2015-11-10 02:09:23
[WARNING] 2015-11-10T10:09:18.114182Z 0
[Warning] --bootstrap is deprecated. Please consider using --initialize instead 2015-11-10T10:09:18.129343Z 0
[Warning] Changed limits: max_open_files: 1024 (requested 5000) 2015-11-10T10:09:18.129408Z 0
[Warning] Changed limits: table_open_cache: 431 (requested 2000)
mysql5.7新特性:由上面可以看出, mysql_install_db 已经不再推荐使用了,建议改成 mysqld --initialize 完成实例初始化。
# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
# ./mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
[ERROR] --initialize specified but the data directory has files in it. Aborting. 2016-04-08T01:46:53.155879Z 0
[ERROR] Aborting
出现上面的错误是因为我们没有清空mysql的data目录, 执行清空命令如下:
# cd /home/mysql/data # 进入安装目录下的data目录
# rm -fr * # 清空数据
# cd /home/mysql
# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-04-08T01:47:59.945537Z 0
[Warning] InnoDB: New log files created, LSN=45790 2016-04-08T01:48:00.333528Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-04-08T01:48:00.434908Z 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: ece26421-fd2b-11e5-a1e3-00163e001e5c. 2016-04-08T01:48:00.440125Z 0
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-04-08T01:48:00.440904Z 1
[Note] A temporary password is generated for root@localhost: **mjT,#x_5sW
牢记上面的随机密码, 如上
**mjT,#x_5sW
, 下面我们修改密码时需要用到。
6、检测下是否能启动mysql服务
# cd /home/mysql
# ./support-files/mysql.server start
Starting MySQL.. OK!
这是正常的情况下。
刚刚我用的是/home/mysql为mysql的安装目录basedir, 则在启动服务时会出现如下错误:
# ./support-files/mysql.server start
./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
由上面可知mysql的tar.gz安装包的默认安装目录为/usr/local/mysql, 这时候我们需要修改/support-files/mysql.server文件的basedir和datadir目录路径为我们环境所在的mysql的basedir和datadir路径, 如下:
# vim support-files/mysql.server
--------------------------
...
basedir=/home/mysql
datadir=/home/mysql/data
...
--------------------------
# ./support-files/mysql.server start
start Starting MySQL.. OK!
注意:上面 basedir 以及datadir部分是手动改的,那么在命令中如何手动改,步骤:
1、 按 键盘 i 键(insert),然后就可以在你想要的位置编写了。
2、编辑完毕退出,esc键。
3、Shift键 加 :(冒号键)退出编辑。
4、wq! 保存编辑
7、创建软链接
# ln -s /home/mysql/bin/mysql /usr/bin/mysql
8、创建配置文件
下面的前3步操作过程中,可能会出错,如果有错就跳过,因为不同电脑的环境不一样,
1、将默认生成的my.cnf备份
# mv /etc/my.cnf /etc/my.cnf.bak
如果这一步报错,没有关系,正常情况下是由于/ect文件夹下没有my.cnf锁引起的,创建一下就可以了,或者你不会创建 , 直接使用 vim /ect/my.cnf 命令编辑 , 然后直接退出编辑就可以,在执行这个命令就不会出错了,退出编辑命令 esc键 -> (shift键+ :(冒号键) -> wq! ),然后在执行上面的命令就不会报错了。
2、进入mysql的安装目录支持文件目录
# cd /home/mysql/support-files
3、拷贝配置文件模板为新的mysql配置文件,
# cp my-default.cnf /etc/my.cnf
4、设置编码, 可按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行. 如下是我修改配置文件/etc/my.cnf, 设置编码为utf8以防乱码
# vim /etc/my.cnf
[mysqld]
basedir = /home/mysql
datadir = /home/mysql/data
character_set_server=utf8
init_connect='SET NAMES utf8'
[client]
default-character-set=utf8
有的时候使用这个命令后,前3步有问题的这里可能my.cnf文件是空的,这个不要紧,既然没有生成,那么你就自己编辑喽!具体编辑怎么实现就参照步骤6.
9、配置mysql服务开机自动启动
# cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld # 拷贝启动文件到/etc/init.d/下并重命令为mysqld
# chmod 755 /etc/init.d/mysqld # 增加执行权限
# chkconfig --list mysqld # 检查自启动项列表中没有mysqld这个,
# chkconfig --add mysqld # 如果没有就添加mysqld:
# chkconfig mysqld on # 用这个命令设置开机启动:
如果在执行第一步就报错,则说明刚刚在第8步中配置出问题了,那你就重新设置一下
做完这步后再重新执行下上面的操作。如果后面的操作中出现以下的问题
这样就是电脑环境的问题了,你就直接把
10、mysql服务的启动/重启/停止
# service mysqld start # 启动服务
# service mysqld restart # 重启服务
# service mysqld stop # 停止服务
11、初始化mysql用户root的密码
# cd /home/mysql
# ./bin/mysqladmin -u root -p '**mjT,#x_5sW' password '123456' 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.
mysql> use mysql
Database changed
mysql> update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.13, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 3
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 1 hour 29 min 17 sec
Threads: 1 Questions: 50 Slow queries: 0 Opens: 136 Flush tables: 1 Open tables: 129 Queries per second avg: 0.009
修改完数据库密码后,一定记得重启MySQL
服务,否则登录时提示密码错误
如果这一步完成不了的话,也就是修改不了密码的话,你也可以直接下面的方式先登录
root@s0:/home/mysql# ./bin/mysqladmin -u root -p '946//EE=v!Ne' password '123456' mysqladmin:
Enter password:
mysqladmin: connect to server at 'localhost' failed --不让你修改
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
root@s0:/home/mysql# find / -name mysql.sock
/tmp/mysql.sock
root@s0:/home/mysql# /home/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) --输入之前的密码
root@s0:/home/mysql# /home/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.13
Copyright (c) 2000, 2016, 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;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. --没有权限
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.04 sec)
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> update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> \s
但是如果不修改密码,不进每次登陆麻烦而且在执行一些操作会报一下错误
You must reset your password using ALTER USER statement before executing this statement # 意思就是你必须重置你的密码在执行这个execute操作前
这里教大家用另外一种方式修改密码,这个是在已经登陆进去的情况下依次执行。
mysql> SET PASSWORD = PASSWORD('123456'); # PASSWORD()里面的123456 是我设置的新密码,你也可以设置成你的密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
12. mysql远程授权
[root@testfornss ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.13 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
或者:
grant all on *.* to 'root'@'%' identified by '123456';
如果不设置远程连接
,用mysql数据库连接工具
可是连接不上
的!!!
14.用数据库连接工具来测试你刚刚的安装是不是成功了
首先要知道,远程连接的Linux系统的ip,确保本机
已经能够ping通
的情况下再连接。
注意:
如果防火墙没有关闭的话有时候也是连接不上的,关闭防火墙一定是要在mysql服务启动后在关闭,如果在mysql服务启动前关闭,系统会在mysql服务启动后自动开启防火墙,这样还是连接不上。