云计算1+X初级题目之一 Centos7的mariadb主从服务
题目:使用镜像 centos7,创建两台云主机master和slave,并分别绑定浮动IP;在这2台云主机上安装mysql据库系统并配置为主从数据库(master为主节点、slave为从节点);并mater云主机的数据库中创建ChinaSkilldb库,在ChinaSkilldb库中创建表testable (id int not null primary key,Teamname varchar(50), remarks varchar(255)),在表中插入记录(1,“Cloud”,“ChinaSkill”)。
master为主节点 : 192.168.100.128
slave为从节点:192.168.100.129
安装mariadb服务(master和slave)
yum install mariadb mariadb-server -y
=============================================
systemctl start mariadb (重启mariadb)
=============================================
systemctl enable mariadb (开机自动启动服务)
=============================================
[root@root@master ~]# mysql_secure_installation(初始化数据库)
=============================================
[root@root@slave ~]# mysql_secure_installation(初始化数据库)
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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):
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
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
... 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] 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.
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.
Reload privilege tables now? [Y/n] 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
[root@master ~]# mysql -uroot -p000000
MariaDB [(none)]> grant all on *.* to 'user'@'%' identified by '000000';
MariaDB [(none)]> update mysql.user set Grant_priv='Y' where Host='%';
=============================================
[root@slave ~]# mysql -uroot -p000000
MariaDB [(none)]> grant all on *.* to 'user'@'%' identified by '000000';
MariaDB [(none)]> update mysql.user set Grant_priv='Y' where Host='%';
以上操作两个节点一致
配置master和slave的my.cnf
master
[root@master ~]# vi /etc/my.cnf
[mysqld]
server-id=99 ID可以自定 但是不能与slave一样
log-bin=mysql-bin
wq 保存退出
[root@master ~]# systemctl restart mariadb 重启服务
slave
[root@slave ~]# vi /etc/my.cnf
[mysqld]
server-id=101 ID可以自定 但是不能与master一样
log-bin=mysql-bin
wq 保存退出
[root@slave ~]# systemctl restart mariadb 重启服务
配置master和slave数据库
master
[root@master ~]# mysql -uroot -p000000 进入mysql
MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 616
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
slave
[root@slave ~]# mysql -uroot -p000000 进入mysql
MariaDB [(none)]> show slave status\G
Empty set (0.00 sec)
=============================================
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
=============================================
MariaDB [(none)]> change master to master_host='192.168.100.128',master_user='user',master_password='000000',master_log_file='mysql-bin.000001',master_log_pos=616;
Query OK, 0 rows affected (0.00 sec)
=============================================
PS:master_log_file='mysql-bin.000001' 对应master的 File:
PS:master_log_pos=616; 对应master的 Position
最后创建ChinaSkilldb库,在ChinaSkilldb库中创建表testable (id int not null primary key,Teamname varchar(50), remarks varchar(255)),在表中插入记录(1,“Cloud”,“ChinaSkill”)
MariaDB [(none)]> create database ChinaSkilldb; 创建ChinaSkilldb库
Query OK, 1 row affected (0.00 sec)
============================================
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ChinaSkilldb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
=============================================
MariaDB [(none)]> use ChinaSkilldb; 使用ChinaSkilldb库
Database changed
=============================================
MariaDB [ChinaSkilldb]> create table testable (id int not null primary key,Teamname varchar(50), remarks varchar(255)); 创建表testable
Query OK, 0 rows affected (0.00 sec)
=============================================
MariaDB [ChinaSkilldb]> show tables;
+------------------------+
| Tables_in_ChinaSkilldb |
+------------------------+
| testable |
+------------------------+
1 row in set (0.00 sec)
=============================================
Database changed
MariaDB [ChinaSkilldb]> insert into testable values (1,"Cloud","ChinaSkill"); 插入内容
Query OK, 1 row affected (0.00 sec)
=============================================
END