参考:https://blog.csdn.net/nuc2015/article/details/80938560
centos7+mysql5.7安装教程
#卸载系统自带的Mariadb
-
[root@master ~]# rpm -qa|grep mariadb
-
mariadb-libs-5.5.44-2.el7.centos.x86_64
-
[root@master ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
#检查mysql是否存在
-
[root@master ~]# rpm -qa | grep mysql
-
[root@master ~]#
#下载MySQL压缩包
#解压并并改名为mysql,在此文件夹下新建data目录
-
[root@master apps]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
-
[root@master apps]# mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
-
[root@master apps]# cd mysql
-
[root@master apps]# mkdir data
#在etc下新建配置文件my.cnf,并在该文件内添加以下配置
[root@master ~]#vi /etc/my.cnf
-
[mysql]
-
#设置mysql客户端默认字符集
-
default-character-set=utf8
-
[mysqld]
-
skip-name-resolve
-
#设置3306端口
-
port = 3306
-
# 设置mysql的安装目录
-
basedir=/apps/mysql/
-
# 设置mysql数据库的数据的存放目录
-
datadir=/apps/mysql/data
-
# 允许最大连接数
-
max_connections=200
-
# 服务端使用的字符集默认为8比特编码的latin1字符集
-
character-set-server=utf8
-
# 创建新表时将使用的默认存储引擎
-
default-storage-engine=INNODB
-
lower_case_table_names=1
-
max_allowed_packet=16M
-
log-error = /apps/mysql/data/error.log
-
pid-file = /apps/mysql/data/mysql.pid
-
user = root
-
tmpdir = /tmp
-
socket = /tmp/mysql.sock
#安装和初始化
-
[root@master ~]cd /apps/mysql
-
[root@master mysql]bin/mysql_install_db --user=root --basedir=/apps/mysql --datadir=/apps/mysql/data
-
[root@master mysql]cp ./support-files/mysql.server /etc/init.d/mysqld
-
[root@master mysql]chown 777 /etc/my.cnf
-
[root@master mysql]chmod +x /etc/init.d/mysqld
#启动服务
-
[root@master mysql]# /etc/init.d/mysqld start
-
Starting MySQL. SUCCESS!
-
#查看端口运行情况(3306)
-
[root@master mysql]# netstat -ntlp
-
Active Internet connections (only servers)
-
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
-
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
-
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1326/dnsmasq
-
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 983/sshd
-
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 986/cupsd
-
tcp6 0 0 :::111 :::* LISTEN 1/systemd
-
tcp6 0 0 :::22 :::* LISTEN 983/sshd
-
tcp6 0 0 ::1:631 :::* LISTEN 986/cupsd
-
tcp6 0 0 :::3306 :::* LISTEN 16421/mysqld
#登录MySQL
-
#查看生成的初始密码
-
[root@master mysql]# cat /root/.mysql_secret
-
# Password set for user 'root@localhost' at 2018-07-06 10:36:35
-
#Lws4rXsnhbA
-
#密码登录(可能会报错)
-
[root@master mysql]# mysql -u root -p
-
Enter password:
-
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
-
#如果登录成功,显示成这样
-
[root@master ~]# mysql -u root -p
-
Enter password:
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 5
-
Server version: 5.7.22 MySQL Community Server (GPL)
-
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>
============================================================================
#安装过程中可能出现的错误
错误1:解决严格按照下面配置进行(/tmp/mysql.sock)位置至关重要
ERROR! The server quit without updating PID file
-
[root@master mysql]# /etc/init.d/mysqld start
-
Starting MySQL.Logging to '/apps/mysql/data/master.hadoop.err'.
-
ERROR! The server quit without updating PID file (/apps/mysql/data/master.hadoop.pid)
-
[mysql]
-
# 设置mysql客户端默认字符集
-
default-character-set=utf8
-
[mysqld]
-
skip-name-resolve
-
#设置3306端口
-
port = 3306
-
# 设置mysql的安装目录
-
basedir=/apps/mysql/
-
# 设置mysql数据库的数据的存放目录
-
datadir=/apps/mysql/data
-
# 允许最大连接数
-
max_connections=200
-
# 服务端使用的字符集默认为8比特编码的latin1字符集
-
character-set-server=utf8
-
# 创建新表时将使用的默认存储引擎
-
default-storage-engine=INNODB
-
lower_case_table_names=1
-
max_allowed_packet=16M
-
log-error = /apps/mysql/data/error.log
-
pid-file = /apps/mysql/data/mysql.pid
-
user = root
-
tmpdir = /tmp
-
socket = /tmp/mysql.sock
然后:删除data目录中的所有文件,重新初始化
[root@master mysql]# rm -rf data/*
[root@master mysql]# bin/mysql_install_db --user=mysql --basedir=/apps/mysql/ --datadir=/apps/mysql/data/
错误2:(mysql_install_db: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1!)
-
[root@master mysql]# bin/mysql_install_db --user=mysql --basedir=/apps/mysql/ --datadir=/apps/mysql/data/
-
mysql_install_db: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1!
-
mysql_install_db: [ERROR] Fatal error in defaults handling. Program aborted!
[root@master mysql]# vi /etc/my.cnf(该文件有语法错误,修改)
============================================================================
#如果再登录中出现了问题
-
#查看生成的初始密码
-
[root@master mysql]# cat /root/.mysql_secret
-
# Password set for user 'root@localhost' at 2018-07-06 10:36:35
-
#Lws4rXsnhbA
-
#密码登录(可能会报错)
-
[root@master mysql]# mysql -u root -p
-
Enter password:
-
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
解决办法:
-
[root@master mysql]# service mysqld stop
-
Shutting down MySQL.. SUCCESS!
-
#报错解决方法
-
[root@master mysql]# vi /etc/my.cnf
-
在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi。
-
[root@master mysql]# service mysqld start
-
Starting MySQL. SUCCESS!
-
[root@master mysql]# mysql
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.7.22 MySQL Community Server (GPL)
-
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的root密码
-
mysql> USE mysql ;
-
mysql> update user set password=password("*******") where user="*******"; #修改密码报错
-
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
-
mysql> update mysql.user set authentication_string=password('*******') where user='*******'; #修改密码成功
-
Query OK, 1 row affected, 1 warning (0.00 sec)
-
Rows matched: 1 Changed: 1 Warnings: 1
-
mysql> flush privileges ;
-
mysql> exit
-
# vi /etc/my.cnf
-
将刚才在[mysqld]的段中加上的skip-grant-tables删除
-
保存并且退出vi。
-
重新启动mysqld
登陆后重要的一步
否则报错:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决办法:
-
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个
-
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
-
step 1: SET PASSWORD = PASSWORD('your new password');
-
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
-
step 3: flush privileges;
-
完成以上三步退出再登,使用新设置的密码就行了,后两部原样设置即可
-
mysql> SET PASSWORD = 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.01 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> show databases
-
-> ;
-
+--------------------+
-
| Database |
-
+--------------------+
-
| information_schema |
-
| mysql |
-
| performance_schema |
-
| sys |
-
+--------------------+
-
4 rows in set (0.00 sec)
-
mysql> exit
-
Bye