目录
一、安装MYSQL之前要先换源
二、安装MYSQL
1、安装
2、安装完成
3、安装后无法登陆
3.1 原因
3.2 登陆后切换database
3.3 修改密码(注意这里账号和密码是双引号)
3.4查看一下用户
3.5 然后进行刷新使得配置生效
3.6 退出
3.7 重启
三、mysql使用测试
1.备份系统之前的源,也可以不备份,将之前的源注释掉
cp /etc/apt/sources.list /etc/apt/sources.list.bak
2.修改源文件/etc/apt/sources.list
vim /etc/apt/sources.list
将原来的内容删除,将下面的源内容粘贴上。
3.这里推荐比较好的两个国内源
3.1阿里源(arm64,速度快,推荐)
deb http://mirrors.aliyun.com/ubuntu-ports/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial main
deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates main
deb http://mirrors.aliyun.com/ubuntu-ports/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial universe
deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-security universe
3.2 华为源(arm64,速度快,二者选其一)
deb https://repo.huaweicloud.com/ubuntu-ports/ bionic main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic main restricted universe multiverse
deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-security main restricted universe multiverse
deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-backports main restricted universe multiverse
## Not recommended
# deb https://repo.huaweicloud.com/ubuntu-ports/ bionic-proposed main restricted universe multiverse
# deb-src https://repo.huaweicloud.com/ubuntu-ports/ bionic-proposed main restricted universe multiverse
4.更新仓库
apt-get update
4.1处理报错
可能出现报错
由于没有公钥,无法验证下列签名...
以下命令解决:
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 40976EAF437D05B5 3B4FE6ACC0B21F32
5.清除软件仓库缓存
apt-get clean
6.更新软件仓库
apt-get update
完成,可以安装MYSQL了
sudo apt-get install mysql-server //服务端
sudo apt-get install mysql-client //客户端
sudo apt-get install libmysqlclient-dev //程序编译时链接的库
安装完成之后dpkg命令查看状态
dpkg -l | grep mysql
输出以下信息则安装成功
ii mysql-client-5.7 5.7.39-0ubuntu0.18.04.2 arm64 MySQL database client binaries
ii mysql-client-core-5.7 5.7.39-0ubuntu0.18.04.2 arm64 MySQL database core client binaries
ii mysql-common 5.8+1.0.5 all MySQL database common files, e.g. /etc/mysql/my.cnf
ii mysql-server 5.7.39-0ubuntu0.18.04.2 all MySQL database server (metapackage depending on the latest version)
ii mysql-server-5.7 5.7.39-0ubuntu0.18.04.2 arm64 MySQL database server binaries and system database setup
ii mysql-server-core-5.7 5.7.39-0ubuntu0.18.04.2 arm64 MySQL database server binaries
使用如下命令启动mysql
service mysql start
启动后使用如下命令查看状态
sudo netstat -anp | grep mysql
显示如下说明启动成功:
tcp6 0 0 :::3306 :::* LISTEN 27608/mysqld
unix 2 [ ACC ] STREAM LISTENING 648125 27608/mysqld /var/run/mysqld/mysqld.sock
unix 3 [ ] STREAM CONNECTED 648111 27608/mysqld
使用命令进行登陆
mysql -u root -p
输入密码,进行登陆,如果之前没有设置密码请看第3步
aaron@aaron-Raytine-PC:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)
Copyright © 2000, 2020, 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>
原因一(大部分):
你是root安装的: 在本机命令行下输入: mysql -uroot 就可以直接登录,然后修改用户名密码啥的
原因二:
使用mysql -u root -p命令但是不知道root密码是因为之前没有设置,这个时候进行环境配置,设置root密码。
1、首先,进行忽略密码登陆,打开配置文件
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
2、在最后加入skip-grant-tables
# # Read the manual, too, if you want chroot! # chroot = /var/lib/mysql/ # # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". # # ssl-ca=/etc/mysql/cacert.pem # ssl-cert=/etc/mysql/server-cert.pem # ssl-key=/etc/mysql/server-key.pem skip-grant-tables -- 插入 --
3、保存退出
4、重启以下mysql服务
sudo service mysql stop
sudo service mysql start
5、重新登陆
mysql -u mysql
进行3.2步骤,所有步骤执行完注释掉skip-grant-tables
原因三:没有把之前的mysql卸载干净
彻底删除MySQL
在删除mysql前 须要先删除一下 /var/lib/mysql 还有 /etc/mysqlubuntu
先停止mysql服务
systemctl stop mysql
须要输入如下几条命令
sudo rm /var/lib/mysql/ -R sudo rm /etc/mysql/ -R sudo apt-get autoremove mysql* --purge sudo apt-get remove apparmor
确保没有mysql的文件夹可忽略此步骤
sudo find / -name mysql
再次安装
sudo apt-get install mysql-server //服务端 sudo apt-get install mysql-client //客户端 sudo apt-get install libmysqlclient-dev //程序编译时链接的库
登录
~$ sudo mysql -u root -p
Enter password:
直接回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.39-0ubuntu0.18.04.2 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
换为database
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> update user set authentication_string=PASSWORD("123456") where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
select user,host from mysql.user;
出现以下信息则配置完成
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
flush privileges;
quit
sudo service mysql restart
1、建立一个数据库
mysql> create database joyrun;
Query OK, 1 row affected (0.00 sec)
2、查看当前数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| joyrun |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
3、建立一个表
mysql> use joyrun
Database changed
mysql> create table tbl_run(id int,name varchar(5),primary key (id));
Query OK, 0 rows affected (0.01 sec)
4、查看表的结构
mysql> desc tbl_run;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(5) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
5、修改字节长度
mysql> alter table tbl_run modify column name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tbl_run;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
6、现在表中不能添加中文字段,修改字符集后才可以
修改库字符集:alter database joyrun character set utf8;
修改表字符集:alter table tbl_run character set utf8;
修改字段字符集:alter table tbl_run change name name varchar(20) character set utf8;
7、增加一条数据
mysql> insert into tbl_run (id,name) values (1,'xiaoming');
Query OK, 1 row affected (0.01 sec)
mysql> select * from tbl_run;
+----+----------+
| id | name |
+----+----------+
| 1 | xiaoming |
+----+----------+
1 row in set (0.00 sec)