MariaDB 10.4:
1、安装
OS: CentOS7.3以后,最小化安装(不带图形化),安装完毕之后禁用SELinux、关闭防火墙。
安装常用软件:vim、wget、bash-completion、net-tools
确保机器联网。
rpm安装–使用yum
通用二进制
源码
[mariadb]
name=mariadb-10.4
baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64/
gpgcheck=0
本地光盘作为yum源:
[root@kongd ~]# yum install mariadb-server -y
2、启动
systemctl start mariadb
systemctl enable mariadb
或者使用下面命令启动:
systemctl enable --now mariadb
[root@kongd ~]# netstat -lnupt | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 38251/mysqld
了解其他数据库端口:
MS SQL:1433
Oracle:1521
[root@kongd ~]# firewall-cmd --permanent --add-service=mysql
success
[root@kongd ~]# firewall-cmd --reload
success
3、初始安全
mysql_secure_installation
设置root密码
禁用root远程登录
移除匿名用户
移除test数据库
4、设置密码
没有密码:
mysqladmin -uroot password “123456”
修改密码:
mysqladmin -uroot -p"888888" password “123456”
登录后修改:
[root@kongd ~]# mysql -uroot -p密码
方法1:alter user root@‘localhost’ identified by ‘123456’; // 5.5版本不支持
方法2:set password for root@localhost = password(‘12345’);
方法3:
MariaDB [(none)]> update mysql.user
-> set password=password(‘123456’)
-> where User=“root” and Host=“localhost”;
MariaDB [(none)]> flush privileges;
5、重置密码
–skip-grant-tables 跳过权限表,以下是10.4重置方式。
1)restart MariaDB with --skip-grant-tables
重启时跳过权限表
前提停止数据库:systemctl stop mariadb
mysqld --skip-grant-tables --user=mysql
2)login into the unprotected server
登录数据库:use mysql
3)run FLUSH PRIVILEGES
刷新权限表:FLUSH PRIVILEGES
4)run SET PASSWORD FOR root@localhost to change the root password
重置密码:SET PASSWORD FOR ‘root’@‘localhost’ = PASSWORD(‘123456’);
5.5 重置root密码:
-
停止数据库
[root@kongd ~]# systemctl stop mariadb -
启动时加上–skip-grant-tables 跳过权限表
[root@kongd ~]# mysqld_safe --skip-grant-tables --user=mysql -
登录数据库,修改密码
[root@kongd ~]# mysql
MariaDB [(none)]> update mysql.user
-> set password=password(‘123’)
-> where User=“root” and Host=“localhost”;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
验证:
停止:
[root@kongd ~]# yum install psmisc -y
[root@kongd ~]# killall -9 mysqld
mysqld: no process found
启动:
[root@kongd ~]# systemctl start mariadb
[root@kongd ~]# mysql -uroot -p123456 -e ‘show databases;’
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
±-------------------+
6、登录
命令行:[root@kongd ~]# mysql -u root -p123456 -D mysql -h localhost
-u:用户名
-p:密码,注意-p和密码不能有空格
-D:数据库名
-h:主机
windows下的客户端(GUI)
Navicat for MySQL
授权一个用户:grant all on . to admin@’%’ identified by ‘123456’;
SQLyog:
7、支持中文
设置服务器默认字符集
[root@kongd my.cnf.d]# grep “[mysqld” -A 2 server.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
设置数据库默认字符集
[root@kongd my.cnf.d]# grep “[mysql]” -A 1 mysql-clients.cnf
[mysql]
default-character-set=utf8
重启服务:
[root@kongd my.cnf.d]# systemctl restart mariadb
测试:
MariaDB [(none)]> show variables like ‘character%’;
±-------------------------±---------------------------+
| Variable_name | Value |
±-------------------------±---------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
±-------------------------±---------------------------+
8 rows in set (0.001 sec)
MariaDB [(none)]> show variables like ‘collation%’;
±---------------------±----------------+
| Variable_name | Value |
±---------------------±----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
±---------------------±----------------+
3 rows in set (0.001 sec)
8、使用SQL命令管理数据库
1)数据库管理命令
查看数据库:MariaDB [(none)]> show databases;
查看当前库:MariaDB [(none)]> select database();
查看当前用户:MariaDB [(none)]> select user();
创建数据库:CREATE DATABASE IF NOT EXISTS dbname;
切换数据库:USE dbname
删除数据库:慎用!!! DROP DATABASE IF EXISTS dbname;
技巧:命令大写可以tab补全。
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS student;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> USE student;
Database changed
2)数据表结构管理
创建表:CREATE TABLE
查看表:SHOW TABLES;
查询指定数据库的表:SHOW TABLES FROM mysql;
查看建表语句:SHOW CREATE TABLE db1.tb1;
表结构:DESCRIBE dbname.tablename;
删除表:DROP TABLE dbname.tablename;
MariaDB [student]> create table score
-> (id int not null primary key auto_increment,
-> name char(12) not null);
Query OK, 0 rows affected (0.005 sec)
更改表名:
alter table db2 rename to tb2;
rename table tb2 to t2;
3)记录的增删改查
插入数据:
insert into score values(20190001,‘张三’);
insert into score(name) values(‘张华’),(“李四”),(“王五”),(“王明”),(“王刚”);
查询:
所有的:
MariaDB [student]> select * from score;
±---------±-------+
| id | name |
±---------±-------+
| 20190001 | 张三 |
| 20190002 | 张华 |
| 20190003 | 李四 |
| 20190004 | 王五 |
| 20190005 | 王明 |
| 20190006 | 王刚 |
±---------±-------+
6 rows in set (0.000 sec)
查询姓王的:
MariaDB [student]> select * from score
-> where name like ‘王%’;
±---------±-------+
| id | name |
±---------±-------+
| 20190004 | 王五 |
| 20190005 | 王明 |
| 20190006 | 王刚 |
±---------±-------+
3 rows in set (0.000 sec)
也可以使用正则
MariaDB [student]> select * from score where name regexp “^王”;
±---------±-------+
| id | name |
±---------±-------+
| 20190004 | 王五 |
| 20190005 | 王明 |
| 20190006 | 王刚 |
±---------±-------+
3 rows in set (0.001 sec)
更新:update
MariaDB [student]> update score
-> set name=“张三丰”
-> where name=“张三”;
删除:delete from 【删除表中指定内容,如果没有where将表中所有数据删除】
MariaDB [student]> delete from score
-> where id=20190006;
快速删除表中所有内容,保留表结构:
TRUNCATE TABLE tb_name;
9、用户及权限
创建用户:create user 用户名@来源 identified by ‘密码’;
来源地址:
localhost --本机
192.168.150.% --网段
% --所有
授权:grant 权限列表 on 数据库名.表名 to 用户名@来源;
以上两条命令可以合二为一。
grant 权限列表 on 数据库名.表名 to 用户名@来源 identified by ‘密码’;
收回:revoke 权限列表 on 数据库名.表名 from 用户名@来源;
查看:show grants for 用户名@来源;
删除用户:DROP USER ‘jeffrey’@‘localhost’;
10、使用mysqldump备份
备份所有库:[root@kongd ~]# mysqldump -uroot -p -A -B > all.db.sql
备份student库:mysqldump -uroot -p -B student > student.sql
备份表:mysqldump -uroot -p student score > student_score.sql
恢复:
方法1: mysql -uroot -p < 备份文件
方法2:进入数据库 source 备份文件