Mysql安装初始化
LiuSw Lv6

Mysql安装初始化

1.安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 创建用户及用户组
groupadd mysql

# 用户 (用户名/密码)
useradd -g mysql mysql

# 授权
chown -R mysql.mysql /data/mysql

# 解开压缩包,修改目录名称为mysql
mv mysql-5.7.29-linux-glibc2.12-x86_64 mysql

cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld

echo 'export PATH=/data/mysql/bin:$PATH' >>/etc/profile

source /etc/profile

2.初始化

1
2
3
4
5
mysqld --defaults-file=/etc/my.cnf --initialize

mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data

mysqld --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data

3.生成service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cat > /lib/systemd/system/mysqld.service <<EOF
[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/var/lib/mysql/bin/mysqld

LimitNOFILE = 5000

EOF

4.配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cat >/etc/my.cnf <<EOF

[mysqld]

user=mysql

basedir=/var/lib/mysql

datadir=/var/lib/mysql/data

socket=/tmp/mysql.sock

[mysql]

socket=/tmp/mysql.sock

prompt=mysql5091_db01 [\\d]>

[client]

socket=/tmp/mysql.sock

EOF

5. 配置权限修改root密码

跳过权限验证(可选)

  • 1.修改vim /etc/my.cnf增加skip-grant-tables(修改后记得注掉,重启mysql)
  • 2.mysqld_safe –user=mysql –skip-grant-tables –skip-networking &
  • 3.mysqld –user=mysql –skip-grant-tables –skip-networking &
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
mysql -uroot -p

-- 方式1
update user SET Password=PASSWORD('root@123') where USER='root';
FLUSH PRIVILEGES;

-- 方式2
update user SET authentication_string=PASSWORD('root@123') where USER='root';
FLUSH PRIVILEGES;

-- 方式3(8.0版本)
alter user 'root'@'localhost' IDENTIFIED BY 'Root@123';
FLUSH PRIVILEGES;

alter user 'root'@'%' IDENTIFIED BY 'Root@123';
FLUSH PRIVILEGES;

-- 方式4
use mysql;
set password='Root@123';
FLUSH PRIVILEGES;

7.创建用户

1
2
3
4
5
6
7
8
9
create database 'ideal' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

create user '#userName'@'#host' identified by '#passWord';

create user 'root'@'%' identified by 'Root@123';

CREATE USER 'root'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'Root@123';

FLUSH PRIVILEGES;

8.授权

root账户中的host项是localhost表示该账号只能进行本地登录,我们需要修改权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- 方式1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root@123' WITH GRANT OPTION;
FLUSH PRIVILEGES;


-- 方式2(8.0版本)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

-- 方式3(8.0版本)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

-- 方式4(8.0版本)
use mysql;
select user,authentication_string,host from user;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES;

 评论