ubuntu下mysql5.7啟動多資料庫例項
阿新 • • 發佈:2019-02-08
Mysql5.7建立啟動多個例項
找到mysql的配置檔案
my.cnf
,一般在/etc/mysql/my.cnf
,不在也沒關係,可以使用一下命令進行查詢:find / -name 'my.cnf'
鑑於mysql5.7裡面的
my.cnf
沒有多少配置內容,需要把mysqld.cnf
檔案裡的配置內容複製到my.cnf
裡。
下面操作假設當前路徑有
my_3307.cnf
檔案和mysql.conf.d
目錄,且該目錄下有mysqld.cnf
檔案# 將 mysqld.cnf 裡的配置內容新增到 my_3307.cnf 裡 grep -v '^#' mysql.conf.d/mysqld.cnf >> my_3307.cnf
-
cd /var/lib mkdir mysql_3307 chown -R mysql:mysql mysql_3307 vim /etc/apparmor.d/usr.sbin.mysqld # 新增 /var/lib/mysql_3307/ r # 新增 /var/lib/mysql_3307/** rwk # mysql_3307即為剛才建立的mysql新例項的資料夾 service apparmor restart
-
vim my_3307.cnf
修改後的配置檔案如下所示:
[mysqld] general_log_file = /var/lib/mysql_3307/op_mysql_3307.log
-
find / -name mysql_install_db # 或者使用如下兩條命令找出 mysql_install_db 所在目錄 find / -name mysql_install_db >>tmp.txt grep -n 'mysql_install' tmp.txt # 我的 mysql_install_db 在 /usr/bin/目錄下 ################################################# cd /usr/bin/ # 初始化資料庫例項 mysql_install_db --defaults-file=/etc/mysql/my_3307.cnf --basedir=/usr/ --datadir=/var/lib/mysql_3307 --user=mysql
-
# 後臺執行 mysql 例項 mysqld_safe --defaults-file=/etc/mysql/my_3307.cnf --user=mysql& # 檢視新的 mysql 例項是否成功啟動 netstat -nlt|grep 330[67] #如果3306埠的資料庫有啟動的話此時應該有兩個資料庫例項 ps -ef|grep mysql #ps命令也可以檢視新建立的 mysql 例項有沒有成功啟動
-
mysql -S /var/lib/mysql_3307/mysqld.sock -P 3307 # 若出現 Error 1045(28000):Access denied for user 'root'@'localhost' (using password:No) #需要把剛啟動的mysql例項停掉 ps -ef|grep mysql # 找出要停掉的mysql的pid kill 9 pid
修改my_3307.cnf配置檔案 —— vim /etc/mysql/my_3307.cnf
# 在my_3307.cnf中新增一行 skip-grant-tables
# 啟動mysql3307服務
mysqld_safe --defaults-file=/etc/mysql/my_3307.cnf --user=mysql&
# 登入mysql3307伺服器,在配置檔案加了 skip-grant-tables 可以不用密碼登入
mysql -S /var/lib/mysql_3307/mysqld.sock -P 3307
update mysql.user
set authentication_string=password('YourNewPassword')
where user='root' and Host ='localhost';
flush privileges;
quit;
停止mysql3307服務
mysql -S -u root -p YourNewPassword /var/lib/mysql_3307/mysqld.sock shutdown
修改my_3307.cnf配置檔案
vim /etc/mysql/my_3307.cnf
# 註釋掉 skip-grant-tables
mysql -u root -S /var/lib/mysql_3307/mysqld.sock -p
# 接著就會進入輸密碼的提示,輸入剛才修改的密碼就可以登入進去
# 停止mysql3307服務用上面的命令
mysqladmin -u root -S /var/lib/mysql_3307/mysqld.sock -p shutdown
# 接著在 “Enter password:” 輸入密碼
停止mysql服務時若出現如下錯誤:
mysqladmin:connect to server at localhost failed, error:”Your password has expired. To log in you must change it using a client that supports expired passwords.”
可按下面操作解決:
/*進入mysql命令列,執行一下命令*/
set PASSWORD = PASSWORD('YourNewPassword');
alter USER 'root'@'localhost' PASSWORD expire never;
flush privileges;