注linux基础命令:Linux 命令大全 | 菜鸟教程

1.下载mysql

官网地址:MySQL :: Download MySQL Community Server 

注意:删除原mysql

# find / -name mysql
# rm -rf

2.解压压缩包

(解压到当前文件夹)
# tar -xzvf /user/local/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
//修改文件夹名称
# mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz/ mysql

3.创建用户和组

# groupadd mysql
# useradd -r -g mysql -s /bin/false mysql

4.创建目录并授权

# mkdir /var/lib/mysql
# mkdir /usr/local/mysql/log
# mkdir /usr/local/mysql/data
# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql:mysql /var/lib/mysql

5.添加环境变量

//命令
# vim ~/.bash_profile
//配置
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
source ~/.bash_profile
  1. Linux的环境变量是保存在变量PATH中,可通过Linux shell命令 echo $PATH 查看输出内容,或者直接输入export查看。
  2. Linux环境变量值之间是通过冒号进行隔开的( : )
    1. 格式为:PATH=$PATH:<PATH 1>:<PATH 2>:<PATH 3>:------:<PATH N> 
6.配置参数文件
//命令
# vim /etc/myf

//配置
[mysql]
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock
[mysqld]
port = 3306
socket=/var/lib/mysql/mysql.sock
basedir=/usr/local/mysql
character-set-server=utf8mb4
default-storage-engine=INNODB
innodb_buffer_pool_size = 200M
max_allowed_packet=16M
explicit_defaults_for_timestamp=1
log-output=FILE
general_log = 0
general_log_file=/usr/local/mysql/log/liandodb_general.err
slow_query_log = ON
slow_query_log_file=/usr/local/mysql/log/liandodb_query.err
long_query_time=10
log-error=/usr/local/mysql/log/liandodb_error.err
default-authentication-plugin=mysql_native_password
lower_case_table_names=1
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
expire_logs_days=5

7.初始化数据库

# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

8.查看初始化日志

[root@liandoyun mysql]# cd /usr/local/mysql/log/
[root@liandoyun log]# ls
liandodb_error.err  liandodb_query.err
[root@liandoyun log]# more liandodb_error.err 
2020-05-15T03:09:59.555661Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process
 1834
2020-05-15T03:09:59.623225Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-15T03:10:03.164817Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-15T03:10:08.144133Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: txu2t6_EGBH:
//txu2t6_EGBH:初始化默认登录密码

9.配置mysql服务

# vim /usr/lib/systemd/system/mysqld.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/myf
LimitNOFILE = 65536
LimitNPROC = 65536

# systemctl daemon-reload

10.测试服务

# systemctl stop mysqld
# systemctl start mysqld
# ps -ef | grep mysqld
//设置开机启动
# systemctl enable mysqld
//查看服务状态
# systemctl status mysqld

/**
*手动启停命令:
*/

启动:
nohup /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/myf &
停止:
mysqladmin -uroot -p shutdown -S /var/lib/mysql/mysql.sock

11.修改root口令

# mysql -u root -p 
Enter password: txu2t6_EGBH

mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.07 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> select host,user from user where user='root';
+-----------+------+
| host      | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)

mysql> create USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.07 sec)

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

*报错信息(报错了建议删了重装节约时间)


2022-02-26T06:23:25.446993Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2022-02-26T06:23:25.447066Z 0 [ERROR] [MY-013236] [Server] The designated data directory /usr/local/mysql/data/ is unusable. You can remove all files that the server added to it.
2022-02-26T06:23:25.447156Z 0 [ERROR] [MY-010119] [Server] Aborting
2022-02-26T06:38:31.984194Z 0 [ERROR] [MY-010187] [Server] Could not open file '/usr/local/mysql/log/liandodb_error.err' for error logging: Permission denied
2022-02-26T06:38:31.984261Z 0 [ERROR] [MY-013455] [Server] The newly created data directory /usr/local/mysql/data/ by --initialize is unusable. You can remove it.
2022-02-26T06:38:31.984272Z 0 [ERROR] [MY-010119] [Server] Aborting
root@ecs-d9a7-0001 log]# more liandodb_error.err
2022-02-26T06:51:43.870604Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2022-02-26T06:51:43.870676Z 0 [Warning] [MY-011068] [Server] The syntax 'expire-logs-days' is deprecated and will be removed in a future release. Please use binlog_expire_logs_seconds instead.
2022-02-26T06:51:43.870765Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
2022-02-26T06:51:43.870785Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 31651
2022-02-26T06:51:43.881199Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-02-26T06:51:44.391702Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-02-26T06:51:45.394167Z 0 [Warning] [MY-010161] [Server] You need to use --log-bin to make --expire_logs_days work.
2022-02-26T06:51:46.457613Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +aCkFk1PaA)u
2022-02-26T07:10:39.098517Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2022-02-26T07:10:39.098706Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2022-02-26T07:10:39.098769Z 0 [ERROR] [MY-010119] [Server] Aborting

参考:centos7.7 安装 mysql8.0.20 

centos7.7 安装 mysql8.0.20

如遇到mysql端口不正常参考

为什么MySQL端口号为0_mysql查看端口为0_刚刚v不不不的博客-CSDN博客

用 mysqldump 导出数据库

mysqldump --socket=/var/lib/mysql/mysql.sock -uroot -pPassword database --default-character-set=utf8>/home/database .sql;

导出 数据库中的表

mysqldump --socket=/var/lib/mysql/mysql.sock -uroot -pPassword database tables --default-character-set=utf8>/home/database .sql;

导入

set names utf8

source /home/database.sql

修改 字段为空

ALTER TABLE 表名 CHANGE COLUMN 字段名 字段名 数据类型 NOT NULL;

alter table topic modify state int(4) null;

更多推荐

Linux 安装 mysql