mysql 基础操作

MariaDB安装配置

简介

MariaDB 是mysql的开源免费分支,命令兼容

环境

centos7.9

安装 启动 开机启动

yum install mariadb-server
systemctl start mariadb 
systemctl enable mariadb 

查看启动结果

ps -aux | grep mysqld

/bin/sh /usr/bin/mysqld_safe --basedir=/usr
/usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

首次安装需要进行数据库的配置

命令都和mysql的一样

mysql_secure_installation

Enter current password for root (enter for none): # 输入数据库超级管理员root的密码(注意不是系统root的密码),第一次进入还没有设置密码则直接回车
Set root password? [Y/n] # 设置密码,y
New password: # 新密码
Re-enter new password: # 再次输入密码
Remove anonymous users? [Y/n] # 移除匿名用户, y
Disallow root login remotely? [Y/n] # 拒绝root远程登录,n,不管y/n,都会拒绝root远程登录
Remove test database and access to it? [Y/n] # 删除test数据库,y:删除。n:不删除,数据库中会有一个test数据库,一般不需要
Reload privilege tables now? [Y/n] # 重新加载权限表,y。或者重启服务

测试是否能够登录成功

mysql -u root -p

Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.68-MariaDB MariaDB Server

Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> \q

设置MariaDB字符集为utf-8

vim /etc/my.cnf

在 [mysqld] 标签下添加

init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

vim /etc/my.cnf.d/client.cnf
在 [client] 标签下添加
default-character-set=utf8

vim /etc/my.cnf.d/mysql-clients.cnf
在 [mysql] 标签下添加
default-character-set=utf8

#重启服务
systemctl restart mariadb

防火墙

默认防火是关闭的
如果是开启的 打开3306端口放行

firewall-cmd --query-port=3306/tcp  # 查看3306端口是否开启
firewall-cmd --zone=public --add-port=3306/tcp --permanent  # 开启3306端口
firewall-cmd --reload  # 重启防火墙
firewall-cmd --query-port=3306/tcp  # 查看3306端口是否开启

配置数据库能远程登陆

修改前

MariaDB [mysql]> use mysql
MariaDB [mysql]> select host,user from user;
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1       | root |
| localhost | root |
+-----------+------+

修改后 可以远程登陆

 MariaDB [mysql]> update user set host='%' where host='localhost';
 MariaDB [mysql]> select host,user from user;
+-----------+------+
| host      | user |
+-----------+------+
| %         | root |
| 127.0.0.1 | root |
| ::1       | root |
+-----------+------+
MariaDB [mysql]> flush privileges;

创建或载入备份数据库

法1

[root@]# mysql -u root -p
MariaDB [none]> create database worepress;
MariaDB [none]> use wordpress;
MariaDB [none]> source /root/ww.sql;

法2

[root@]# mysql -u root -p wordpress < /root/ww.sql;

备份数据库

[root@]# mysqldump -u root -p wordpress > /root/ww.sql;

数据库常用操作

MariaDB自身的一些配置存储在数据库 mysql 中

登陆

[root@]# mysql -u root -p
MariaDB [(none)]>

创建删除数据库

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>drop database wordpress;


创建用户

MariaDB [(none)]> use mysql;
MariaDB [(mysql)]> create user 'username'@'%' identified by '123456';
MariaDB [(mysql)]> select host,user from user;
+-----------+----------+
| host      | user     |
+-----------+----------+
| %         | root     |
| %         | username |
| 127.0.0.1 | root     |
| ::1       | root     |
+-----------+----------+

username:新增用户名

%:任何IP都可以访问

123456:密码

给用户授权

MariaDB [(mysql)]> grant select,insert,update,delete,create,index,alter on *.* to username;
MariaDB [(mysql)]> flush privileges;
MariaDB [(mysql)]> update user set host='localhost' where host='%' and user='root';
MariaDB [(mysql)]> flush privileges;
MariaDB [(mysql)]> select host,user from user;
+-----------+----------+
| host      | user     |
+-----------+----------+
| %         | username |
| 127.0.0.1 | root     |
| ::1       | root     |
| localhost | root     |
+-----------+----------+

查看用户权限

当前用户

MariaDB [(mysql)]> show grants;

其他用户

MariaDB [(mysql)]> show grants for 'username'@'%';

修改用户密码

1.知道root密码,修改任意用户密码

MariaDB [(mysql)]>update user set password=password('123456') where user='username';

2.忘记root密码

先停止服务

[root@]# systemctl stop mariadb

使用跳过授权的方式启动 mariadb,之后mysql就可以跳过登陆,然后按照上面的办法修改密码

[root@]# mysqld_safe --skip-grant-tables &
[1] 13109
[root@]# 211223 14:14:56 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
211223 14:14:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@]# ps -aux | grep 13109
root     13109  0.0  0.0 113420  1616 pts/1    S    14:14   0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
root     13575  0.0  0.0 112812   976 pts/1    R+   14:16   0:00 grep --color=auto 13109
[root@] mysql
[root@] reboot

用户重命名

[root@]# mysql -u root -p
MariaDB [(mysql)]> rename user 'username'@'%' to 'myname'@'%';
MariaDB [(mysql)]> flush privileges;

删除用户

MariaDB [(mysql)]> drop user 'myname'@'%';
MariaDB [(mysql)]> flush privileges;

mariaDB权限列表

权 限作用范围作 用
all服务器所有权限
select表、列选择行
insert表、列插入行
update表、列更新行
delete删除行
create数据库、表、索引创建
drop数据库、表、视图删除
reload服务器允许使用flush语句
shutdown服务器关闭服务
process服务器查看线程信息
file服务器文件操作
grant option数据库、表、存储过程授权
references数据库、表外键约束的父表
index创建/删除索引
alter修改表结构
show databases服务器查看数据库名称
super服务器超级权限
create temporary tables创建临时表
lock tables数据库锁表
execute存储过程执行
replication client服务器允许查看主/从/二进制日志状态
replication slave服务器主从复制
create view视图创建视图
show view视图查看视图
create routine存储过程创建存储过程
alter routine存储过程修改/删除存储过程
create user服务器创建用户
event数据库创建/更改/删除/查看事件
trigger触发器
create tablespace服务器创建/更改/删除表空间/日志文件
proxy服务器代理成为其它用户
usage服务器没有权限
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值