Centos 7 安装MySQL5.7.22

下载 MySQL Community Server 5.7.22

   MySQL Community Server 5.7.22 包:mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

    其他版本下载:  https://dev.mysql.com/downloads/

卸载mariadb或者mysql

# 删除mysql的rpm包
[root@master ~]# rpm --nodeps `rpm -qa | grep -i mysql`
# 删除mysql的相关文件和文件夹
[root@master ~]# rm -rf `find / -name mysql`

创建mysql组和用户

# 创建组
[root@master ~]# groupadd mysql
# 创建用户
[root@master ~]# useradd -g mysql mysql
# 设置密码(mysql)
[root@master ~]# passwd mysql
# 此提示可以忽略
BAD PASSWORD: The password is shorter than 8 characters

安装步骤

  • 将 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz 文件上传到linux,我上传到/opt目录


# 解压mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@master opt]# mkdir /usr/local/mysql
[root@master opt]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql/
# 复制完后 cd到mysql目录
[root@master opt]# cd /usr/local/mysql
[root@master mysql]# mkdir data
# 查看
[root@master mysql]# ll
total 36
drwxr-xr-x  2 root root   4096 May  8 09:05 bin
-rw-r--r--  1 7161 31415 17987 Mar  4 05:40 COPYING
drwxr-xr-x  2 root root      6 May  8 09:07 data
drwxr-xr-x  2 root root     55 May  8 09:05 docs
drwxr-xr-x  3 root root   4096 May  8 09:05 include
drwxr-xr-x  5 root root    229 May  8 09:05 lib
drwxr-xr-x  4 root root     30 May  8 09:05 man
-rw-r--r--  1 7161 31415  2478 Mar  4 05:40 README
drwxr-xr-x 28 root root   4096 May  8 09:05 share
drwxr-xr-x  2 root root     90 May  8 09:05 support-files
  • 开始安装
# cd到/usr/local/mysql/bin目录,执行如下命令
[root@master bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
# 当出现Version: '5.7.22'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)时,再打开一个linux终端继续操作

测试mysql能否启动

# cd到/usr/local/mysql/support-files/
[root@master ~]# cd /usr/local/mysql/support-files/
[root@master support-files]# ll
total 24
-rw-r--r-- 1 7161 31415   773 Mar  4 05:40 magic
-rwxr-xr-x 1 7161 31415  1061 Mar  4 06:02 mysqld_multi.server
-rwxr-xr-x 1 7161 31415   894 Mar  4 06:02 mysql-log-rotate
-rwxr-xr-x 1 7161 31415 10576 Mar  4 06:02 mysql.server
# 编辑mysql.server,修改basedir、datadir
[root@master support-files]# vim mysql.server
# 启动测试
[root@master support-files]# ./mysql.server start
Starting MySQL SUCCESS!
# 创建连接
[root@master support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin

mysqld配置

[root@master ~]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@master ~]# chmod 755 /etc/init.d/mysqld
[root@master ~]# chkconfig --add mysqld
[root@master ~]# chkconfig mysqld on
# 启动测试
[root@master ~]# systemctl stop mysqld
[root@master etc]# systemctl status mysqld
mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (exited) since Tue 2018-05-08 09:55:37 PDT; 2s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 7935 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)

May 08 09:55:36 master systemd[1]: Starting LSB: start and stop MySQL...
May 08 09:55:37 master mysqld[7935]: Starting MySQL SUCCESS!
May 08 09:55:37 master systemd[1]: Started LSB: start and stop MySQL.
May 08 09:55:37 master mysqld[7935]: 2018-05-08T16:55:37.106676Z mysqld_safe A mysqld process already exists

重置密码

# 1.修改配置文件my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
skip-grant-tables
# port = .....
# server_id = .....
# socket = .....
[client]
default-character-set=utf8


# 2.找到[mysqld]在之后添加 skip-grant-tables
# 3.重启mysql服务
[root@master ~]# systemctl restart mysqld
# 直接登录
[root@master ~]# mysql -u root -p
mysql> update mysql.user set authentication_string=password('123456') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出mysql,到my.cnf中把开始添加的skip-grant-tables注释掉或删掉
# 重启
[root@master ~]# systemctl restart mysqld
#登录
[root@master ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

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>

到此,mysql安装完成

若要外部连接登录MySQL执行:

mysql> update mysql.user set host = '%' where user = 'root';     

mysql> flush privileges;

mysql> select user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)


注意

  1. 配置文件中 basedir和datadir路径一定要写对
  2. 若出现ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement,以此执行以下命令(红色部分修改即可)
    mysql> set password = PASSWORD('your new password');
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    mysql> alter user 'root'@'localhost' PASSWORD EXPIRE NEVER;
    Query OK, 0 rows affected (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)


最后修改:2018/08/03 22:11
如果觉得我的文章对你有用,请随意赞赏

发表评论

颜文字