配置YUM源
在MySQL官網中下載YUM源rpm安裝包:http://dev.mysql.com/downloads/repo/yum/
# 下載mysql源安裝包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm # 安裝mysql源 shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm
檢查mysql源是否安裝成功
shell> yum repolist enabled | grep "mysql.*-community.*"
輸出以下內容表示ok
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast !mysql-connectors-community/x86_64 MySQL Connectors Community 74 !mysql-tools-community/x86_64 MySQL Tools Community 74 !mysql57-community/x86_64 MySQL 5.7 Community Server 307
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改變默認安裝的mysql版本。比如要安裝5.6版本,將5.7源的enabled=1改成enabled=0。然后再將5.6源的enabled=0改成enabled=1即可。
安裝MySQL
先安裝相關依賴:
yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake bison git openssl openssl-devel
開始安裝:
shell> yum install mysql-community-server
啟動mysql
systemctl start mysqld
開機啟動
systemctl enable mysqld.service
停止服務
systemctl enable mysqld.service
重啟服務
systemctl restart mysqld.service
查看狀態
systemctl status mysqld.service
mysql查找報錯信息&配置信息
查找mysql路徑:
which mysqld
輸出:/usr/sbin/mysqld
查找my.cnf的路徑同時如果有報錯信息也會輸出
/usr/sbin/mysqld --verbose --help |grep -A 1 'Default options'
my.cnf基本配置(/etc/my.cnf)
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 lower_case_table_names=1 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' #validate_password = off [mysqld] #character_set_server=utf8 #init_connect='SET NAMES utf8' #skip-grant-tables wait_timeout=86400 character-set-server=utf8mb4 [mysql] default-character-set=utf8mb4
修改默認密碼
mysql5.7 會生成默認密碼在:/var/log/mysqld.log [Note] A temporary password is generated for root@localhost: lQidlh;BX4*x
通過命令查看默認密碼
grep 'temporary password' /var/log/mysqld.log
密碼不對 or 找不到密碼怎嘛辦..?
- 修改配置文件
vim /etc/my.cnf
在[mysqld]節點添加 skip-grant-tables
- 重啟mysql
- 用空密碼進入
mysql -uroot
update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost'; flush privileges; exit;
- 還原my.cnf
授權遠程連接權限
mysql -u root -p
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION; FLUSH PRIVILEGES;#重載授權表: exit;