我要评分
获取效率
正确性
完整性
易理解

Starting and Using Cobar

  1. Start Cobar.
    1
    2
    cd /home/cobar-server-1.2.8-SNAPSHOT/bin
    ./startup.sh
    

    Or

    1
    2
    cd /home/cobar-server-1.2.8-SNAPSHOT/bin
    ./restart.sh
    

    The Cobar process name is CobarStartup. View the process and port information or view the logs to check whether the process is started properly.

    • View the Cobar process and port information.
      1
      2
      3
      ps -ef|grep CobarStartup
      netstat -an|grep 8066
      netstat -an|grep 9066
      

    • View the logs.
      1
      2
      cd /home/cobar-server-1.2.8-SNAPSHOT/logs
      cat stdout.log
      

  2. Access Cobar.
    The method of accessing Cobar is the same as that of accessing MySQL. The general access method is as follows:
    1
    /usr/local/mysql-20/bin/mysql -h192.168.200.56 -utest -ptest -P8066 -Ddbtest
    

    In this document, Cobar is deployed on the host whose IP address is 192.168.200.56. Replace the IP address with the actual one and keep other information unchanged.

  3. Check the database and tables.
    1
    2
    show databases; # dbtest1, dbtest2, and dbtest3 are transparent to users.
    show tables; # The dbtest database contains two tables: tb1 and tb2.
    

  4. Insert and query data.
    1
    2
    3
    4
    5
    insert into tb1 (id, gmt) values (1, now()); # Insert a single data record to tb1.
    insert into tb2 (id, val) values (1, "part1"); # Insert a single data record to tb2.
    insert into tb2 (id, val) values (2, "part1"), (513, "part2"); # Insert multiple data records to tb2.
    select * from tb1; # Query the tb1 table to check whether the data insertion is successful.
    select * from tb2; # Query the tb2 table to check whether the data insertion is successful.
    

    If the SQL routing field has database shards, Cobar extracts the routing field value to determine the database shard to which the SQL statement is routed. If the SQL statement does not contain a routing field value, Cobar distributes the SQL statement to all database shards. If you split tb2 into two copies by ID and place them in different MySQL instances, and then run the following SQL statements using Cobar, the results are different.

    mysql>insert into tb2 values (1, "part1"). Insert one data record into tb2. The ID is a routing field, and Cobar cannot extract the ID field from the SQL statement. Therefore, the SQL statement is distributed to the dbtest2 and dbtest3 databases. Then a record is added to both of the two MySQL shards.

    Log in to the MySQL database through the backend.

    mysql>insert into tb2 (id, val) values (2, "part1"). Insert another data record into tb2. The routing field value extracted by Cobar is 2. The SQL statement is distributed to the corresponding shard based on the routing rule. In this way, only one MySQL shard is added at the backend.

    Log in to the MySQL database through the backend.

    Therefore, it is important to specify the sharding field in the SQL statement, and it is not advised to change the value of the sharding field of a record. If the value of the sharding field is changed, Cobar reports an error.

  5. Verify Cobar.
    1. Log in to the MySQL backend and view the MySQL databases dbtest1, dbtest2, and dbtest3 to verify that data is distributed in different databases.
      1
      /usr/local/mysql-20/bin/mysql -h192.168.200.56 -uroot -p123456
      

    2. Check the content in the tb1 table of dbtest1.
      1
      2
      3
      4
      show databases;
      use dbtest1;
      show tables;
      select * from tb1;
      

    3. Check the content in the tb2 table of dbtest2.
      1
      2
      3
      use dbtest2;
      show tables;
      select * from tb2;
      

    4. Check the content in the tb2 table of dbtest3.
      1
      2
      3
      use dbtest3;
      show tables;
      select * from tb2;
      

    The verification result is as follows:

    The database provided by the system is dbtest, which contains two tables: tb1 and tb2. The data of tb1 is mapped to the tb1 table of the dbtest1 physical database. Some data of tb2 is mapped to the tb2 table of the dbtest2 physical database, and the other data of tb2 is mapped to the tb2 table of the dbtest3 physical database.