安裝配置Mysql
方法一(適用ubuntu)
編譯安裝Mysql
1、安裝依賴環境
ubuntu@ubuntu:~$ 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源碼包並解壓
ubuntu@ubuntu:~$ ls
mysql-5.1.63.tar.gz mysql-5.1.63
3、編譯安裝
ubuntu@ubuntu:~$ cd mysql-5.1.63/
ubuntu@ubuntu:~/mysql-5.1.63$ make
ubuntu@ubuntu:~/mysql-5.1.63$ make install
說明:編譯過程中查看是否有報錯信息,也可$?查看返回值是否為0
4、設置開機文件
ubuntu@ubuntu:~/mysql-5.1.63$ sudo cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
ubuntu@ubuntu:~/mysql-5.1.63$ sudo cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
5、添加mysql用戶
ubuntu@ubuntu:/usr/local/mysql$ sudo useradd mysql
ubuntu@ubuntu:/usr/local/mysql$ sudo chown -R mysql.mysql /usr/local/mysql
ubuntu@ubuntu:/usr/local/mysql$ sudo chown -R mysql var
6、開啟mysql服務
ubuntu@ubuntu:/usr/local/mysql$ sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &
7、查看mysql進程
ubuntu@ubuntu:/usr/local/mysql$ ps -ef |grep mysql
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端口
ubuntu@ubuntu:/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的運行狀況
ubuntu@ubuntu:/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授權)
ubuntu@ubuntu:~$ 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 root@localhost = 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