数据库
四、数据库操作(linux)
安装数据库
sudo apt install -y mysql-server mysql-client
开启数据库服务
- Ubuntu :
service mysql start|stop|restart|status
- Deepin :
systemctl start|stop|restart|status mysqld
- CentOS7 :
systemctl start|stop|restart|status mysqld
- CentOS6 :
service mysqld start|stop|restart|status
连接数据库
各个 Linux 系统连接数据库都⼀样 语法: mysql -hloaclhost -uroot -p123456 -P3306
- -h :
host(ip地址) localhost = 127.0.0.1
- -u :
username(⽤⼾账⼾)
- -p :
password(密码)
- -P :
port(端⼝, 默认端⼝3306)
备注: 第⼀次使⽤ root 连接后最好添加⼀个新的⽤⼾来操 作。出于安全考虑,⽇常开发中最好不要使⽤ root。 root⽤⼾⼀般是管理员使⽤的。
创建新用户
-- 创建新⽤⼾,并设置密码
-- *.* 代表该⽤⼾可以操作任何库、任何表
-- 主机名可以使⽤ '%', 代表允许该⽤⼾从任何机器登陆
GRANT ALL PRIVILEGES on *.* to '⽤⼾名'@'主机'
IDENTIFIED BY "密码" WITH GRANT
OPTION;
-- 刷新使权限⽣效
flush privileges;
退出数据库
四种⽅式效果⼀样:
- exit
- quit
- \q
- 快捷键: ctrl + d
密码忘记
-
打开配置: vim /etc/my.cnf
-
添加这么⼀段:
[mysqld] skip-grant-tables
#如果⽂件中已存在 [mysqld] , 则直接将 skip-grant-tables 写到其下⽅即可。
- 修改完成后,保存退出,重启服务:
sudo systemctl restart mysqld