[数据库] MariaDB安装及使用
一、安装MariaDB
1.使用官方源安装marisdb
如果使用阿里云的源,目前的版本号为5.5.64。如果想安装最新的10.x版本,则需要使用MariaDB的官方源。
1)配置官方源:
在/etc/yum.repos.d/下创建MariaDB.repo:
[root@centos-db ~]# cd /etc/yum.repos.d/
[root@centos-db yum.repos.d]# touch MariaDB.repo
[root@centos-db yum.repos.d]# vi MariaDB.repo [mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/PRM-GPG-KEY-MariaDB
gpgcheck=
执行命令:
[root@centos-db yum.repos.d]# yum clean all
[root@centos-db yum.repos.d]# yum makecache
2)安装MariaDB-server和MariaDB-client
配置好MariaDB官方源以后,使用以下命令安装:
[root@centos-db yum.repos.d]# yum install MariaDB-server MariaDB-client
可以看到具体的版本号:
============================================================================================================================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================================================================================================================
Installing:
MariaDB-client x86_64 10.1.-.el7.centos mariadb M
MariaDB-server x86_64 10.1.-.el7.centos mariadb M
2.使用阿里源安装mariadb
如果官方源速度慢,并且我们对新版本没有特别要求,则可以使用阿里源进行安装。
1)删除官方源的repo文件
[root@centos-db yum.repos.d]# cd /etc/yum.repos.d/
[root@centos-db yum.repos.d]# mv MariaDB.repo MariaDB.repo.bk
2)清理缓存
[root@centos-db yum.repos.d]# yum clean all
3)安装mariadb
[root@centos-db yum.repos.d]# yum install mariadb-server mariadb
注意,使用阿里源安装时,名字和官方的不一样,官方的名字是MariaDB-server、MariaDB-client。而阿里源为mariadb-server、mariadb。注意区分大小写和名称。
3.启动MariaDB
使用官方源或阿里源安装好mariadb后,使用以下命令来管理MariaDB服务:
systemctl status mariadb # 查看状态
systemctl start mariadb # 启动
systemctl stop mariadb # 停止
systemctl restart mariadb # 重启
systemctl enable mariadb # 设置开机启动
systemctl disable mariadb # 取消开机启动
启动mariadb:
[root@centos-db yum.repos.d]# systemctl status mariadb
鈼[0m mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Wed -- :: CST; 2s ago
Process: ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=/SUCCESS)
Process: ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=/SUCCESS)
Main PID: (mysqld_safe)
CGroup: /system.slice/mariadb.service
鈹溾攢1702 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
鈹斺攢1864 /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 Jan :: centos-db mariadb-prepare-db-dir[]: MySQL manual for more instructions.
Jan :: centos-db mariadb-prepare-db-dir[]: Please report any problems at http://mariadb.org/jira
Jan :: centos-db mariadb-prepare-db-dir[]: The latest information about MariaDB is available at http://mariadb.org/.
Jan :: centos-db mariadb-prepare-db-dir[]: You can find additional information about the MySQL part at:
Jan :: centos-db mariadb-prepare-db-dir[]: http://dev.mysql.com
Jan :: centos-db mariadb-prepare-db-dir[]: Consider joining MariaDB's strong and vibrant community:
Jan :: centos-db mariadb-prepare-db-dir[]: https://mariadb.org/get-involved/
Jan :: centos-db mysqld_safe[]: :: mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Jan :: centos-db mysqld_safe[]: :: mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jan :: centos-db systemd[]: Started MariaDB database server.
二、初始化
使用命令:
mysql_secure_installation
[root@centos-db yum.repos.d]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here. Enter current password for root (enter for none): # 这里直接enter就可以了
OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation. Set root password? [Y/n] y # 1.是否设置root密码,选择Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success! By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment. Remove anonymous users? [Y/n] y # 2.是否删除匿名账户,因为不安全,选择Y
... Success! Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y # 3.是否允许root用户远程登录,因为要在windows机器上去开发,所以选择Y
... Success! By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment. Remove test database and access to it? [Y/n] y # 4.删除测试数据库,选择Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success! Reloading the privilege tables will ensure that all changes made so far
will take effect immediately. Reload privilege tables now? [Y/n] y # 5.是否立即刷新权限表,选择Y
... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB
installation should now be secure. Thanks for using MariaDB!
三、使用mysql
1.登录mysql
[root@centos-db yum.repos.d]# mysql -uroot -p
Enter password: # 输入root密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
2.修改服务器字符编码
1)查看服务器字符编码
MariaDB [(none)]> \s
--------------
mysql Ver 15.1 Distrib 5.5.-MariaDB, for Linux (x86_64) using readline 5.1 Connection id:
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 5.5.-MariaDB MariaDB Server
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.048
--------------
可以看到,mysql服务器字符编码为latin1,无法显示中文。并且在这种字符编码下,创建的数据库和表都不能插入和显示中文。
2)修改配置文件
[root@centos-db etc]# vi my.cnf # 修改/etc/my.cnf配置文件 [mysqld]
character-set-server=utf8 datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid #
# include all files from the config directory
#
!includedir /etc/my.cnf.d
添加character-set-server=utf8配置。
3)重启mariadb服务
systemctl restart mariadb
4)再次查看字符编码
MariaDB [(none)]> \s
--------------
mysql Ver 15.1 Distrib 5.5.-MariaDB, for Linux (x86_64) using readline 5.1 Connection id:
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 5.5.-MariaDB MariaDB Server
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.285
--------------
3.创建数据库和表
1)查看所有数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.00 sec)
2)创建数据库
MariaDB [(none)]> create database mydb;
Query OK, row affected (0.00 sec)
查看数据库创建过程:
MariaDB [mydb]> show create database mydb;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| mydb | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
row in set (0.00 sec)
3)使用某个数据库
MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]>
4)查看所有表
MariaDB [mydb]> show tables;
Empty set (0.00 sec) # 目前没有任何表
5)创建一张表
MariaDB [mydb]> create table user_info (id int,name char(),age int);
Query OK, rows affected (0.00 sec)
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| user_info |
+----------------+
row in set (0.00 sec)
查看表的创建过程:
MariaDB [mydb]> show create table user_info;
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_info | CREATE TABLE `user_info` (
`id` int() DEFAULT NULL,
`name` char() DEFAULT NULL,
`age` int() DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec)
4.表操作
1)插入一条数据
MariaDB [mydb]> insert into user_info (id,name,age) values (,"张三",);
MariaDB [mydb]> select * from user_info;
+------+--------+------+
| id | name | age |
+------+--------+------+
| | 张三 | |
+------+--------+------+
row in set (0.00 sec)
2)查询数据
MariaDB [mydb]> select * from user_info;
+------+--------+------+
| id | name | age |
+------+--------+------+
| | 张三 | |
| | 李四 | |
+------+--------+------+
rows in set (0.00 sec)
MariaDB [mydb]> select name,age from user_info where id=;
+--------+------+
| name | age |
+--------+------+
| 张三 | |
+--------+------+
row in set (0.00 sec)
3)删除一条数据
MariaDB [mydb]> delete from user_info where id=;
Query OK, row affected (0.00 sec)
MariaDB [mydb]> select * from user_info;
+------+--------+------+
| id | name | age |
+------+--------+------+
| | 李四 | |
+------+--------+------+
4)查看表结构
MariaDB [mydb]> desc user_info;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int() | YES | | NULL | |
| name | char() | YES | | NULL | |
| age | int() | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
rows in set (0.00 sec)
5.管理操作
1)创建用户
MariaDB [(none)]> create user leokale@'%' identified by ''; # '%'表示用户'leokale'在所有的主机地址都可以登录,使用密码是'11111111'。
Query OK, rows affected (0.00 sec)
刚创建好的用户权限很低,无法创建数据库。
2)修改用户密码
自己修改自己密码:
MariaDB [mydb]> set password = PASSWORD('');
Query OK, rows affected (0.00 sec)
root修改其他用户密码:
MariaDB [(none)]> set password for leokale@'%' = PASSWORD("");
Query OK, rows affected (0.00 sec)
3)查看用户表
[root@centos-db etc]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.00 sec) 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]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
rows in set (0.00 sec)
查看用户表中的信息:
MariaDB [mysql]> select host,user,password from user;
+-----------+---------+-------------------------------------------+
| host | user | password |
+-----------+---------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root | *4D77515AB2D393245315E18C62709EC959669A89 |
| :: | root | *4D77515AB2D393245315E18C62709EC959669A89 |
| % | leokale | *E9D057131C22A0D76B4AAD2C61655BDFA706E637 |
+-----------+---------+-------------------------------------------+
rows in set (0.00 sec)
可以看到我们创建的leokale用户。
4)给用户添加权限
使用grant命令:(grant是授予的意思)
MariaDB [mysql]> grant all privileges on *.* to leokale@'%';
MariaDB [mysql]> flush privileges;
Query OK, rows affected (0.00 sec)
这个命令的意思是,授予所有的权限(all privileges),所有的数据库和表(*.*),给用户"leokale"在所有主机登录时。
使用"leokale"登录mysql:
[root@centos-db etc]# mysql -uleokale -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.00 sec)
可以看到,leokale用户在赋予权限后,可以查询到所有的数据库。并且可以进行所有操作。
6.远程登录
1)在远程计算机上安装mariadb-client,然后使用"leokale"用户进行远程登录:
[root@centos-dbclient ~]# mysql -uleokale -p -h 192.168.1.189
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
可以看到"leokale"用户可以正常登录。
2)root用户远程登录
[root@centos-dbclient ~]# mysql -uroot -p -h 192.168.1.189
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'192.168.1.190' (using password: YES)
我们发现,root用户无法远程登录;
这是因为root用户默认不支持所有主机登录,可以从mysql.user表中看到:
MariaDB [mysql]> select user,host from user;
+---------+-----------+
| user | host |
+---------+-----------+
| leokale | % |
| root | 127.0.0.1 |
| root | :: |
| root | localhost |
+---------+-----------+
rows in set (0.00 sec)
所以,我们需要给root用户赋一个可以在任何主机远程登录的权限:
MariaDB [mysql]> grant all privileges on *.* to root@'%' identified by '';
Query OK, rows affected (0.00 sec)
MariaDB [mysql]> flush privileges;
Query OK, rows affected (0.00 sec)
注意,这里一定要重新指定一个密码(远程登录密码和本地密码不一样),也就是identified by "密码"。这里的密码和在localhost上使用root登录的密码不一样。否则无法远程登录。
四、备份和恢复
1.备份数据库
首先观察我们自己创建的数据库有哪些:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mydb2 |
| mydb3 |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.00 sec)
mydb、mydb1、mydb2都是我们自己创建的数据库;
使用linux命令来备份数据库:
[root@centos-db ~]# mysqldump -u root -p --all-databases > ~/AllMysql.dump
Enter password:
[root@centos-db ~]#
可以看到在/root目录下生成了一个AllMysql.dump的文件:
[root@centos-db ~]# ll
total
-rw-r--r--. root root Jan : AllMysql.dump
-rw-------. root root Dec : anaconda-ks.cfg
2.删除数据库
MariaDB [(none)]> drop database mydb; # 删除mydb
Query OK, row affected (0.00 sec) MariaDB [(none)]> drop database mydb2; # 删除mydb2
Query OK, rows affected (0.00 sec) MariaDB [(none)]> drop database mydb3; # 删除mydb3
Query OK, rows affected (0.00 sec)
3.恢复数据库
使用source命令,恢复:
MariaDB [(none)]> source ~/AllMysql.dump
也可以在linux命令行中恢复:
[root@centos-db ~]# mysql -uroot -p < ~/AllMysql.dump
Enter password:
[root@centos-db ~]#
查看是否恢复:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mydb2 |
| mydb3 |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.00 sec)
可以看到,被我们删除的数据库已经恢复。
五、主从复制
1.基本原理
主从的数据同步如下图所示:
当主库有数据更新时,会将操作写入到Binary log中,然后从库会从Binary log中读取操作命令,并且写到自己的log中,然后执行命令,向自己的库中插入数据。这就保证从库的数据和主库同步。
2.主库配置
1)主库配置文件
[mysqld]
# mysql服务器id
server-id=
# 指定Binary log文件名
log-bin=mysql-bin character-set-server=utf8
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid #
# include all files from the config directory
#
!includedir /etc/my.cnf.d
2)登录数据库,查看主从状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)
可以看到,我们的Binary log文件名, 以及Position(记录位置)。
3)主库创建负责同步的用户
MariaDB [(none)]> create user 'copyer'@'%' identified by '';
Query OK, rows affected (0.00 sec)
4)赋予拷贝权限:
MariaDB [(none)]> grant replication slave on *.* to 'copyer'@'%'; # 授予其slave身份
Query OK, rows affected (0.00 sec)
5)查看账号
MariaDB [(none)]> select user,host from mysql.user;
+---------+-----------+
| user | host |
+---------+-----------+
| copyer | % |
| leokale | % |
| root | % |
| root | 127.0.0.1 |
| root | :: |
| root | localhost |
+---------+-----------+
rows in set (0.00 sec)
6)检查授权账号的权限
MariaDB [(none)]> show grants for copyer@'%';
+-------------------------------------------------------------------------------------------------------------------+
| Grants for copyer@% |
+-------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'copyer'@'%' IDENTIFIED BY PASSWORD '*2DF3063C523DF16DEFB8A454F9DA045D92D509BA' |
+-------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec)
7)锁定数据库
锁定数据库是为了不让数据变动,确保position不动。
MariaDB [(none)]> flush table with read lock;
Query OK, rows affected (0.00 sec)
此时,查看一下position:
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| | 474 | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)
8)导出数据
导出数据,并在从节点恢复,保证主从的旧数据同步。
主节点导出数据:
[root@centos-db ~]# mysqldump -uroot -p --all-databases > /opt/zhucong.dump
Enter password:
[root@centos-db ~]#
将备份文件传输到从节点:
[root@centos-db ~]# scp /opt/zhucong.dump root@192.168.1.190:/opt/
The authenticity of host '192.168.1.190 (192.168.1.190)' can't be established.
ECDSA key fingerprint is SHA256:fGo4tMrS0HOc+z7k+6tkQUTxPxWwAoBw30xYF59d5tc.
ECDSA key fingerprint is MD5:::f3:3f::3a:::7e::c6:8f::d1:8a:.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.190' (ECDSA) to the list of known hosts.
root@192.168.1.190's password:
zhucong.dump
9)从节点导入数据
# 登录从节点mysql操作 MariaDB [(none)]> source /opt/zhucong.dump
Query OK, rows affected (0.00 sec)
查看从节点数据库:
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mydb2 |
| mydb3 |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.00 sec)
注意:当我们的从节点导入了从主节点导出的数据,账号密码也变得和主节点一致,当重启服务后,需要使用主节点的root密码来登录。
3.从库配置
1)从库配置文件
[mysqld]
server-id=
character-set-server=utf8
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid #
# include all files from the config directory
#
!includedir /etc/my.cnf.d
指定一个server-id,注意,要和主库区分开(主库为server-id=1)。
修改完配置文件后,重启数据库:
[root@centos-dbclient opt]# systemctl restart mariadb
2)查看从库的log_bin和server_id
MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
row in set (0.00 sec)
可以看到,从节点没有启用log-bin(只有主节点需要使用Binary log)。
MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | |
+---------------+-------+
row in set (0.00 sec)
可以看到,我们在配置文件中配置的从节点server-id=2已生效。
3)配置从库与主库的联系(很重要)
MariaDB [(none)]> change master to master_host='192.168.1.189', # master_host是主节点IP
-> master_user='copyer', # master_user是我们在主节点上创建的负责复制数据的用户
-> master_password='', # copyer用户密码
-> master_log_file='mysql-bin.000001', # 主节点的Binary log文件名,在主节点可以用show master status;查看
-> master_log_pos=; # Binary log文件的内容位置,也是通过show master status;查看(我们事先进行了锁表,pos不会变)
Query OK, rows affected (0.01 sec)
4)开启slave开关
MariaDB [(none)]> start slave;
Query OK, rows affected (0.00 sec)
开启slave后,从节点就会开启两个线程来和主节点通信。
5)查看slave信息
MariaDB [(none)]> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.189
Master_User: copyer
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mariadb-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
row in set (0.00 sec)
可以看到 Slave_IO_Running 和 Slave_SQL_Running都是YES。
4.解锁主库,并验证
1)主库从库都配置完毕后,我们将主库解锁:
MariaDB [(none)]> unlock tables;
Query OK, rows affected (0.00 sec)
2)验证
在主库中创建一个数据库:
MariaDB [(none)]> create database test_db;
Query OK, row affected (0.00 sec)
在从库中查看数据库:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mydb2 |
| mydb3 |
| mysql |
| performance_schema |
| test_db |
+--------------------+
rows in set (0.00 sec)
我们可以看到,从库也创建了一个数据库叫test_db;
至此,主从复制部署完毕。
(>‿◠)✌
[数据库] MariaDB安装及使用的更多相关文章
- yum install mariadb安装数据库开启不了
centos7内置的MySQL镜像已经放弃Oracle公司的MySQL,改用MySQL的分支数据库mariaDB,使用以下安装mariadb: yum install mariadb 然后使用命令sy ...
- Windows10下MariaDB数据库的安装与卸载
MariaDB数据库管理系统是MySQL的一个分支,100%兼容Mysql,开源免费,在Windows系统和Linux系统中都能运行,很受到欢迎.自从mysql被Oracle收购后,MariaDB就成 ...
- Ubuntu 下 Mariadb 数据库的安装和目录迁移
Ubuntu 下 Mariadb 数据库的安装和目录迁移 1.简介 本文主要是 Ubuntu 下 Mariadb 数据库的安装和目录迁移,同样适用于 Debian 系统:Ubuntu 20.0.4 M ...
- mysql、mariadb安装和多实例配置
本文目录:1. mysql单实例安装 1.1 rpm安装mysql 1.2 通用二进制包安装mysql 1.2.1 初始化数据库 1.2.2 安装后的规范化操作 1.3 编译安装 1.3.1 编译安装 ...
- centos 7 mariadb安装
centos 7 mariadb安装 1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB sy ...
- MariaDB安装及基本配置
MariaDB安装及基本配置(CentOS6.9) 数据库基础概念 数据库(Database, DB)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. DBMS: Database ...
- mariadb安装和一些sql基础
MariaDB安装 yum -y install mariadb mariadb-server 启动 systemctl start mariadb systemctl enabl ...
- mariadb安装配置
CentOS 7安装MariaDB 详解以及相关配置 第一步:添加 MariaDB yum 仓库 首先在CentOS操作系统中/etc/yum.repos.d/目录下添加 MariaDB 的YUM配置 ...
- mysq数据库的安装和基本操作
一.数据库的简介 1.什么是数据库? 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独 ...
随机推荐
- IntelliJ IDEA 2017.3尚硅谷-----生成 javadoc
Locale:输入语言类型:zh_CN Other command line arguments:-encoding UTF-8 -charset UTF-8
- Python入门2 —— 变量
一:问号三连 1.什么是变量? 变 指的是事物的状态是可以发生变化的 量 指的是记录事物的状态 2.为什么要有变量? 为了让计算机像人一样去记录事物的状态 3.怎么用变量? 先定义 后引用 二:变量的 ...
- pip知识点
pip第三方模块保存在\Lib\site-packages目录 安装第三方模块:\Script目录下->shift+鼠标右点打开powershift窗口 ->pip install 模块名 ...
- python多进程编程学习笔记
摘自[https://www.cnblogs.com/chenhuabin/p/10070996.html] by 奥辰 赞
- 新手学习PHP的避雷针,这些坑在PHP开发中就别跳了
不要!用记事本编辑php文件 早些年能用记事本编程是一些人自我吹嘘的资本,能用记事本编程就是牛逼的代名词.但是这里要告诫大家的是,千万不要使用Windows自带的记事本编辑任何文本文件.用Window ...
- 一文明白所谓的CS与BS设计模式
CS设计模式 概念:CS设计模式,C代表的是Client,S代表的是Server.正如图中的所示,是客户机与服务器之间的交互.这种交互在早期的软件系统中,大多数都是采用这种模式,通过将任务合理分配到C ...
- POJ 3177 Redundant Paths (tarjan边双连通分量)
题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...
- 题解 P2320 【[HNOI2006]鬼谷子的钱袋】
P2320 [HNOI2006]鬼谷子的钱袋 挺有趣的一道题,之所以发这篇题解是因为感觉思路的更清晰一点qwq 此题主要有两种方法: 一.分治思想 例如要凑出1~20,假如我们已经能凑出1~10了,那 ...
- Codeforces A. Serval and Bus
inputstandard inputoutputstandard outputIt is raining heavily. But this is the first day for Serval, ...
- 概率dp poj2096
/** dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样 ...