centos8系统上面的库和应用都是最新的。默认的数据库版本是mysql8
以下操作需要centos8系统
安装mysql8
[root@bogon ~]# yum install mysql mysql-devel -y
Last metadata expiration check: 0:41:46 ago on Thu 05 Mar 2020 08:46:54 PM EST.
Package mysql-8.0.17-3.module_el8.0.0+181+899d6349.x86_64 is already installed.
Package mysql-devel-8.0.17-3.module_el8.0.0+181+899d6349.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@bogon ~]#
查看安装是否成功
[root@bogon ~]# yum list mysql-server
Last metadata expiration check: 0:36:17 ago on Thu 05 Mar 2020 08:46:54 PM EST.
Installed Packages
mysql-server.x86_64 8.0.17-3.module_el8.0.0+181+899d6349 @AppStream
[root@bogon ~]# yum list mysql
Last metadata expiration check: 0:36:20 ago on Thu 05 Mar 2020 08:46:54 PM EST.
Installed Packages
mysql.x86_64 8.0.17-3.module_el8.0.0+181+899d6349 @AppStream
[root@bogon ~]#
修改root密码
在MySQL 8.04前,执行:SET PASSWORD=PASSWORD(‘[新密码]’);但是MySQL8.0.4开始,这样默认是不行的。因为之前,MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。
修改密码为123456(密码可以设置为任意值):
[root@bogon ~]# mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@bogon ~]#
用密码进入mysql命令行
[root@bogon ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
创建数据库、数据表
mysql> CREATE TABLE `uid`
-> (
-> `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
-> `business_id` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '业务id',
-> `max_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '最大id',
-> `step` int(10) unsigned NOT NULL DEFAULT '1000' COMMENT '步长',
-> `description` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '描述',
-> `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
-> `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
-> PRIMARY KEY (`id`),
-> UNIQUE KEY `uk_business_id` (`business_id`)
-> ) ENGINE = InnoDB
-> DEFAULT CHARSET = utf8mb4
-> COLLATE = utf8mb4_bin COMMENT ='分布式自增主键';
Query OK, 0 rows affected, 3 warnings (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| uid |
+----------------+
1 row in set (0.00 sec)
mysql> desc uid;
+-------------+---------------------+------+-----+-------------------+-----------------------------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+-------------------+-----------------------------------------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| business_id | varchar(128) | NO | UNI | NULL | |
| max_id | bigint(20) unsigned | NO | | 0 | |
| step | int(10) unsigned | NO | | 1000 | |
| description | varchar(255) | NO | | NULL | |
| create_time | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
| update_time | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+-------------+---------------------+------+-----+-------------------+-----------------------------------------------+
7 rows in set (0.01 sec)
mysql>
插入数据
mysql> insert into uid (business_id, max_id, step, description, create_time, update_time)
-> values
-> ('device_id', 1580, 5, '设备id', '2020-1-1 7:00:00', '2020-1-1 12:00:00');
Query OK, 1 row affected (0.02 sec)
查看数据
mysql> select * from uid;
+----+-------------+--------+------+-------------+---------------------+---------------------+
| id | business_id | max_id | step | description | create_time | update_time |
+----+-------------+--------+------+-------------+---------------------+---------------------+
| 1 | device_id | 1580 | 5 | 设备id | 2020-01-01 07:00:00 | 2020-01-01 12:00:00 |
+----+-------------+--------+------+-------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql>