Centos7 安装MariaDB(mysql)数据库
卸载MariaDB 原有的(如果没安装可忽略)
yum remove mariadb-server -y
一、使用yum安装
安装数据库客户端和服务端
yum -y install mariadb mariadb-server
二、数据库初始化配置
启动服务
systemctl start mariadb
设置自动启动
systemctl enable mariadb
初始化数据库
mysql_secure_installation
Enter current password for root (enter for none): =======> '输入数据库root密码 第一次初始化不用输入'
OK, successfully used password, moving on...
Set root password? [Y/n] y =======> '是否设置root密码'
New password: =======> '设置root密码'
Re-enter new password: =======> '设置root密码'
Password updated successfully!
Reloading privilege tables..
Remove anonymous users? [Y/n] y =======> '是否移除匿名用户'
... Success!
Disallow root login remotely? [Y/n] y =======> '拒绝root远程登录'
... Success!
Remove test database and access to it? [Y/n] y =======> '删除temp数据库'
- Dropping test database...
... Success! - Removing privileges on test database...
... Success!
Reload privilege tables now? [Y/n] y =======> '重载权限'
... Success!
设置字符集为utf-8
注意:当没有对应的文件直接编辑创建即可
/etc/my.cnf 文件,在[mysqld] 标签下添加
vim /etc/my.cnf
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
/etc/my.cnf.d/client.cnf 文件,在 [client] 标签下添加
vim /etc/my.cnf.d/client.cnf
[client]
default-character-set=utf8
/etc/my.cnf.d/mysql-clients.cnf 文件,在 [mysql] 标签下添加
vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set=utf8
重启服务
systemctl restart mariadb
登录mariadb查看字符集 (先进入数据库在执行)
show variables like "%character%";show variables like "%collation%";
先查看mysql数据库中的user表
MariaDB [(none)]> 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
MariaDB [mysql]> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)
MariaDB [mysql]>
将host为localhost数据改为%
MariaDB [mysql]> update user set host='%' where host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
+-----------+------+
3 rows in set (0.00 sec)
MariaDB [mysql]>
刷新权限表
flush privileges;
使用navicat 软件远程连接mariadb(选择MySQL也行)
三、设置root远程登录
本机登录数据库
mysql -u root -p
选择数据库
USE mysql;
更新字段允许任意host登录
UPDATE user SET host="%" WHERE user="root";
刷新权限
FLUSH PRIVILEGES;
退出
EXIT
四、防火墙配置
启动防火墙服务
systemctl start firewalld.service
修改防火墙房型3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重载配置
firewall-cmd --reload
查看配置
firewall-cmd --list-all
开启自动启动防火墙
systemctl enable firewalld.service
五、创建数据库、用户
登录
mysql -u root -p
创建数据库
CREATE DATABASE 数据库名 CHARACTER SET utf8mb4;
创建用户
CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
授权用户管理数据库
GRANT ALL PRIVILEGES ON 数据库名.* TO '用户名'@'%';
数据刷新
FLUSH PRIVILEGES;
退出
EXIT;