mysql 服务
任务描述:请安装 mysql 服务,建立数据表。
(1)配置 linux2 为 mysql 服务器,创建数据库用户 xiao,在任意机器上对所有数据库有完全权限。
在 Linux 服务器上执行以下命令安装 MySQL 服务器:重启 MySQL 服务器:
yum install mysql* -y
systemctl start mysqld.service
登录到 MySQL 服务器:
mysql -u root -p
创建用户和授予权限:执行以下命令以创建用户 xiao 和授予权限:
CREATE USER 'xiao'@'%' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'xiao'@'%';
FLUSH PRIVILEGES;
将 'your_password' 替换为您为用户 xiao 设置的密码。
(2)创建数据库 userdb;在库中创建表 userinfo,表结构如下:
mysql> create database userdb;
Query OK, 1 row affected (0.01 sec)
mysql> use userdb;
mysql> create table userinfo(
-> id int primary key auto_increment,
-> name varchar(10),
-> birthday datetime,
-> sex varchar(5),
-> password varchar(200)) character set='utf8';
Query OK, 0 rows affected (0.02 sec)
mysql> desc userinfo
;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| birthday | datetime | YES | | NULL | |
| sex | varchar(5) | YES | | NULL | |
| password | varchar(200) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(3)在表中插入 2 条记录,分别为(1,user1,1999-07-01,男),(2,user2,1999-07-02,女),password 与 name 相同,password 字段用 password 函数加密。
INSERT INTO userinfo (id, name, birthday, sex, password)
VALUES (1, 'user1', '1999-07-01', '男', PASSWORD('user1')),
(2, 'user2', '1999-07-02', '女', PASSWORD('user2'));
可能会报以下错误,
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('user1'))' at line 2
错误 1064 (42000):SQL 语法中有错误;检查与您的MySQL服务器版本相对应的手册,了解在第2行的“('user1'))”附近使用的正确语法
可能就是本版问题需要重新安装其他版本的mysql
(4)修改表 userinfo 的结构,在 name 字段后添加新字段 height(数据类型为 float),更新 user1 和 user2 的 height 字段内容为 1.61 和 1.62。
mysql> alter table userinfo add height float ;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update userinfo set height=1.61 where id =1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update userinfo set height=1.62 where id =2;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(5)每周五凌晨 1:00 以 root 用户身份备份数据库 userdb 到 /var/databak/userdb.sql(含创建数据库命令)。
Crontab -e
0 1 * * 5 mysqldump -u root -p '123456' userdb > /var/databak/userdb.sql