1、下载tar包,这里使用wget从官网下载
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
或者官网 https://dev.mysql.com/downloads/mysql/5.7.html#downloads
直接执行wget命令较为快速简洁,推荐
2.检查库文件是否存在,如有删除。
[root@localhost Desktop]$ rpm -qa | grep mysql
mysql-libs-5.1.52-1.el6_0.1.x86_64
[root@localhost ~]# rpm -e mysql-libs-5.1.52.x86_64 --nodeps
3.检查mysql组和用户是否存在,如无创建。
[root@localhost ~]# cat /etc/group | grep mysql
mysql: x :490:
[root@localhost ~]# cat /etc/passwd | grep mysql
mysql: x :496:490::/home/mysql:/bin/bash
以上为默认存在的情况,如无,执行添加命令:
mysql用户组
[root@localhost ~]#groupadd mysql
mysql用户
[root@localhost ~]#useradd -r -g mysql mysql
//useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
4、将mysql安装到/usr/local/mysql
解压
tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
移动
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/
重命名
mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
5、新建data目录
mkdir /usr/local/mysql/data
6、将/usr/local/mysql的所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql
7、配置
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data (过时)
如果出现以下错误:
2018-07-14 06:40:32 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-14 06:40:32 [ERROR] Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
2018-07-14 06:40:32 [ERROR] Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql
-- server log begin --
-- server log end --
则使用以下命令:
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
如果出现以下错误:
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
则执行以下命令:
yum install libaio
完成后继续安装:
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
会获得一个临时密码,保存:
2019-08-20T03:46:17.272795Z 0 [Warning] TIMESTAMP with implicit
DEFAULT value is deprecated. Please use
–explicit_defaults_for_timestamp server option (see documentation for more details). 2019-08-20T03:46:18.300040Z 0 [Warning] InnoDB: New log
files created, LSN=45790 2019-08-20T03:46:18.436267Z 0 [Warning]
InnoDB: Creating foreign key constraint system tables.
2019-08-20T03:46:18.501483Z 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:
115825dc-c2fd-11e9-9e3f-00163e0216e0. 2019-08-20T03:46:18.504893Z 0
[Warning] Gtid table is not ready to be used. Table
‘mysql.gtid_executed’ cannot be opened. 2019-08-20T03:46:18.505423Z 1
[Note] A temporary password is generated for root@localhost:
YIYP1UElsa!s
编辑/etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
8、开启服务
将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
开机自启
chkconfig mysql on
开启
service mysql start
9、设置密码
登录(由于/etc/my.cnf中设置了取消密码验证,所以此处密码任意)
/usr/local/mysql/bin/mysql -u root -p
操作mysql数据库
>>use mysql;
修改密码
>>update user set authentication_string=password('你的密码') where user='root';
>>flush privileges;
>>exit;
10、将/etc/my.cnf中的skip-grant-tables删除
11、登录再次设置密码(不再次设置密码就操作不了数据库,password_expired值为Y)
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
/usr/local/mysql/bin/mysql -u root -p
Enter password:
>>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';
12、允许远程连接
>>use mysql;
>>update user set host='%' where user = 'root';
>>flush privileges;
>>eixt;
13、添加快捷方式
ln -s /usr/local/mysql/bin/mysql /usr/bin
14、mysql其他操作
停止mysql服务
1、使用 service 启动:service mysql stop
2、使用 mysqld 脚本启动:/etc/inint.d/mysql stop
3、mysqladmin shutdown
重启mysql服务
1、使用 service 启动:service mysql restart
2、使用 mysqld 脚本启动:/etc/inint.d/mysql restart
启动mysql服务
1、使用 service 启动:service mysql start
2、使用 mysqld 脚本启动:/etc/inint.d/mysql start
3、使用 safe_mysqld 启动:safe_mysql&
查看mysql状态
>>mysql