1. 如何得知linux系统的版本信息,并选择对应兼容的MySQL?
终端输入命令如下:
uname -srm
例如,得到版本信息:Linux 4.4.0-184-generic x86_64
相应地对系统版本信息分析如下:
Linux系统版本是4.4.0-184-generic,架构是x86_64。
---从而得知使用的Ubuntu 16.04 LTS(Xenial Xerus)
对于这个版本的Linux,可选择安装MySQL5.7,与Ubuntu16.04兼容性较好。
2. 安装对应系统版本的mysql(MySQL5.7)
方法1 因为使用的是Ubuntu的Linux发行版,可通过命令直接安装软件包
步骤如下:
(1)更新软件包列表
sudo apt update
(2)安装MySQL服务器和客户端软件包
sudo apt install mysql-server-5.7
出现如下的结果:
tomexam3_free.tm_system_log OK
tomexam3_free.tm_user OK
tomexam3_free.tm_user_addition OK
tomexam3_free.tm_user_collection OK
tomexam3_free.tm_user_collection_type OK
tomexam3_free.tm_user_position OK
tomexam3_free.tm_usertest OK
tomexam3_free.tm_vote OK
tomexam3_free.tm_vote_log OK
Upgrade process completed successfully.
Checking if update is needed.。
(3)安装完后,MySQL服务器将会自动启动,检查是否运行(出现active、running等字眼)
sudo systemctl status mysql
方法2 官网下载对应的安装包,然后解压安装和配置
步骤如下:
(1)访问MySQL官网:https://www.mysql.com/downloads/
(2)选择“MySQL Community(GPL) Downloads”,进入
(3)选择“MySQL Community Server",进入如下页面,并按照Linux系统选择如下MySQL
(4)下载第一个“Compressed TAR Archive"版本, 点击”Downloads“
(5)解压安装包
tar xvf mysql-<对应版本>-linux-glibc2.12-x86_64.tar.gz
(6)将解压后的文件夹移动到 /usr/local 目录下
sudo mv mysql-<对应版本>-linux-glibc2.12-x86_64 /usr/local/mysql
(7)进入 /usr/local/mysql 目录
cd /usr/local/mysql
(8)创建一个新的MySQL数据目录
sudo mkdir mysql-data
(9)初始化MySQL数据库
sudo bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysql-data
出现如下提示:
2023-07-26T09:44:17.960457Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-07-26T09:44:18.035804Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-07-26T09:44:18.050062Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-07-26T09:44:18.102469Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: fd602662-2b98-11ee-aafb-509a4c616806.
2023-07-26T09:44:18.103033Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-07-26T09:44:19.130937Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-07-26T09:44:19.130943Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-07-26T09:44:19.134563Z 0 [Warning] CA certificate ca.pem is self signed.
2023-07-26T09:44:19.558017Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option
(10)启动MySQL服务器
sudo bin/mysqld_safe --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysql-data &
(11)检查MySQL服务器是否已经成功启动
sudo bin/mysqladmin -u root version
(12)这里安装过程中,未出现密码的设置,使用如下命令以root身份登录到MySQL服务器
sudo bin/mysql -u root
(13)在MySQL提示符下,更改root用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' //密码设置为123456,可对应替换
(14)退出MySQL提示符
exit
3. 输入mysql -u root -p出现报错
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
前提:MySQL可执行文件位于/usr/local/mysql/bin目录中,如果MySQL安装路径不同,需要相应修改命令中的路径
方法1:进入mysql安装的路径文件夹下:/usr/local/mysql/,输入: sudo bin/mysql -u root -p,输入对应password,然后成功登录
方法2:将mysql的可执行文件目录添加至系统的PATH环境变量中,这样可以在任何位置直接使用mysql命令:
(1)编辑 ~/.bashrc 文件
sudo vim ~/.bashrc
(2)在文件的末尾添加如下:
export PATH="/usr/local/mysql/bin:$PATH"
1. 注意 ~/.bashrc文件中若存在其他export PATH="......",再添加export PATH="/usr/local/mysql/bin:$PATH"可能会导致冲突,解决方法是将两个进行合并,如:
export PATH="/usr/local/mysql/bin:/usr/local/node/bin:$PATH"
2. 不同于Windows系统,在Unix和Linux系统中,用于分隔多个路径的字符是冒号而不是分号,因此在上述export PATH语句中,应使用冒号来分隔多个路径。
3. 在多数情况下,将路径放在引号中,特别是当路径中包含空格或其他特殊字符时,这样可确保路径被正确解析和传递给系统;但如果路径中不包含空格或特殊字符,通常可以省略引号,因此上述也可这样添加:
export PATH=/usr/local/mysql/bin:/usr/local/node/bin:$PATH
(3)保存并关闭文件: esc键 + w + q,并使用以下命令使更改生效:
source ~/.bashrc
(4)最后,可直接使用sudo mysql -u root -p 命令登录MySQL,而前面不需要输入bin/了