虚拟机有网络的情况下通过apt安装mysql
本文以ubuntu用户进行安装
更新系统可用的包
sudo apt update
安装mysql-server
sudo apt install mysql-server -y
通过root用户登录mysql
sudo mysql --user=root mysql
修改root用户任何地址都可以访问
use mysql; UPDATE user SET Host='%' WHERE User='root'; flush privileges;
修改root用户密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; flush privileges; quit;
修改配置文件 sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
注释43行,让mysql可以外部访问
设置编码为utf8
character-set-server=utf8 collation-server=utf8_general_ci
重启:sudo service mysql restart
查看状态:sudo service mysql status
mysql常用命令
show variables like 'character%';
Ubuntu18.04.6 通过自编译的方式指定mysql5.7安装目录
下载mysql包
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
或者百度网盘下载
链接:https://pan.baidu.com/s/1ZiWdrSWT4qLa6C19vppvwA?pwd=1fwt
提取码:1fwt
自编译
登录到root用户
su root
更新系统包
apt -y install make cmake gcc g++ perl bison libaio-dev libncurses5 libncurses5-dev libnuma-dev libssl-dev pkg-config
添加mysql用户组和用户
groupadd mysql useradd -r -g mysql mysql
预设一些目录
rm -rf /opt/mysql mkdir -p /opt/mysql/download # 存放mysql等安装包 mkdir -p /opt/mysql/install # 存放安装后的软件 mkdir -p /opt/mysql/data # 存放数据 mkdir -p /opt/mysql/log # 存放日志 chown -R mysql:mysql /opt/mysql
将下载的mysql包放入到 /opt/mysql/download 目录下
解压
cd /opt/mysql/download/ tar -zxf mysql-boost-5.7.41.tar.gz #这里包版本可能会变
cd mysql-5.7.41/ cmake \ -DCMAKE_INSTALL_PREFIX=/opt/mysql/install \ -DMYSQL_UNIX_ADDR=/opt/mysql/install/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/opt/mysql/data \ -DMYSQL_TCP_PORT=3306 \ -DWITH_SSL=system \ -DWITH_BOOST=boost
echo $? 0 # 如果是0,预编译通过,其他数字,表示失败,自行排错 # 编译参数的参考文档:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
正式编译,耐心等待
make -j4 make install
修改安装目录的所有者和所属组
chown -R mysql:mysql /opt/mysql chgrp -R mysql /opt/mysql
初始化密码
# 初始化,生成一个临时的mysql root用户密码 /opt/mysql/install/bin/mysqld --initialize --user=mysql
# 启动mysql,修改root密码为123456 /opt/mysql/install/support-files/mysql.server start # 启动后,按回车回到命令行 /opt/mysql/install/bin/mysql -u root -p Enter Password: # 这里输入刚才生成的密码 set password for root@localhost = password('123456');
将mysql注册为系统服务
# 目前不太方便,系统没有mysql这个命令,没有用设置开机启动。 # 先停止mysql /opt/mysql/install/support-files/mysql.server stop cp /opt/mysql/install/support-files/mysql.server /etc/init.d/mysqld /etc/init.d/mysqld start # 这一步自动注册成systemd服务了,可以使用systemctl管理mysql了 chmod +x /etc/init.d/mysqld systemctl stop mysqld systemctl start mysqld systemctl enable mysqld
添加mysql、mysqldump、mysqlpump等一系列命令
vim /etc/profile # 文件末尾追加 PATH=$PATH:/opt/mysql/install/bin
验证mysql命令
source /etc/profile mysql -u root -p Enter password: 123456
重启系统
reboot
Ubuntu18.04.6 离线安装mysql5.7
百度云下载mysql包
链接:https://pan.baidu.com/s/1kbcRXY7OIEAGm2xrkz5osw?pwd=7h6p
提取码:7h6p
设置目录权限,三个包一并上传至 /opt 目录
sudo chown ubuntu:ubuntu /opt/
安装mysql
直接使用dpkg会报错,解决办法
sudo rm /var/lib/dpkg/lock sudo rm /var/lib/dpkg/lock-frontend sudo rm /var/cache/apt/archives/lock
tar -xvf mysql-server_5.7.22-1ubuntu17.10_amd64.deb-bundle.tar sudo dpkg -i mysql-common_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg-preconfigure mysql-community-server_5.7.22-1ubuntu17.10_amd64.deb #这一步设置密码 sudo dpkg -i libmysqlclient20_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg -i libmysqlclient-dev_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg -i libmysqld-dev_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg -i libaio1_0.3.110-5_amd64.deb sudo dpkg -i libmecab2_0.996-5_amd64.deb sudo dpkg -i mysql-community-client_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg -i mysql-client_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg -i mysql-common_5.7.22-1ubuntu17.10_amd64.deb sudo dpkg -i mysql-community-server_5.7.22-1ubuntu17.10_amd64.deb
至此mysql就可以登录了,后续设置参考第一章节