安装

安装和启动

这里使用yum方式安装:

yum -y install mysql MariaDB-server MariaDB-client 

安装完毕后启动:

systemctl restart mariadb # 使用start也可以

设置配置

输入 mysql_secure_installation 命令进入配置。

step1:
Enter current password for root (enter for none):<–初次运行直接回车 

step2:
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

others:
Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车
Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,
Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车
Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

细心的你一定发现了:
1、 Y/n,Y是大写的,表示默认选项。
2、只有new password那两行需要配置,其他的都可以用默认。

然后本机连接:

mysql -uroot -p1234 

本机可以了,但是发现从远程还是不行。

配置远程登录

# 查看用户信息
select User, host from mysql.user;

# 授权 任意主机的root用户('root'@'%') 可以访问 任意库的任意表(*.*) 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1234' WITH GRANT OPTION;

# 刷新权限
flush privileges;

# 扩展知识(非必须):如果只授权给某个ip可以访问,用这个语句
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.100.%' IDENTIFIED BY 'my-new-password' WITH GRANT OPTION;

发现远程可以连接了。

卸载

停止服务:

systemctl stop mariadb

删除配置文件:

rm -f /etc/my.cnf

删除数据目录(慎重,一定要弄清是不是这个目录):

rm -rf /var/lib/mysql/*