One 、 Create a new database
clickhouse The syntax for creating a database is almost the same as other relational databases , The difference is clickhouse Cluster exists cluster And library engine engine The concept of , You can specify as needed . If there is no special need , The default can be .
CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster] [ENGINE = engine(...)]
When creating a database , We first need to confirm the directory where the database files are saved , about clickhouse The user has read and write permission . If you install , The database file directory has not been modified , The default directory is /var/lib/clickhouse/. We can use the following command , Change the owner of this directory and its subdirectories to clickhouse user .
chown clickhouse /var/lib/clickhouse/ -R
After completing the above steps , You can go through clickhouse-client -m --password < Your password > Connect clichouse Service instance , Create database's SQL as follows :
:) CREATE DATABASE acaidb;
After creating the database , Use show databases; Command to see the current clickhouse, What are they? database.(acaidb We built it ourselves ,default and system yes clickhouse Created by default database)
:) show databases;
┌─name────┐
│ acaidb │
│ default │
│ system │
└─────────┘
Two 、 by default Add management authority to the account
ClickHouse Create a database by default default, A user is created by default default. We are now targeting default Users increase access management permission , Because the user does not have administrator permission by default , namely : By default , Cannot add role with this user 、 Add users 、 And other permission management operations .
So in order for default Users have more administrative authority , We modify /etc/clickhouse-server/users.xml file , stay users-> default In the label , take access_management Remove comments from .( In some versions, the default value is 0, It is amended as follows 1)
After modifying the configuration file , restart clickhouse-server, Use the following command :
clickhouse restart
3、 ... and 、 Create roles and users
Create the role
Create a character acaidb_rw, This role is for the database acaidb Read and write permission .
CREATE ROLE acaidb_rw;
GRANT SELECT, INSERT, ALTER UPDATE, ALTER DELETE ON acaidb.* TO acaidb_rw;
If you let a role only allow select, Data modification is not allowed , The following statement can .
CREATE ROLE acaidb_read;
GRANT SELECT ON acaidb.* TO acaidb_read;
Create general accounts and empower
We create accounts separately reader and writer, Then put the read-only role ro Assigned to reader, Assign the read-write role to writer. such ,reader Have read-only permission ,writer Have read and write access .
Create a new database user acai, And set the password ( What follows is < Your password > Replace ). And give it to the user acai Assign read and write permission roles acaidb_rw .
CREATE USER IF NOT EXISTS acai IDENTIFIED WITH sha256_password BY '< Your password >';
GRANT acaidb_rw TO acai;
Use the following command to access clickhouse data ,show databases obtain acaidb, It shows that we have succeeded in . because acai Users can only access acaidb.
# clickhouse-client -m -u acai --password '< Your password >' --query 'show databases;'
acaidb
Recommended reading
Limited to blog space , I won't list more wonderful contents one by one , Recommended reading
《 Original boutique videos and supporting documents :springboot- Recorded 97 section ( free )》
Etc., etc.








