MySQL安装 — Ubuntu18.04

1.版本选择及下载

mysql版本:mysql-8.0.22-el7-x86_64.tar.gz
下载地址:https://downloads.mysql.com/archives/community/
这里选的是tar.gz包,没有选择ubuntu的.deb后缀的安装包
(1)选择版本
版本选择
(2)下载压缩包
tar.gz包

2.安装libaio1

安装之前安装
apt-get install libaio1
不然会报错

bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

3.安装并配置mysql

(1)创建目录并解压安装包

创建目录

cd /usr/local/
mkdir mysql

将解压后的包文件移动到/usr/local/mysql目录下

tar -zxvf mysql-8.0.22-el7-x86_64.tar.gz
mv  mysql-8.0.22-el7-x86_64  /usr/local/mysql  //将文件移动到 /usr/local/ 目录下,并更名为mysql
(2)为系统添加mysql 组和用户。
groupadd mysql
useradd -r -g mysql mysql
(3)进入 /usr/local/mysql 目录下,创建目录并修改相关权限。

先创建/usr/local/mysql/log 文件夹
并在此文件夹下创建mysqld.log日志文件
最后将当前目录改为mysql用户

cd /usr/local/mysql    //进入/usr/local/mysql 目录
mkdir log
cd log
touch mysqld.log
chown -R mysql:mysql ./  //修改当前目录为mysql用户
(4)mysql初始化操作,记录下临时密码,之后第一次登录的时候会用到。
bin/mysqld  --initialize  --user=mysql  --basedir=/usr/local/mysql/mysql-8.0.22-el7-x86_64  --datadir=/usr/local/mysql/data  //运行

cd到目录/usr/local/mysql/mysql-8.0.22-el7-x86_64下执行命令,最后会生成一个密码

root@admin1-YANYU:/usr/local/mysql/mysql-8.0.22-el7-x86_64# bin/mysqld  --initialize  --user=mysql  --basedir=/usr/local/mysql/mysql-8.0.22-el7-x86_64  --datadir=/usr/local/mysql/data
2023-05-09T06:43:27.293525Z 0 [System] [MY-013169] [Server] /usr/local/mysql/mysql-8.0.22-el7-x86_64/bin/mysqld (mysqld 8.0.22) initializing of server in progress as process 1971
2023-05-09T06:43:27.298623Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-05-09T06:43:27.671340Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-05-09T06:43:28.563551Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: CEh.z>UZ7wly
(5)my.cnf 文件

查看初始化过程中有没有自动生成my.cnf 文件,如果没有则新建一个

cd /etc/   //进入/etc/目录下
ls | grep my.cnf   //查看是否有my.cnf 文件

my.cnf 的信息如下

[mysqld]
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
max_connections=2000
max_connect_errors=10
default-storage-engine=INNODB
innodb_buffer_pool_size=2G
event_scheduler=ON

basedir=/usr/local/mysql/mysql-8.0.22-el7-x86_64
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
default_authentication_plugin=mysql_native_password

log-error=/usr/local/mysql/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid

log_bin=off
[client]
port=3306
socket=/usr/local/mysql/data/mysql.sock
(6)设置环境变量

vi /etc/profile
添加以下内容

#set mysql environment
export MYSQL_HOME=/usr/local/mysql/mysql-8.0.22-el7-x86_64
export PATH=$PATH:$MYSQL_HOME/lib:$MYSQL_HOME/bin

然后执行 source /etc/profile

(7)启动mysql

拷贝文件

cp /usr/local/mysql/support-files/mysql.server   /etc/init.d/mysql

启动mysql

service mysql start
(8)修改密码
service mysql start   //开启服务器。
mysql -uroot -p      //登录进入mysql,然后提示输入密码,输入上面初始化生成的密码

alter user 'root'@'localhost' identified by 'your_password';
(9)修改连接权限

登录mysql,并切换到mysql database,执行一下语句

use mysql;
update user set host = '%' where user = 'root';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'youpassword'; //如果执行时出现错误,则再执行一遍,或者退出再登录,再执行一遍
flush privileges; // 刷新权限

第8、9步命令执行记录

mysql> alter user  'root'@'localhost' identified by 'youpassword';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

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> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'youpassword';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'youpassword';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
(10)加入开机启动
sudo update-rc.d -f mysql defaults

4. 常见错误及解决办法

(1)启动mysql报错,安装libaio1

root@admin1:/usr/local/mysql# bin/mysqld  --initialize  --user=mysql  --basedir=/usr/local/mysql  --datadir=/usr/local/mysql/data 
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

安装日志

root@admin1:/usr/local/mysql# apt-get install libaio1
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列【新】软件包将被安装:
  libaio1
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 6,448 B 的归档。
解压缩后会消耗 30.7 kB 的额外空间。
获取:1 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 libaio1 amd64 0.3.110-5 [6,448 B]
已下载 6,448 B,耗时 11(611 B/s) 
正在选中未选择的软件包 libaio1:amd64。
(正在读取数据库 ... 系统当前共安装有 136830 个文件和目录。)
正准备解包 .../libaio1_0.3.110-5_amd64.deb  ...
正在解包 libaio1:amd64 (0.3.110-5) ...
正在设置 libaio1:amd64 (0.3.110-5) ...
正在处理用于 libc-bin (2.27-3ubuntu1.4) 的触发器 ...

(2)启动报错
没有创建mysqld.log,在usr/local/mysql/目录下创建log目录,并在log目录下创建mysqld.log,并将log目录的属主改为mysql,操作步骤参见安装步骤第三步

.2023-05-09T06:52:03.974650Z mysqld_safe error: log-error set to '/usr/local/mysql/log/mysqld.log', however file don't exists. Create writable for user 'mysql'.
* The server quit without updating PID file (/usr/local/mysql/data/mysqld.pid).

详细日志

root@admin1-YANYU:/etc/init.d# systemctl status mysql.service
● mysql.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/init.d/mysql; generated)
   Active: failed (Result: exit-code) since Tue 2023-05-09 14:52:04 CST; 13s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2183 ExecStart=/etc/init.d/mysql start (code=exited, status=1/FAILURE)

5月 09 14:52:03 admin1-YANYU systemd[1]: Starting LSB: start and stop MySQL...
5月 09 14:52:03 admin1-YANYU mysql[2183]: Starting MySQL
5月 09 14:52:03 admin1-YANYU mysql[2183]: .2023-05-09T06:52:03.974650Z mysqld_safe error: log-error set to '/usr/local/mysql/log/mysqld.log', however file don't exists. Create writable for user 'mysql'.
5月 09 14:52:04 admin1-YANYU mysql[2183]:  * The server quit without updating PID file (/usr/local/mysql/data/mysqld.pid).
5月 09 14:52:04 admin1-YANYU systemd[1]: mysql.service: Control process exited, code=exited status=1
5月 09 14:52:04 admin1-YANYU systemd[1]: mysql.service: Failed with result 'exit-code'.
5月 09 14:52:04 admin1-YANYU systemd[1]: Failed to start LSB: start and stop MySQL.

常用命令
mysql启停命令

service mysql start
service mysql stop
service mysql status
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值