centos7.x安装mysql
如果系统是CentOS7.x,则自带的数据库是Mariadb,是Mysql的一个分支,虽然使用上完全兼容,但自从甲骨文收购Mysql后还没有取消开源,那就继续用Mysql吧。
先卸载Mariadb:
[gdcni@server202 ~]$ sudo yum remove mariadb* -y |
下载Mysql的yum源:
[gdcni@server202 ~]$ sudo wget -P /home/gdcni http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm |
安装Mysql的yum源:
[gdcni@server202 ~]$ cd /home/gdcni/ [gdcni@server202 ~]$ sudo rpm -ivh mysql57-community-release-el7-11.noarch.rpm |
查看Mysql版本:
[gdcni@server202 ~]$ yum repolist all | grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community 禁用 mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 禁用 mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community 禁用 mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 禁用 mysql-connectors-community/x86_64 MySQL Connectors Community 启用: 83 mysql-connectors-community-source MySQL Connectors Community - Sou 禁用 mysql-tools-community/x86_64 MySQL Tools Community 启用: 79 mysql-tools-community-source MySQL Tools Community - Source 禁用 mysql-tools-preview/x86_64 MySQL Tools Preview 禁用 mysql-tools-preview-source MySQL Tools Preview - Source 禁用 mysql55-community/x86_64 MySQL 5.5 Community Server 禁用 mysql55-community-source MySQL 5.5 Community Server - Sou 禁用 mysql56-community/x86_64 MySQL 5.6 Community Server 禁用 mysql56-community-source MySQL 5.6 Community Server - Sou 禁用 mysql57-community/x86_64 MySQL 5.7 Community Server 启用: 327 mysql57-community-source MySQL 5.7 Community Server - Sou 禁用 mysql80-community/x86_64 MySQL 8.0 Community Server 禁用 mysql80-community-source MySQL 8.0 Community Server - Sou 禁用 |
可以看到57版本是默认启用的。当然,也可以通过如下命令启用或禁用版本:
[gdcni@server202 ~]$ sudo yum-config-manager --enable mysql57-community [gdcni@server202 ~]$ sudo yum-config-manager --disable mysql56-community |
注意,任何时候只能启动一个版本。
好,现在可以安装并启动Mysql了:
[gdcni@server202 ~]$ sudo yum install mysql-community-server [gdcni@server202 ~]$ sudo systemctl start mysqld [gdcni@server202 ~]$ sudo systemctl enable mysqld [gdcni@server202 ~]$ mysql --version mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper |
刚安装完成后首先需要修改root密码,虽然有多种修改方法,但可能都会遇到如下报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
修改方法为:
1) 先关闭Mysql密码认证,vim /etc/my.cnf,在[mysqld]节点下任意位置添加skip-grant-tables:
[mysqld] skip-grant-tables |
2) 然后重启Mysql:
[gdcni@server202 ~]$ sudo systemctl restart mysqld |
3) 现在登录Mysql修改一下密码:
[gdcni@server202 ~]$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 MySQL Community Server (GPL)
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> 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> update user set authentication_string=password('gdcni') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 |
4) 最后重新打开配置vim /etc/my.cnf,去掉前面加的skip-grant-tables,并重启mysqld即可。现在登录就要这样了:
[gdcni@server202 ~]$ mysql -uroot -pgdcni |