Creating your first Tarantool database
Example on GitHub: create_db
In this tutorial, you create a Tarantool database, write data to it, and select data from this database.
Before starting this tutorial:
The tt create command can be used to create an application from a predefined or custom template. In this tutorial, the application layout is prepared manually:
- Create a tt environment in the current directory using the tt init command.
- Inside the
instances.enableddirectory of the created tt environment, create thecreate_dbdirectory. - Inside
instances.enabled/create_db, create theinstances.ymlandconfig.yamlfiles:instances.ymlspecifies instances to run in the current environment. In this example, there is one instance:
instance001:
- `config.yaml` contains basic instance[configuration](../../platform/configuration#configuration_file):
groups:group001:replicasets:replicaset001:instances:instance001:iproto:listen:- uri: '127.0.0.1:3301'
The instance in the configuration accepts incoming requests on the`3301` port.
-
Start the Tarantool instance from the tt environment directory using tt start:
$ tt start create_db -
To check the running instance, use the tt status command:
$ tt status create_dbINSTANCE STATUS PID MODE CONFIG BOX UPSTREAMcreate_db:instance001 RUNNING 8685 RW ready running -- -
Connect to the instance with tt connect:
$ tt connect create_db:instance001• Connecting to the instance...• Connected to create_db:instance001create_db:instance001>This command opens an interactive Tarantool console with the
create_db:instance001>prompt. Now you can enter requests in the command line.
-
Create a space named
bands:create_db:instance001> box.schema.space.create('bands')---- engine: memtxbefore_replace: 'function: 0x010229d788'field_count: 0is_sync: falseis_local: falseon_replace: 'function: 0x010229d750'temporary: falseindex:type: normalenabled: falsename: bandsid: 512- created... -
Format the created space by specifying
fieldnames and types:create_db:instance001> box.space.bands:format({{ name = 'id', type = 'unsigned' },{ name = 'band_name', type = 'string' },{ name = 'year', type = 'unsigned' }})---...
-
Create the primary index based on the
idfield:create_db:instance001> box.space.bands:create_index('primary', { parts = { 'id' } })---- unique: trueparts:- fieldno: 1sort_order: asctype: unsignedexclude_null: falseis_nullable: falsehint: trueid: 0type: TREEspace_id: 512name: primary... -
Create the secondary index based on the
band_namefield:create_db:instance001> box.space.bands:create_index('secondary', { parts = { 'band_name' } })---- unique: trueparts:- fieldno: 2sort_order: asctype: stringexclude_null: falseis_nullable: falsehint: trueid: 1type: TREEspace_id: 512name: secondary...
-
Insert three tuples into the space:
create_db:instance001> box.space.bands:insert { 1, 'Roxette', 1986 }---- [1, 'Roxette', 1986]...create_db:instance001> box.space.bands:insert { 2, 'Scorpions', 1965 }---- [2, 'Scorpions', 1965]...create_db:instance001> box.space.bands:insert { 3, 'Ace of Base', 1987 }---- [3, 'Ace of Base', 1987]... -
Select a tuple using the
primaryindex:create_db:instance001> box.space.bands:select { 3 }---- - [3, 'Ace of Base', 1987]... -
Select tuples using the
secondaryindex:create_db:instance001> box.space.bands.index.secondary:select{'Scorpions'}---- - [2, 'Scorpions', 1965]...