Mysql安裝、查看密碼、修改密碼、初始化、修改字符類型

安裝mysql

參照python篇一鍵安裝lnmp。安裝完之後再按照下面修改密碼,修改配置文件,否則安裝的時候就修改配置文件會出錯。

注意:這也是二進制安裝mysql。另一種二進制安裝容易出錯,生產環境不要用rpm包安裝,會出問題,測試過安裝msyql不要改裏面的字符類型,否則安裝時候報錯。等安裝完後再修改

2、查看初始化的密碼:

初始化mysql時生成的密碼

初始化mysql
2018-11-27T07:28:39.407117Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-27T07:28:40.069251Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-11-27T07:28:40.181277Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-11-27T07:28:40.194608Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 0fbb7543-f216-11e8-9f31-000c29c231f7.
2018-11-27T07:28:40.195834Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2018-11-27T07:28:40.197256Z 1 [Note] A temporary password is generated for root@localhost: 8YuFFueq4P!o

8YuFFueq4P!o 就是密碼

3、重置爲複雜密碼

3.1、配置免密登錄 、重啓mysql、登錄mysql

在[mysqld]下面添加skip-grant-tables

[root@ecs-03 mysql]#service mysqld stop
[root@ecs-03 mysql]# vi /etc/my.cnf
[mysqld]
skip-grant-tables   #跳過密碼

重啓數據庫

[root@ecs-03 mysql]# systemctl restart mysqld

一條命令執行以上步驟如下

systemctl stop mysqld && sed -i ‘/mysqld/a skip-grant-tables’ /etc/my.cnf && systemctl restart mysqld

登錄數據庫,下面直接回車 不用輸入密碼

[root@ecs-03 mysql]# mysql -u root -p
Enter password:

[root@ecs-02 mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright © 2000, 2018, 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>

3.2、 修改root數據庫賬號密碼方式一(推薦)

修改當前用戶爲root

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;
±--------------±--------------+
| host | user |
±--------------±--------------+
| % | root |
| 192.168.0.% | root |
| 192.168.0.1.% | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
±--------------±--------------+
6 rows in set (0.00 sec)

mysql> update user set host=’%’ where user=‘root’;
ERROR 1062 (23000): Duplicate entry ‘%-root’ for key ‘PRIMARY’ #不用管,沒有影響,直接下一步
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

重啓數數據,否則不生效

systemctl restart mysqld #等操作完下面一起重啓

授予root用戶給其他用戶授權的權限

update mysql.user set Grant_priv=‘Y’ where User=‘root’ and Host=’%’;

flush privileges;(刷新權限)

修改root密碼、開啓遠程連接授權

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

mysql> grant all privileges on . TO ‘root’@’%’ identified by ‘jenkins@123’ with grant option;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql>grant all on . to root@‘localhost’ identified by ‘jenkins@123’ with grant option;
mysql> alter user ‘root’@‘localhost’ identified by ‘jenkins@123’;
mysql> flush privileges;
Mysql>exit

修改後把kip-grant-tables註釋掉重啓mysql

systemctl stop mysqld && sed -i ‘s/skip-grant-tables/#skip-grant-tables/g’ /etc/my.cnf && systemctl restart mysqld

3.3、修改root數據庫賬號密碼方式二

mysql> set password=password(“youpassword”);

mysql> alter user ‘root’@‘localhost’ identified by ‘123456’;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

報錯不用管,接着輸入下面

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ identified by ‘zihao@666’;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql>grant all on . to root@‘localhost’ identified by ‘zihao@666’;
mysql> alter user ‘root’@‘localhost’ identified by ‘zihao@666’;
mysql> FLUSH PRIVILEGES;

修改後把kip-grant-tables註釋掉重啓mysql

systemctl stop mysqld && sed -i ‘s/skip-grant-tables/#skip-grant-tables/g’ /etc/my.cnf && systemctl restart mysqld

3.4、用修改後的密碼登錄mysql

[root@bogon local]# mysql -u root -pzihao@666
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright © 2000, 2018, 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>

4、重置爲簡單版密碼

4.1、配置免密登錄 、重啓mysql、登錄mysql

[root@ecs-03 mysql]# vi /etc/my.cnf

刪掉:kip-grant-tables ,
添加:validate_password=off :可以修改成簡單的密碼

然後重啓:

[mysqld]
validate_password=off
#skip-grant-tables

4.2、修改root數據庫賬號密碼同上

5、修改字符爲utf8mb4

安裝完成後需要修改字符滿足生產需求,之前公司就是總是出現亂碼,後來改成utf8mb4問題解決

臨時修改

mysql>show variables like ‘character%’;

mysql>set character_set_database=utf8mb4;

mysql>set character_set_server=utf8mb4;

永久修改

systemctl stop mysqld && sed -i -e ‘s/character-set-server = utf8/character-set-server = utf8mb4/g’ -e ‘/client/ a\default-character-set = utf8mb4’ -e ‘$a\default-character-set = utf8mb4’ /etc/my.cnf && systemctl start mysqld

注意:character_set_database=utf8mb4 這句話不要寫到配置文件裏面,否則啓動報錯

要達到的效果

mysql> show variables like ‘character%’;
±-------------------------±---------------------------------+
| Variable_name | Value |
±-------------------------±---------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
±-------------------------±---------------------------------+
8 rows in set (0.01 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章