安装配置Mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装配置Mysql相关的知识,希望对你有一定的参考价值。

搭建配置musql

方法一(适用ubuntu)
编译安装mysql

1、安装依赖环境
[email protected]:~$ sudo apt-get install make cmake gcc g++ bison libncurses5-dev build-essential
说明:make、cmake、gcc、g++、bison、libncurses5-dev、build-essential都是安装mysql数据库所需要的依赖环境。

2、下载mysql源码包并解压
[email protected]:~$ ls
mysql-5.1.63.tar.gz mysql-5.1.63

3、编译安装
[email protected]:~$ cd mysql-5.1.63/
[email protected]:~/mysql-5.1.63$ ./configure --prefix=/usr/local/mysql --enable-assembler
[email protected]:~/mysql-5.1.63$ make
[email protected]:~/mysql-5.1.63$ make install
说明:编译过程中查看是否有报错信息,也可$?查看返回值是否为0

4、设置开机文件
[email protected]:~/mysql-5.1.63$ sudo cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
[email protected]:~/mysql-5.1.63$ sudo cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld

5、添加mysql用户
[email protected]:/usr/local/mysql$ sudo useradd mysql
[email protected]:/usr/local/mysql$ sudo chown -R mysql.mysql /usr/local/mysql
[email protected]:/usr/local/mysql$ sudo chown -R mysql var

6、开启mysql服务
[email protected]:/usr/local/mysql$ sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &

7、查看mysql进程
[email protected]:/usr/local/mysql$ ps -ef |grep mysql
root 2174 1 0 Jul18 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/var --pid-file=/usr/local/mysql/var/ubuntu.pid
mysql 2276 2174 0 Jul18 ? 00:00:00 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql --log-error=/usr/local/mysql/var/ubuntu.err --pid-file=/usr/local/mysql/var/ubuntu.pid --socket=/tmp/mysql.sock --port=3306
ubuntu 2309 1944 0 Jul18 pts/3 00:00:00 mysql -u root
ubuntu 3061 1606 0 00:16 pts/2 00:00:00 grep --color=auto mysql

8、查看mysql端口
[email protected]:/usr/local/mysql$ sudo netstat -anptu|grep 3306
[sudo] password for ubuntu:
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2276/mysqld

9、mysql的配置文件和启动文件
/etc/my.cnf
/etc/init.d/mysqld

10、查看mysql的运行状况
[email protected]:/usr/local/mysql$ sudo service mysqld status
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/init.d/mysqld; bad; vendor preset: enabled)
Active: active (running) since Wed 2018-07-18 22:37:10 PDT; 1h 41min ago
Docs: man:systemd-sysv-generator(8)
Process: 2139 ExecStop=/etc/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Process: 2160 ExecStart=/etc/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─2174 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/var --pid-file=/usr/local/mysql/var/ubuntu
└─2276 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql --log-e

Jul 18 22:37:09 ubuntu systemd[1]: Starting LSB: start and stop MySQL...
Jul 18 22:37:09 ubuntu mysqld[2160]: Starting MySQL
Jul 18 22:37:10 ubuntu mysqld[2160]: . *
Jul 18 22:37:10 ubuntu systemd[1]: Started LSB: start and stop MySQL.

11、登录mysql数据库(前面不要用sudo授权)
[email protected]:~$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.1.63-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

mysql>

总结:编译安装完成之后,如若不能正常启动mysql数据库可以重启系统再试一遍,或者在配置文件中排查一些是否有配置问题

方法二(适用centos、Redhat)
yum直接安装(配置好yum源)

1、安装mysql
yum install -y mysql*

2、启动mysql
systemctl start mysqld

3、查看mysql端口状态和进程
netstat -anptu|grep mysqld
ps -ef |grep mysqld

4、进入mysql
mysql -u root -p
输入密码
备注:安装完成mysql默认的密码放在/var/log/mysqld.log这个文件里
修改密码两种方法
1)ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘root‘; #第一个root是用户名,第二个root是设置的密码
2)set password for [email protected] = password(‘123‘); #括号里123位设置的密码
有可能有密码复杂策略要求 要设置满足复杂要求的密码

修改密码复杂度策略
#进入mysql
mysql -u root -p
#查看一下msyql密码相关的几个全局参数:
mysql> select @@validate_password_policy;
+----------------------------+
| @@validate_password_policy |
+----------------------------+
| MEDIUM |
+----------------------------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE ‘validate_password%‘;
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
6 rows in set (0.08 sec)

2)参数解释

validate_password_dictionary_file
插件用于验证密码强度的字典文件路径。

validate_password_length
密码最小长度,参数默认为8,它有最小值的限制,最小值为:validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)

validate_password_mixed_case_count
密码至少要包含的小写字母个数和大写字母个数。

validate_password_number_count
密码至少要包含的数字个数。

validate_password_policy
密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG。有以下取值:
Policy Tests Performed
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。

validate_password_special_char_count
密码至少要包含的特殊字符数。

3)修改mysql参数配置

[sql] view plain copy
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.05 sec)

mysql>
mysql>
mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE ‘validate_password%‘;
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_dictionary_file | |
| validate_password_length | 3 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 3 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 0 |
+--------------------------------------+-------+
6 rows in set (0.00 sec)

4)修改简单密码:

[sql] view plain copy
mysql> SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘123‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

以上是关于安装配置Mysql的主要内容,如果未能解决你的问题,请参考以下文章

MySQL的安装与配置

MySQL的安装与配置

MySQL的安装与配置

mysql安装配置教程

Hive应用实践-安装并配置mysql

MySQL-8.0.26免安装版配置步骤