# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE:RUNNINGALLPARTSOFTHISSCRIPTISRECOMMENDEDFORALLMariaDBSERVERSINPRODUCTION USE!PLEASEREADEACHSTEP CAREFULLY!In order to log intoMariaDB 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: #输入数据库root密码123456
Re-enter new password: #重复输入密码123456Password updated successfully!Reloading privilege tables..... Success!Bydefault, a MariaDB installation has an anonymous user, allowing anyone
to log intoMariaDB without having to have a user account created for
them.Thisis 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.Bydefault,MariaDB comes with a database named 'test' that anyone can
access.Thisis 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.Thanksforusing MariaDB!
(4)配置数据库集群主节点
//编辑主节点db1虚拟机的数据库配置文件my.cnf,在配置文件my.cnf中增添下面的内容。# vi /etc/my.cnf [mysqld]
log_bin = mysql-bin //记录操作日志
binlog_ignore_db = mysql //不同步MySQL系统数据库
server_id =18//数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写18
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
//编辑完成后 重启mariadb服务# systemctl restart mariadb
(5)开放主节点的数据库权限
//在主节点db1虚拟机上使用mysql命令登录MariaDB数据库,授权在任何客户端机器上可以以root用户登录到数据库。# mysql -uroot -p123456Welcome to the MariaDB monitor.Commands end with ; or \g.YourMariaDB connection id is137Server version:10.3.18-MariaDB-log MariaDBServer
Copyright (c)2000,2018, Oracle,MariaDBCorporationAb and others.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "123456";//在主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限,命令如下
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '123456';
(6)配置从节点db2同步主节点db1
//在从节点db2虚拟机上使用mysql命令登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名db1,master_user为在步骤(5)中创建的用户user,命令如下。# mysql -uroot -p123456Welcome to the MariaDB monitor.Commands end with ; or \g.YourMariaDB connection id is88Server version:10.3.18-MariaDB-log MariaDBServer
Copyright (c)2000,2018, Oracle,MariaDBCorporationAb and others.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='123456';//配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G; //命令并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;***************************1. row ***************************
Slave_IO_State:Waitingfor master to send event
Master_Host: db1
Master_User: user
Master_Port:3306
Connect_Retry:60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos:885
Relay_Log_File: mariadb-relay-bin.000008
Relay_Log_Pos:1169
Relay_Master_Log_File: mysql-bin.000003
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:0
Last_Error:
Skip_Counter:0
Exec_Master_Log_Pos:885
Relay_Log_Space:1749
Until_Condition:None
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed:No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0
Master_SSL_Verify_Server_Cert:No
Last_IO_Errno:0
Last_IO_Error:
Last_SQL_Errno:0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:1341 row inset(0.00 sec)
ERROR:No query specified
(7)验证主从数据库的同步功能
//先在主节点db1的数据库中创建库test,并在库test中创建表company//插入表数据。创建完成后,查看表company数据
MariaDB [(none)]> create database test;Query OK,1 row affected (0.001 sec)
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.165 sec)
MariaDB [test]> insert into company values(1,"facebook","usa");Query OK,1 row affected (0.062 sec)
MariaDB [test]>select*from company;+----+----------+------+| id | name | addr |+----+----------+------+|1| facebook | usa |+----+----------+------+1 row inset(0.000 sec)
//这时从节点db2的数据库就会同步主节点数据库创建的test库//如果可以查询到信息,就能验证主从数据库集群功能在正常运行
MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows inset(0.001 sec)
MariaDB [(none)]>select*from test.company;+----+----------+------+| id | name | addr |+----+----------+------+|1| facebook | usa |+----+----------+------+1 row inset(0.001 sec)
//先在Mycat虚拟机上使用Yum安装mariadb-client服务。//如果没有可用安装包,就先添加一个仓库再安装mariadb-client#curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash# yum install -y MariaDB-client//在Mycat虚拟机上使用mysql命令查看Mycat服务的逻辑库USERDB,因为Mycat的逻辑库USERDB对应数据库test(在部署主从数据库时已安装),所以可以查看库中已经创建的表company。# mysql -h127.0.0.1 -P8066 -uroot -p123456Welcome to the MariaDB monitor.Commands end with ; or \g.YourMySQL connection id is2Server version:5.6.29-mycat-1.6-RELEASE-20161028204710MyCat Server (OpenCloundDB)
Copyright (c)2000,2018, Oracle,MariaDBCorporationAb and others.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;+----------+| DATABASE |+----------+| USERDB |+----------+1 row inset(0.001 sec)
MySQL [(none)]> use USERDBReading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MySQL [USERDB]> show tables;+----------------+| Tables_in_test |+----------------+| company |+----------------+1 row inset(0.003 sec)
MySQL [USERDB]>select*from company;+----+----------+------+| id | name | addr |+----+----------+------+|1| facebook | usa |+----+----------+------+1 row inset(0.005 sec)
(2)用Mycat服务添加表数据
//在Mycat虚拟机上使用mysql命令对表company添加一条数据(2,"basketball","usa"),添加完毕后查看表信息
MySQL [USERDB]> insert into company values(2,"bastetball","usa");Query OK,1 row affected (0.050 sec)
MySQL [USERDB]>select*from company;+----+------------+------+| id | name | addr |+----+------------+------+|1| facebook | usa ||2| bastetball | usa |+----+------------+------+2 rows inset(0.002 sec)
(3)验证Mycat服务对数据库读写操作分离
//在Mycat虚拟机节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。可以看到所有的写入操作WRITE_LOAD数都在db1主数据库节点上,所有的读取操作READ_LOAD数都在db2主数据库节点上。# 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.153.134|3306| W |0|10|1000|6426|4|1|| dn1 | hostS1 | mysql |192.168.153.135.30|3306| R |0|0|1000|0|0|0|+----------+--------+-------+--------------------+------+------+--------+------+------+---------+-----------+------------+