一、通过apt安装mysql

#更新源
sudo apt-get update
sudo apt-get upgrade
#安装mysql
sudo apt-get install mysql-server
1、初始化配置

sudo mysql_secure_installation

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (选择N ,不会进行密码的强校验)

##2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

##3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (选择N,不删除匿名用户)

##4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N (选择N,允许root远程连接)

##5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (选择N,不删除test数据库)

##6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (选择Y,修改权限立即生效)
2、检查mysql状态

systemctl status mysql.service

二、配置远程访问

找到 bind-address 修改值为 0.0.0.0(如果需要远程访问)

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf #找到 bind-address 修改值为 0.0.0.0(如果需要远程访问)
systemctl restart mysql.service  #重启
sudo mysql -uroot -p
#切换数据库
mysql> use mysql;
#查询用户表
mysql> select user,authentication_string,host from user;
#查看状态
mysql> select host,user,plugin from user;
#设置权限与密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码'; #使用mysql_native_password修改加密规则
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '密码' PASSWORD EXPIRE NEVER; #更新一下用户的密码
mysql> UPDATE user SET host = '%' WHERE user = 'root'; #允许远程访问
#刷新cache中配置 刷新权限
mysql> flush privileges;

FLUSH PRIVILEGES;作用

将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。
MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。
通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。
而不必冒太大风险。

新增用户赋权并设置远程访问

mysql8和原来的版本有点不一样,8的安全级别更高,所以在创建远程连接用户的时候,不能用原来的命令(同时创建用户和赋权)

##必须先创建用户(密码规则:mysql8.0以上密码策略限制必须要大小写加数字特殊符号)
mysql> CREATE USER 'sammy'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
##赋权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'%' WITH GRANT OPTION;

修改加密方式

mysql8.0 引入了新特性 caching_sha2_password;这种密码加密方式Navicat 12以下客户端不支持;Navicat 12以下客户端支持的是mysql_native_password这种加密方式.

mysql> update user set plugin='mysql_native_password' where user='root'

三、数据库操作命令

1、mysql服务操作

#登录数据库
mysql -uroot -p

#查看数据库版本
mysql> status;

#退出mysql
mysql> quit;

#启动服务
service mysql start

#停止服务
service mysql stop

#重启服务
service mysql restart

#更改密码:mysqladmin -u用户名 -p旧密码 password新密码
mysql> mysqladmin -uroot -proot password 123456

#增加新用户:grant all privileges on 数据库.* to 用户名@登录主机 identified by 密码
mysql> grant all privileges on *.* to root@"%" identified by "pwd" with grant option;

#增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了
mysql> grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";

#如果你不想test2有密码,可以再打一个命令将密码消掉
mysql> grant select,insert,update,delete on mydb.* to test2@localhost identified by "";

#查看字符集
mysql> show variables like 'character%';

2、数据库操作

#创建数据库
mysql> create database 数据库名 charset=utf8;

#删除数据库
mysql> drop database 数据库名;

#切换数据库
mysql> use 数据库名;

#查看当前选择的数据库
mysql> select database();

#列出所有数据库
mysql> show databases;

3、表操作

#查看当前数据库中的所有表
mysql> show tables;

#创建表
mysql> create table 表名(列及类型);
如:
create table students(
    id int auto_increment primary key,
    sname varchar(10) not null
);
#auto_increment表示自动增长

#修改表
mysql> alter table 表名 add|change|drop 列名 类型;
如:
alter table students add birthday datetime;

#删除表
mysql> drop table 表名;

#查看表结构
mysql> desc 表名;

#更改表名
mysql> rename table 原表名 to 新表名;

#查看建表语句
mysql> show create table '表名';

4、修改表结构

#更改表得的定义把某个栏位设为主键
ALTER TABLE tab_name ADD PRIMARY KEY (col_name)

#把主键的定义删除
ALTER TABLE tab_name DROP PRIMARY KEY (col_name)

#在tab_name表中增加一个名为col_name的字段且类型为varchar(20)
alter table tab_name add col_name varchar(20);

#在tab_name中将col_name字段删除
alter table tab_name drop col_name;

#修改字段属性,注若加上not null则要求原字段下没有数据
alter table tab_name modify col_name varchar(40) not null;

#修改表名
alter table tab_name rename to new_tab_name;

#修改字段名
alter table tab_name change old_col new_col varchar(40);

#用一个已存在的表来建新表,但不包含旧表的数据
create table new_tab_name like old_tab_name;

5、数据操作

查询
select * from 表名
增加
全列插入:insert into 表名 values(…)
缺省插入:insert into 表名(列1,…) values(值1,…)
同时插入多条数据:insert into 表名 values(…),(…)…;
或insert into 表名(列1,…) values(值1,…),(值1,…)…;
主键列是自动增长,但是在全列插入时需要占位,通常使用0,插入成功后以实际数据为准
修改
update 表名 set 列1=值1,… where 条件
删除
delete from 表名 where 条件
逻辑删除,本质就是修改操作update
alter table students add isdelete bit default 0;
如果需要删除则
update students isdelete=1 where …;

6、卸载

dpkg --list|grep mysql        #在终端中查看MySQL的依赖项
sudo apt-get remove mysql-common  #卸载
sudo apt-get autoremove --purge mysql-server-8.0
##sudo apt-get autoremove --purge mysqlxxx
清理残留数据
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
再次查看MySQL的剩余依赖项
dpkg --list|grep mysql

继续删除剩余依赖项,如:sudo apt-get autoremove --purge mysql-apt-config

删除原先配置文件
sudo rm -rf /etc/mysql/ /var/lib/mysql
sudo apt autoremove
sudo apt autoreclean(如果提示指令有误,就把reclean改成clean)
END
Last modification:August 1st, 2021 at 08:00 pm
如果觉得我的文章对你有用,请随意赞赏