linux centos7 安装MySQL5.7

1:下载

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

 

2:解压

tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar 

3:再移动并重命名一下

mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql

4:创建mysql用户组和用户并修改权限

groupadd mysql
useradd -r -g mysql mysql

5:创建数据目录

mkdir -p  /data/mysql              #创建目录
chown mysql:mysql -R /data/mysql   #赋予权限

6:配置my.cnf

vim /etc/my.cnf

内容

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

7:初始化数据库

进入mysql的bin目录

cd /usr/local/mysql/bin/

初始化

./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize


 查看密码

cat /data/mysql/mysql.err

 

 

8:启动mysql,并更改root 密码
先将mysql.server放置到/etc/init.d/mysql中

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

启动!!!

service mysql start

如果以前安装过,没有卸载干净,会遇到一下问题

[root@VM-0-4-centos bin]# service mysql start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).

查看日志,端口在占用

 

 解决

[root@VM-0-4-centos ~]# netstat -apn  | grep 3306 
tcp6       0      0 :::3306                 :::*                    LISTEN      12519/mysqld        
[root@VM-0-4-centos ~]# kill -9 12519
[root@VM-0-4-centos ~]# ^C

重新启动,就OK

[root@VM-0-4-centos mysql]# service mysql start
Starting MySQL. SUCCESS! 
[root@VM-0-4-centos mysql]# 

首先登录mysql,密码是前面随机生成的。

[root@VM-0-4-centos mysql]# cd /usr/local/mysql/
[root@VM-0-4-centos mysql]# ./bin/my
myisamchk                   my_print_defaults           mysqlcheck                  mysqld                      mysqldump                   mysql_install_db            mysqlshow                   mysql_tzinfo_to_sql
myisam_ftdump               mysql                       mysql_client_test_embedded  mysqld-debug                mysqldumpslow               mysql_plugin                mysqlslap                   mysql_upgrade
myisamlog                   mysqladmin                  mysql_config                mysqld_multi                mysql_embedded              mysqlpump                   mysql_ssl_rsa_setup         mysqlxtest
myisampack                  mysqlbinlog                 mysql_config_editor         mysqld_safe                 mysqlimport                 mysql_secure_installation   mysqltest_embedded          
[root@VM-0-4-centos mysql]# ./bin/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.26

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> 
mysql> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
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)

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> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql>

如果不希望每次都到bin目录下使用mysql命令则可以创建软连接

ln -s  /usr/local/mysql/bin/mysql    /usr/bin

至此,完成!

 

posted on 2021-06-22 09:30  paike123  阅读(63)  评论(0编辑  收藏  举报

导航