一、系统环境
[root@localhost home]# cat /etc/RedHat-release

CentOS Linux release 7.2.1511 (Core)

二、mysql安装
安装mysql和mysql-devel都成功,但是安装mysql-server失败,如下:

  1. [root@localhost home]# yum install mysql-server 已加载插件:fastestmirror, langpacks
  2. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
  3. base | 3.6 kB 00:00:00
  4. extras | 3.4 kB 00:00:00
  5. updates | 3.4 kB 00:00:00
  6. (1/4): base/7/x86_64/group_gz | 155 kB 00:00:01
  7. (2/4): extras/7/x86_64/primary_db | 139 kB 00:00:01
  8. (3/4): base/7/x86_64/primary_db | 5.6 MB 00:00:38
  9. (4/4): updates/7/x86_64/primary_db | 4.7 MB 00:00:39
  10. Loading mirror speeds from cached hostfile
  11. base: mirrors.163.com
  12. extras: mirrors.163.com
  13. updates: mirrors.163.com
  14. 没有可用软件包 mysql-server
  15. 错误:无须任何处理
  16. 出现这样问题是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了

有两种解决办法:

1、方法一:安装mariadb

MariaDB数据库管理系统是MySQL的一个分支,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。MariaDB由MySQL的创始人Michael Widenius(英语:Michael Widenius)主导开发,他早前将自己创建的公司MySQL AB卖给了SUN,此后,随着SUN被甲骨文收购,MySQL的所有权也落入Oracle的手中。MariaDB名称来自Michael Widenius的女儿Maria的名字。

安装mariadb,输入安装命令

[root@localhost home]# yum install mariadb-server mariadb

mariadb数据库的相关命令是:

systemctl start mariadb #启动MariaDB

systemctl stop mariadb #停止MariaDB

systemctl restart mariadb #重启MariaDB

systemctl enable mariadb #设置开机启动

先启动数据库

[root@yl-web yl]# systemctl start mariadb

默认无密码

[root@localhost lzh]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> show databases;
+——————————+
| Database |
+——————————+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————————+
4 rows in set (0.01 sec)

2、方法二:官网下载安装mysql-server

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm —nodeps —force

yum install mysql-community-server

安装成功后重启mysql服务

  1. # service mysqld restart

初次安装mysql,root账户没有密码

  1. [root@localhost lzh]# mysql -u root
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 3
  4. Server version: 5.6.26 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show databases;
  11. +--------------------+
  12. | Database |
  13. +--------------------+
  14. | information_schema |
  15. | mysql |
  16. | performance_schema |
  17. | test |
  18. +--------------------+
  19. 4 rows in set (0.01 sec)

设置密码

mysql> set password for ‘root’@’localhost’ =password(‘password’);
Query OK, 0 rows affected (0.00 sec)
安装完以后mariadb自动就被替换了,将不再生效。

[root@localhost lzh]# rpm -qa |grep mariadb

开启远程访问

https://www.cnblogs.com/wilwei/p/10244774.html

  1. #登陆mysql
  2. $ mysql -uroot -p
  3. mysql> use mysql;
  4. mysql> update user set host = '%' where user = 'root';
  5. mysql> select host, user from user;
  6. +-----------+------------------+
  7. | host | user |
  8. +-----------+------------------+
  9. | % | root |
  10. | localhost | debian-sys-maint |
  11. | localhost | mysql.session |
  12. | localhost | mysql.sys |
  13. +-----------+------------------+
  14. 4 rows in set (0.00 sec)
  15. #ok 退出MySQL 重启服务
  16. mysql> quit;
  17. $ service mysql restart
  18. #发现客户端远程还是连接不上 继续修改mysql.cnf配置文件
  19. #需要root权限,配置文件是只读的
  20. $ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
  21. #往下翻,注释掉这一行,保存退出
  22. # bind-address = 127.0.0.1
  23. #重启服务
  24. $ service mysql restart;
  25. #ok 可以了
  26. 或者
  27. #允许所有用户远程访问 修改用户名和密码为你自己的
  28. mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
  29. #允许单个ip 修改用户名和密码为你自己的
  30. mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
  31. #最后
  32. mysql> FLUSH PRIVILEGES;

参考