在ubuntu上安装MYSQL
- 打开终端,输入命令:
sudo apt-get install mysql-server
,安装mysql-server; - 查看mysql-server是否安装成功,输入命令:
sudo netstat -tap | grep mysql
,如果出现以下内容,则安装成功:(或者输入sudo service mysql status
)
tcp 0 0 localhost:mysql *:* LISTEN 2023/mysqld
- 使用root用户登录mysql,输入命令:
sudo mysql -u root -p123456
; - 查看mysql的版本,输入命令:
mysql --version
。
MYSQL基本设置
sudo mysql_secure_installation
是一个用于加强MySQL安全性的命令。它会引导您完成一系列安全设置,包括设置root密码、删除匿名用户、禁用远程root登录等。
重置密码
- 停止mysql服务:
sudo service mysql stop
; - 进入mysql配置文件的目录:
cd /etc/mysql/
,发现mysql.cnf文件; - 修改mysql.cnf文件:
sudo vim mysql.cnf
,没有找到[mysqld];
mysql.cnf内容如下:
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
- 说明应该是mysql.cnf文件被分割成了多个文件,所以需要修改
/etc/mysql/mysql.conf.d/mysqld.cnf
文件; - 修改
/etc/mysql/mysql.conf.d/mysqld.cnf
文件,找到[mysqld],在下面添加skip-grant-tables
,保存退出; - 在终端输入命令:
sudo service mysql start
,启动mysql服务,然后再输入命令:sudo mysql
,回车后提示输入密码,直接回车,进入mysql; - 进入mysql后,输入命令:
use mysql;
,回车后提示Database changed
,说明已经进入mysql数据库; - 修改root用户的密码,输入命令:
update user set authentication_string=password('123456') where user='root';
,将root用户密码修改为123456; - 刷新权限,输入命令:
flush privileges;
,回车后提示Query OK, 0 rows affected (0.00 sec)
,说明刷新权限成功; - 退出mysql,输入命令:
exit;
,回车后提示Bye
,说明退出mysql成功; - 重启mysql服务,输入命令:
sudo service mysql restart
,回车后提示mysql stop/waiting
,说明mysql服务已经停止,然后再输入命令:sudo service mysql start
,回车后提示mysql start/running, process 2023
,说明mysql服务已经启动;
MYSQL基本操作
创建数据库
进入数据库:
mysql -uroot -p123456
create database test;
show databases;
USE test;
show tables;
删除数据库
drop database test;
创建表
CREATE mywind_raw; # 创建表`mywind_raw`
# or specific the name and constrain of the column
CREATE TABLE StudentsInfo(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
age INT(11) NOT NULL,
PRIMARY KEY(id)
);
在名为”music”的数据库中创建一个名为”song_info”的表格,并包含”歌曲名”、“歌手信息”、”专辑”和”发行年份”这四个字段:(使用VARCHAR类型,最大长度为255;发行年份,使用INT类型)
USE music;
CREATE TABLE song_info (
song_name VARCHAR(255),
artist_info VARCHAR(255),
album VARCHAR(255),
release_year INT
);
# 查看表的定义
desc song_info;
# +--------------+--------------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +--------------+--------------+------+-----+---------+-------+
# | song_name | varchar(255) | YES | | NULL | |
# | artist_info | varchar(255) | YES | | NULL | |
# | album | varchar(255) | YES | | NULL | |
# | release_year | int | YES | | NULL | |
# +--------------+--------------+------+-----+---------+-------+
# 4 rows in set (0.01 sec)
虽然desc 命令可以查看表定义,但是其输出的信息还是不够全面,为了查看更全面的表定 义信息,有时就需要通过查看创建表的SQL 语句来得到:
SHOW CREATE TABLE song_info \G;