Centos 7服务器搭建(二)—安装MySQL

来源:互联网 发布:门面模式 java 编辑:程序博客网 时间:2024/06/09 19:19

在Centos6.*下,安装mysql可以直接用yum实现:

yum install mysql mysql-server mysql-libs mysql-server

但在Centos 7下,安装mysql-server会失败,因为甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此开源社区采用分支的方式来避开这个风险,维护MySQL的分支MariaDB,同样采用GPL授权许可。MariaDB 团队正尽力保持对 MySQL 的全面兼容,因此完全可以从mysql迁移到MariaDB,甚至不用改任何东西,只是把安装的数据库软件换掉。
(1)安装
依然用yum来安装

yum install mariadb-server mariadb 

(2)启动

systemctl start mariadb

其它响应命令是:

systemctl start mariadb #启动MariaDB

systemctl stop mariadb #停止MariaDB

systemctl restart mariadb #重启MariaDB

systemctl enable mariadb #设置开机启动

(3)使用
使用方法和MySQL一样,甚至可以直接用MySQL的命令

[root@****]mysqlWelcome to the MariaDB monitor.  Commands end with ; or \g.………………………………

(4)配置密码
进入MySQL后,

mysql> set password for 'root'@'localhost' =password('password');Query OK, 0 rows affected (0.00 sec)mysql>

直接生效,不需要重启数据库。

(5)其它配置

  • 编码
    mysql配置文件为/etc/my.cnf,只需要在其中加上编码配置即可
[mysql]default-character-set =utf8

需要注意的是这里的编码需要和/usr/share/mysql/charsets/Index.xml中的一样

  • 远程连接
    赋予root用户所有权限
mysql> grant all privileges on *.* to root@'%'identified by 'password';mysql> grant all privileges on *.* to root@localhost identified by 'password';mysql> grant all privileges on *.* to root@192.169.1.* identified by 'password';

如果是新用户而不是root,则要先新建用户,

mysql>create user 'username'@'%' identified by 'password';

之后记得要刷新权限:

flush privileges;

(6)开放firewalld端口
如果需要加入远程访问,需要开放对应的端口,默认是3306

firewall-cmd --list-all(查看规则)firewall-cmd --add-port=3306/tcp --permanentfirewall-cmd --reload
0 0