$ sudo yum clean all && yum repolist 已加载插件:fastestmirror 正在清理软件源: mariadb Cleaning up list of fastest mirrors Other repos take up 634 M of disk space (use --verbose for details) 已加载插件:fastestmirror Determining fastest mirrors mariadb | 2.9 kB 00:00:00 mariadb/primary_db | 144 kB 00:00:00 源标识 源名称 状态 mariadb mariadb 165 repolist: 165
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.
# 这里需要输入当前 root 用户的密码 # 如果刚安装 MariaDB 并还没有设置 root 用户密码,那么密码是空的,按回车即可 Enter current password for root (enter for none): #默认按回车 OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
# 这里问你是否需要设置 root 用户的密码 # 按 Y 进行设置 Set root passworNew password: #这里输入密码123456 Re-enter new password: #确认密码:再输一遍123456 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.
# 是否移除匿名用户 # 按 Y 进行移除 Remove anonymous users? [Y/n] d? [Y/n] 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.
# 是否禁用 root 用户远程登录 # 按 Y 禁用远程登录 Disallow root login remotely? [Y/n] n ... skipping.
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.
# 是否移除测试数据库 test 及其权限 # 按 Y 移除 Remove test database and access to it? [Y/n] 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.
# 在主节点 db1 虚拟机上使用 mysql 命令登录 MariaDB 数据库 $ mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.18-MariaDB-log 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 用户登录到数据库 MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.001 sec)
# 在主节点 db1 数据库上创建一个 user 用户让从节点 db2 连接,并赋予从节点同步主数据库的权限 MariaDB [(none)]> GRANT REPLICATION slave ON *.* TO 'user'@'db2' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.001 sec)
2.5 配置从节点 db2 同步主节点 db1
在从节点 db2 上使用 mysql 命令登录 MariaDB 数据库,配置从节点连接主节点的连接信息
master_host 为主节点主机名 db1,master_user 则是在上一步中创建的用户 user
1 2 3 4 5 6 7 8 9 10 11 12 13
$ mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 10.3.18-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)]> USE test; Database changed MariaDB [test]> CREATE TABLE company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.011 sec)
MariaDB [test]> INSERT INTO company VALUES(1,"facebook","usa"); Query OK, 1 row affected (0.003 sec)
MariaDB [test]> SELECT * FROM company; +----+----------+------+ | id | name | addr | +----+----------+------+ | 1 | facebook | usa | +----+----------+------+ 1 row in set (0.000 sec)
此时 从节点 db2 的数据库就会同步主节点数据库创建的 test 库,可以在从节点查询 test 数据库与表 company 如果可以查询到信息,就能验证主从数据库集群功能在正常运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.001 sec)
MariaDB [(none)]> SELECT * FROM test.company; +----+----------+------+ | id | name | addr | +----+----------+------+ | 1 | facebook | usa | +----+----------+------+ 1 row in set (0.001 sec)
# 查看 mycat 的服务库 USERDB(它对应的是数据库 test) $ mysql -h127.0.0.1 -P8066 -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> MySQL [(none)]> SHOW DATABASES; +----------+ | DATABASE | +----------+ | USERDB | +----------+ 1 row in set (0.004 sec)
MySQL [(none)]> USE USERDB; Database changed
MySQL [USERDB]> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | company | +----------------+ 1 row in set (0.004 sec)
MySQL [USERDB]> SELECT * FROM company; +----+----------+------+ | id | name | addr | +----+----------+------+ | 1 | facebook | usa | +----+----------+------+ 1 row in set (0.087 sec)
4.2 用 MyCat 服务添加表数据
1 2 3 4 5 6 7 8 9 10 11 12 13
# 在 MyCat 上使用 mysql 命令对标 company 添加一条数据 `(2,"basketball","usa")` MySQL [USERDB]> INSERT INTO company VALUES(2,"basketball","usa"); Query OK, 1 row affected (0.012 sec)
# 查看表信息 MySQL [USERDB]> SELECT * FROM company; +----+------------+------+ | id | name | addr | +----+------------+------+ | 1 | facebook | usa | | 2 | basketball | usa | +----+------------+------+ 2 rows in set (0.003 sec)
4.3 验证 MyCat 服务对数据库读写操作分离
在 mycat 虚拟机上使用 mysql 命令,通过 9066 端口查询对数据库读写操作的分离信息
1 2 3 4 5 6 7
$ [root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show @@datasource;' +----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+ | DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD | +----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+ | dn1 | hostM1 | mysql | 192.168.92.131 | 3306 | W | 0 | 10 | 1000 | 92 | 0 | 1 | | dn1 | hostS1 | mysql | 192.168.92.132 | 3306 | R | 0 | 6 | 1000 | 91 | 4 | 0 | +----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+