centos7安装mysql

参考:https://blog.csdn.net/nuc2015/article/details/80938560

centos7+mysql5.7安装教程

#卸载系统自带的Mariadb

 
  1. [root@master ~]# rpm -qa|grep mariadb

  2. mariadb-libs-5.5.44-2.el7.centos.x86_64

  3. [root@master ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

#检查mysql是否存在

 
  1. [root@master ~]# rpm -qa | grep mysql

  2. [root@master ~]#

#下载MySQL压缩包

点击打开链接


#解压并并改名为mysql,在此文件夹下新建data目录

 
  1. [root@master apps]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

  2. [root@master apps]# mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql

  3. [root@master apps]# cd mysql

  4. [root@master apps]# mkdir data

#在etc下新建配置文件my.cnf,并在该文件内添加以下配置

[root@master ~]#vi /etc/my.cnf
 
  1. [mysql]

  2. #设置mysql客户端默认字符集

  3. default-character-set=utf8

  4. [mysqld]

  5. skip-name-resolve

  6. #设置3306端口

  7. port = 3306

  8. # 设置mysql的安装目录

  9. basedir=/apps/mysql/

  10. # 设置mysql数据库的数据的存放目录

  11. datadir=/apps/mysql/data

  12. # 允许最大连接数

  13. max_connections=200

  14. # 服务端使用的字符集默认为8比特编码的latin1字符集

  15. character-set-server=utf8

  16. # 创建新表时将使用的默认存储引擎

  17. default-storage-engine=INNODB

  18. lower_case_table_names=1

  19. max_allowed_packet=16M

  20. log-error = /apps/mysql/data/error.log

  21. pid-file = /apps/mysql/data/mysql.pid

  22. user = root

  23. tmpdir = /tmp

  24. socket = /tmp/mysql.sock

 

#安装和初始化

 
  1. [root@master ~]cd /apps/mysql

  2. [root@master mysql]bin/mysql_install_db --user=root --basedir=/apps/mysql --datadir=/apps/mysql/data

  3. [root@master mysql]cp ./support-files/mysql.server /etc/init.d/mysqld

  4. [root@master mysql]chown 777 /etc/my.cnf

  5. [root@master mysql]chmod +x /etc/init.d/mysqld

#启动服务

 
  1. [root@master mysql]# /etc/init.d/mysqld start

  2. Starting MySQL. SUCCESS!

  3.  
  4. #查看端口运行情况(3306)

  5. [root@master mysql]# netstat -ntlp

  6. Active Internet connections (only servers)

  7. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

  8. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd

  9. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1326/dnsmasq

  10. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 983/sshd

  11. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 986/cupsd

  12. tcp6 0 0 :::111 :::* LISTEN 1/systemd

  13. tcp6 0 0 :::22 :::* LISTEN 983/sshd

  14. tcp6 0 0 ::1:631 :::* LISTEN 986/cupsd

  15. tcp6 0 0 :::3306 :::* LISTEN 16421/mysqld

 

#登录MySQL

 
  1. #查看生成的初始密码

  2. [root@master mysql]# cat /root/.mysql_secret

  3. # Password set for user 'root@localhost' at 2018-07-06 10:36:35

  4. #Lws4rXsnhbA

  5.  
  6. #密码登录(可能会报错)

  7. [root@master mysql]# mysql -u root -p

  8. Enter password:

  9. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

  10.  
  11. #如果登录成功,显示成这样

  12. [root@master ~]# mysql -u root -p

  13. Enter password:

  14. Welcome to the MySQL monitor. Commands end with ; or \g.

  15. Your MySQL connection id is 5

  16. Server version: 5.7.22 MySQL Community Server (GPL)

  17.  
  18. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

  19.  
  20. Oracle is a registered trademark of Oracle Corporation and/or its

  21. affiliates. Other names may be trademarks of their respective

  22. owners.

  23.  
  24. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  25.  
  26. mysql>

============================================================================

#安装过程中可能出现的错误

错误1:解决严格按照下面配置进行(/tmp/mysql.sock)位置至关重要

ERROR! The server quit without updating PID file
 
  1. [root@master mysql]# /etc/init.d/mysqld start

  2. Starting MySQL.Logging to '/apps/mysql/data/master.hadoop.err'.

  3. ERROR! The server quit without updating PID file (/apps/mysql/data/master.hadoop.pid)

 
  1. [mysql]

  2. # 设置mysql客户端默认字符集

  3. default-character-set=utf8

  4. [mysqld]

  5. skip-name-resolve

  6. #设置3306端口

  7. port = 3306

  8. # 设置mysql的安装目录

  9. basedir=/apps/mysql/

  10. # 设置mysql数据库的数据的存放目录

  11. datadir=/apps/mysql/data

  12. # 允许最大连接数

  13. max_connections=200

  14. # 服务端使用的字符集默认为8比特编码的latin1字符集

  15. character-set-server=utf8

  16. # 创建新表时将使用的默认存储引擎

  17. default-storage-engine=INNODB

  18. lower_case_table_names=1

  19. max_allowed_packet=16M

  20. log-error = /apps/mysql/data/error.log

  21. pid-file = /apps/mysql/data/mysql.pid

  22. user = root

  23. tmpdir = /tmp

  24. 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!)

 
  1. [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!

  3. mysql_install_db: [ERROR] Fatal error in defaults handling. Program aborted!

[root@master mysql]# vi /etc/my.cnf(该文件有语法错误,修改)

============================================================================

#如果再登录中出现了问题

 
  1. #查看生成的初始密码

  2. [root@master mysql]# cat /root/.mysql_secret

  3. # Password set for user 'root@localhost' at 2018-07-06 10:36:35

  4. #Lws4rXsnhbA

  5.  
  6. #密码登录(可能会报错)

  7. [root@master mysql]# mysql -u root -p

  8. Enter password:

  9. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

 

解决办法:

 

 
  1. [root@master mysql]# service mysqld stop

  2. Shutting down MySQL.. SUCCESS!

  3.  
  4. #报错解决方法

  5. [root@master mysql]# vi /etc/my.cnf

  6. 在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi。

  7.  
  8. [root@master mysql]# service mysqld start

  9. Starting MySQL. SUCCESS!

  10. [root@master mysql]# mysql

  11. Welcome to the MySQL monitor. Commands end with ; or \g.

  12. Your MySQL connection id is 2

  13. Server version: 5.7.22 MySQL Community Server (GPL)

  14.  
  15. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

  16.  
  17. Oracle is a registered trademark of Oracle Corporation and/or its

  18. affiliates. Other names may be trademarks of their respective

  19. owners.

  20.  
  21. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  22.  
  23. mysql>

  24.  
  25. 登录并修改MySQL的root密码

  26. mysql> USE mysql ;

  27. mysql> update user set password=password("*******") where user="*******"; #修改密码报错

  28. ERROR 1054 (42S22): Unknown column 'password' in 'field list'

  29. mysql> update mysql.user set authentication_string=password('*******') where user='*******'; #修改密码成功

  30. Query OK, 1 row affected, 1 warning (0.00 sec)

  31. Rows matched: 1 Changed: 1 Warnings: 1

  32. mysql> flush privileges ;

  33. mysql> exit

  34.  
  35. # vi /etc/my.cnf

  36. 将刚才在[mysqld]的段中加上的skip-grant-tables删除

  37. 保存并且退出vi。

  38.  
  39. 重新启动mysqld

 

登陆后重要的一步

 

否则报错:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决办法:

 

 
  1. 安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个

  2. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

  3.  
  4. step 1: SET PASSWORD = PASSWORD('your new password');

  5.  
  6. step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

  7.  
  8. step 3: flush privileges;

  9.  
  10. 完成以上三步退出再登,使用新设置的密码就行了,后两部原样设置即可

  11.  
  12. mysql> SET PASSWORD = PASSWORD('******');

  13. Query OK, 0 rows affected, 1 warning (0.00 sec)

  14.  
  15. mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

  16. Query OK, 0 rows affected (0.00 sec)

  17.  
  18. mysql> flush privileges;

  19. Query OK, 0 rows affected (0.01 sec)

  20.  
  21. mysql> use mysql

  22. Reading table information for completion of table and column names

  23. You can turn off this feature to get a quicker startup with -A

  24.  
  25. Database changed

  26. mysql> show databases

  27. -> ;

  28. +--------------------+

  29. | Database |

  30. +--------------------+

  31. | information_schema |

  32. | mysql |

  33. | performance_schema |

  34. | sys |

  35. +--------------------+

  36. 4 rows in set (0.00 sec)

  37.  
  38. mysql> exit

  39. Bye

  40.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值