sentos7下yum安装mariadb

1. MySQL 常见版本

⚫ MySQL Community Server 社区版本,开源免费,但不提供官方技术支持。
⚫ MySQL Enterprise Edition 企业版本,需付费,可以试用 30 天。
⚫ MySQL Cluster 集群版,开源免费。可将几个 MySQL Server 封装成一个 Server。
⚫ MySQL Cluster CGE 高级集群版,需付费

2. 常见资料:

服务:mysql
端口:3306
主配置文件:/etc/my.cnf
初始化脚本:mysql_install_db
启动命令:mysqld_safe
数据目录 :/var/lib/mysql
套接字文件:/var/lib/mysql/mysql.sock
注:一般需要软连接到/tmp/下
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
进程文件:/var/run/mariadb/mariadb.pid
日志:/var/log/mariadb/mariadb.log

3. yum安装mariadb

[root@oppo2 ~]# yum -y install mariadb*

启动mariadb

[root@oppo2 ~]# mysql -uroot -p123456

报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

查看数据库是否启动

[root@oppo2 ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

未启动数据库,我们启动数据库

[root@oppo2 ~]# systemctl start mariadb
[root@oppo2 ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2021-03-18 11:39:02 CST; 3s ago
  Process: 1537 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1454 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 1536 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─1536 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─1701 /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.pi...

3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: MySQL manual for more instructions.
3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: Please report any problems at http://mariadb.org/jira
3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: The latest information about MariaDB is available at http://mariadb.org/.
3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: You can find additional information about the MySQL part at:
3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: http://dev.mysql.com
3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: Consider joining MariaDB's strong and vibrant community:
3月 18 11:39:00 oppo2 mariadb-prepare-db-dir[1454]: https://mariadb.org/get-involved/
3月 18 11:39:00 oppo2 mysqld_safe[1536]: 210318 11:39:00 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
3月 18 11:39:00 oppo2 mysqld_safe[1536]: 210318 11:39:00 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
3月 18 11:39:02 oppo2 systemd[1]: Started MariaDB database server.
[root@oppo2 ~]# 

重新链接数据库

[root@oppo2 ~]# mysql -uroot -p123456    
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

可能是密码错误,修改密码

[root@oppo2 ~]# mysqladmin -uroot password '123456'

重新登录,成功进入数据库

[root@oppo2 ~]# mysql -uroot -p123456              
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 

注:这里要是报错误[root@oppo1 tmp]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
就是找不到/tmp/mysql.sock文件,将/var/lib/mysql/mysql.sock文件链接到/tmp/下即可
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

4. MySQL 登录及退出命令:

设置密码:mysqladmin -uroot password ‘123456’
登录:mysql -u 用户名 -p 密码 -P 端口 -S 套接字文件
-p 用户密码
-h 登陆位置(主机名或 ip 地址)
-P 端口号(3306 改了就不是了)
-S 套接字文件(/var/lib/mysql/mysql.sock)
退出命令:exit 或 ctrl+d

5. MySQL 管理命令

  1. 创建登录用户
mysql>create user zhangsan@‘%’ identified by '123456';
%:指任意的远程终端
  1. 测试用户登录
# yum -y install mariadb
# mysql -uzhangsan -p123456 -h 192.168.88.10
  1. 用户为自己更改密码
mysql>set password=password ('123456');
  1. root 用户为其他用户找回密码
mysql>set password for atguigu@'%'=password ('123456');

5.root 找回自己的密码并修改
关闭数据库,修改主配置文件(/etc/my.cnf)添加:skip-grant-tables

# vim /etc/my.cnf
skip-grant-tables

启动数据库,空密码登录并修改密码

update mysql.user set password=password('新密码') where user='root';

删除 skip-grant-tables,重启数据库验证新密码
6.创建查询数据库

mysql>create database web; 
mysql>show databases;

7.创建数据表

Mysql>use web;  
 #选择使用web库
Mysql>create table a1 (id int ,name char(30));
#创建 a1 表,并添加 id 和 name 字段以及类型
Mysql>describe a1;  
#desc是describe的缩写【desc a1;】
#查看表结构(字段)

复杂一点的

Mysql>create table a2 (
->id int unsigned not null auto_increment, #字段要求为正数、且自增长、主键
->name char(30) not null default '', #字符型长度 30 字节,默认值为空格 
->age int not null default 0, #字段默认值为 0 
->primary key (id)); #设置 id 为主键
Mysql> describe a2;

8.插入数据

Mysql>insert into a2 (id,name,age) values (1,'zhangsan',21)#指明插入字段和数据,字符要用单引号括起来
Mysql>select * from a2;
Mysql>insert into a2 values (2,'lisi',20)#按顺序插入指定字段
Mysql>insert into a2 values (3,'wangwu')#未声明年龄
Mysql>insert into a2 values (4,'zhao',19)(5,'sun',25)#插入多条数据

9.将表 a2 的数据复制到表 a1

Mysql>select * from a1;
Mysql>insert into a1 (id,name) select id,name from a2;
#查询 a2 值,并写入到 a1
Mysql>select * from a1;

10.删除数据库

Mysql>drop database abc;
Mysql>show databases;

11.删除数据表

Mysql>drop table a1;
Mysql>show table;

12.删除表里的数据记录

Mysql>delete from a2 where id=5; #删除 id=5 的记录
Mysql>delete from a2 where between 23 and 25; #删除年龄在 23-25 之间的

注:库和表的删除用 drop,记录删除用 delete


13.修改表中的数据

Mysql>update a2 set age=21 where id=3;

14.修改数据表的名称

Mysql>alter table a2 rename a1;

15.修改数据表的字段类型

Mysql>describe a1;
Mysql>alter table a1 modify name char(50);
Mysql>describe a1;

16.修改数据表的字段类型详情

Mysql>describe a1;
Mysql>alter table a1 change name username char(50) not null default ‘’;
Mysql>describe a1;

“修改数据表的字段类型(modify)”和”修改数据表的字段类型详情(change)”是不一样的,前者是只修改字段的字符长度,后者是修改包括字符长度,默认值和自增之类的属性。

17.添加字段

Mysql>describe a1;
Mysql>alter table a1 add time datetime;
Mysql>describe a1;
#添加位置默认在末尾
Mysql>alter table a1 add birthday year first; #添加字段到第一列
Mysql>alter table a1 add sex nchar(1) after id; #添加到指定字段后

18.删除字段

Mysql>alter table a1 drop birthday;

删除字段和删除表、库、数据不同。删除表和库是使用“drop”,删除数据使用“delete”,删除字段也是使用“drop”,只是需要配合"alter" 进行使用。

19.Mysql 用户授权

授予用户全部权限
Mysql>select user from mysql.user;
Mysql>grant all on aa.a1 to chenfeng@'%'; #给已存在用户授aa库a1表权限
Mysql>grant all on *.* to chenfeng@'%'; #给已存在用户授所有库所有表的权限
Mysql>grant all on aa.a1 to chenfeng@'%' identified by '123456'; #创建用户并授aa库a1表权限
Mysql>grant all on *.* to chenfeng@'%' identified by '123456'; #创建用户并授所有库所有表的权限

取消 abc 用户的删除库、表、表中数据的权限

Mysql>revoke drop,delete on aa.a1 from chenfeng@'%'; #取消删除权限(登录 chenfeng 测试)
Mysql>show grants for chenfeng@'%'; #查看指定用户的授权

6.备份和还原

mysqldump 备份:

备份:

mysqldump -u 用户名 -p 数据库名 > /备份路径/备份文件名(备份整个数据库)
mysqldump -u 用户名 -p 数据库名 表名 > /备份路径/备份文件名(备份数据表)
备份多个库:--databases 库 1,库 2
备份所有库:--all-databases
备份多个表:库名 表 1 表 2

还原:

mysql 数据库 < 备份文件
mysql -u账号 -p密码  数据库 < 备份文件

注意:还原时,若导入的是某表,请指定导入到哪一个库中

mysqlhotcopy 备份:

备份:

mysqlhotcopy --flushlog -u=’用户’ -p=’密码’ --regexp=正则 备份目录

还原:

cp -a 备份目录 数据目录(/var/lib/mysql)

示例

备份有规则的数据库
Mysql>create database a1; #连续创建三个 a 开头的数据库
# mysqlhotcopy --flushlog –u=‘root’ –p=‘456’ --regexp=^a
还原(先模拟丢失)
Mysql>drop database a1; #顺序删除 a 开头的数据库
cp –a /mnt/* /var/lib/mysql/ #复制产生的文件到数据库目录下
#登录数据库查看即可

mysql-binlog 日志备份:

二进制日志(log-bin 日志):所有对数据库状态更改的操作(create、drop、update 等)

修改 my.cnf 配置文件开启 binlog 日志记录功能

# vim /etc/my.cnf
log-bin=mysql-bin #启动二进制日志

二进制备份方式:

  1. 按时间还原:
--start-datetime
--stop-datetime
格式:mysqlbinlog --start-datetime ‘YY-MM-DD HH:MM:SS’ --stop-datetime ‘YY-MM-DD HH:MM:SS’ 二进制日志 | mysql -uroot -p
  1. 按文件大小还原:
--start-position
--stop-position

在开启二进制时先关闭数据库,然后进行修改配置文件的操作,再开启数据库。
开启数据库时报错:

`210318 16:07:32 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended
210318 16:10:26 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
210318 16:10:26 [Note] /usr/libexec/mysqld (mysqld 5.5.68-MariaDB) starting as process 3760 ...
210318 16:10:26 InnoDB: The InnoDB memory heap is disabled
210318 16:10:26 InnoDB: Mutexes and rw_locks use GCC atomic builtins
210318 16:10:26 InnoDB: Compressed tables use zlib 1.2.7
210318 16:10:26 InnoDB: Using Linux native AIO
210318 16:10:26 InnoDB: Initializing buffer pool, size = 128.0M
210318 16:10:26 InnoDB: Completed initialization of buffer pool
InnoDB: Error: checksum mismatch in data file ./ibdata1
210318 16:10:26 InnoDB: Could not open or create data files.
210318 16:10:26 InnoDB: If you tried to add new data files, and it failed here,
210318 16:10:26 InnoDB: you should now edit innodb_data_file_path in my.cnf back
210318 16:10:26 InnoDB: to what it was, and remove the new ibdata files InnoDB created
210318 16:10:26 InnoDB: in this failed attempt. InnoDB only wrote those files full of
210318 16:10:26 InnoDB: zeros, but did not yet use them in any way. But be careful: do not
210318 16:10:26 InnoDB: remove old data files which contain your precious data!
210318 16:10:26 [ERROR] Plugin 'InnoDB' init function returned error.
210318 16:10:26 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
210318 16:10:26 [Note] Plugin 'FEEDBACK' is disabled.
210318 16:10:26 [ERROR] Unknown/unsupported storage engine: InnoDB
210318 16:10:26 [ERROR] Aborting

210318 16:10:26 [Note] /usr/libexec/mysqld: Shutdown complete`

日志文件冲突导致的,删除mysql下的日志(路径/var/lib/mysql),名字叫ib_logfile和ibdata的统统删掉(为了保险起见,建议将要删除的文件备份,或者就直接mv就好)。然后再次重启mariadb就可以了。
查看/var/lbi/mysql/下是否存在mariadb的二进制文件mysql-bin.000001

查看mysqlbinlog 文件:

[root@oppo1 mysql]# mysqlbinlog mysql-bin.000001 

找到时间和大小,然后根据时间还原和大小还原的方式进行即可。

猜你喜欢

转载自blog.csdn.net/qq_40736702/article/details/114967674